简体   繁体   English

如何在鳗鱼python中使用Jinja2模板?

[英]How to use Jinja2 template in eel python?

I want to create an desktop application in python, so I started using the eel library to easily design a nice looking GUI application using html, css & javascript.我想在 python 中创建一个桌面应用程序,所以我开始使用 eel 库来轻松设计一个漂亮的 GUI 应用程序,使用 html、ZC7A628CBA22E28EB17B5F5C6AE2A2662919B9ED78DEE27。

I have the following code in which I have imported the eel module and started the application with web/templates/index.html file to display as the main page & I have created a variable name which I want to use in the template and display it's value:我有以下代码,其中我导入了 eel 模块并使用web/templates/index.html文件启动应用程序以显示为主页,并且我创建了一个变量name ,我想在模板中使用它并显示它价值:

main.py主文件

import eel
import pyautogui

name = 'Ajay'

eel.init('web')
eel.start(
    'templates/index.html', 
    size=pyautogui.size(), 
    jinja_templates='web/templates'
)

project's directory tree项目的目录树

FirstProj
  - main.py
  - web/
      - templates/
          - index.html

index.html索引.html

<html>
   <head>
       <script type="text/javascript" src="/eel.js"></script>
   </head>
   <body>
       <h1> Hello {{name}}! </h1>
   </body>
</html>

I found out that using the jinja2_template method of eel.btl I can fill the values context values into the template.我发现使用eel.btljinja2_template方法可以将值上下文值填充到模板中。 but how to render it?但是如何渲染呢? I tried the following but it didn't work:我尝试了以下但没有奏效:

import eel, pyautogui

context = { 'name': 'Ajay' }
temp = eel.btl.jinja2_template('web/templates/index.html', context)

eel.init()
eel.start(temp, size=pyautogui.size(), jinja_templates='web/templates')

It showed me the following error page:它向我显示了以下错误页面:

错误

Any help will be appreciated.任何帮助将不胜感激。 Thanks in advance!提前致谢!

I found the solution installing eel[jinja2] after that it needs to have a structure folder like我找到了安装eel[jinja2]的解决方案,之后它需要一个结构文件夹,如

.
├── app.py
└── web
    ├── css
    │   ├── bulma.min.css
    │   └── main.css
    ├── js
    │   └── main.js
    └── templates
        ├── base.html
        └── index.html

for start in app.pyapp.py开始

eel.start(
    'templates/index.html',
    jinja_templates='templates'
)

in the base.html在底座中base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Eel example</title>

    <link rel="stylesheet" href="../css/main.css" />
    <link rel="stylesheet" href="../css/bulma.min.css" />
    <script type="text/javascript" src="/eel.js"></script>
</head>
<body>
  {% block content %}
  {% endblock %}
</body>
<script src="../js/main.js"></script>
</html>

and in index.html并在索引中index.html

{% extends "base.html" %}

{% block content %}
<h1 class="title has-text-centered">Hello world!</h1>
{% endblock %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM