【发布时间】:2019-05-14 23:38:38
【问题描述】:
我正在关注这个answer,我想使用 render_template 来调用我的 html 文件,而不是直接在我的 py 中运行 plot。
我想使用类似的东西:
return render_template('hello.html', plot_url)
代替:
return '<img src="data:image/png;base64,{}">'.format(plot_url)
我的问题是,是否有任何方法可以将绘图传递给 html 文件,然后在烧瓶中运行?
编辑:
@app.route('/')
def build_Plot():
y = [1, 2, 3, 4, 5]
x = [0, 2, 1, 3, 4]
plt.plot(x, y)
output = io.BytesIO()
plt.savefig(output, format='png')
output.seek(0)
plot_data = base64.b64encode(output.getvalue()).decode()
return render_template("home.html", url=plot_data)
在 home.html 中:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>MyFlaskApp</title>
</head>
<body>
{% block body %}
<img src="data:image/png;base64 = {{ url }} "/>
{% endblock %}
</body>
</html>
【问题讨论】:
标签: python html python-3.x flask server