Python 官方文档:入门教程 => 点击学习
WEB框架flask中引入pyecharts代码运行,出错显cannot import name 'Markup' from 'jinja2'(如图1),分析发现在jinja2版本过高 3.1.2中已经去掉了Markup,改为marupsa
WEB框架flask中引入pyecharts代码运行,出错显cannot import name 'Markup' from 'jinja2'(如图1),分析发现在jinja2版本过高 3.1.2中已经去掉了Markup,改为marupsafe可实现。
from flask import Flaskfrom jinja2 import Markup, Environment, FileSystemLoaderfrom pyecharts.globals import CurrentConfig# 关于 CurrentConfig,可参考 [基本使用-全局变量]CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates"))from pyecharts import options as optsfrom pyecharts.charts import Barapp = Flask(__name__, static_folder="templates")def bar_base() -> Bar: c = ( Bar() .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]) .add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) .add_yaxis("商家B", [15, 25, 16, 55, 48, 8]) .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题")) ) return c@app.route("/")def index(): c = bar_base() return Markup(c.render_embed())if __name__ == "__main__": app.run()
将代码改一下,
from jinja2.utils import markupsafe
def index(): c = bar_base() return markupsafe.Markup(c.render_embed())
代码如下:
from flask import Flask# from jinja2 import Markupfrom jinja2.utils import markupsafefrom pyecharts import options as optsfrom pyecharts.charts import Barapp = Flask(__name__, static_folder="templates")def bar_base() -> Bar: c = ( Bar() .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]) .add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) .add_yaxis("商家B", [15, 25, 16, 55, 48, 8]) .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题")) ) return c@app.route("/")def index(): c = bar_base() return markupsafe.Markup(c.render_embed())if __name__ == "__main__": app.run()
运行结果:
来源地址:https://blog.csdn.net/u014125025/article/details/129064453
--结束END--
本文标题: cannot import name Markup from jinja2解决方案
本文链接: https://lsjlt.com/news/425701.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0