gpt4 book ai didi

python - 如何在 wxpython html 窗口中插入动态字符串? (wx.html.html 窗口)

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:40 26 4
gpt4 key购买 nike

我正在用 wxpython 制作一个 html 窗口并想打印它。在此之前,我需要在 html 页面中输入用户输入(例如他的名字或类似的东西)。如何做到这一点?提前致谢,

最佳答案

使用Jinja2 .

在需要显示用户输入数据的地方创建一个带有变量的 HTML 模板。然后使用包含该数据的字典呈现模板。

在这里,我将为您编写一个辅助模块。

# templates.py
import jinja2 as jinja

def create_env():
loader = jinja.FileSystemLoader(PATH_TO_YOUR_TEMPLATES)
env = jinja.Environment(loader=loader)
return env

env = create_env()

def render(name, context=None):
context = context or {}
return env.get_template(name).render(context)

# my_module.py
import templates

data = {
'first_name': 'John',
'last_name': 'Smith',
}

html = templates.render('my_template.html', data)
# do something with html string

# my_template.html
<p>Hello, {{ first_name }} {{ last_name }}. This is a template.</p>

关于python - 如何在 wxpython html 窗口中插入动态字符串? (wx.html.html 窗口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3287455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com