如何渲染和返回剧情在烧瓶中查看? [英] How to render and return plot to view in flask?
本文介绍了如何渲染和返回剧情在烧瓶中查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 flask
?
devices.py:
@ devices_blueprint.route('/ devices / test /')
def test():
y = [1,2,3,4,5]
x = [0,2,1,3,4]
plot_url = plt.plot(x,y)
return render_template('devices / test.html',plot_url = plot_url)
test.html
< div class =container>
< h2>图片< / h2>
< img src = {{resized_img_src('plot_url')}} class =img-roundedalt =aquiwidth =304height =236>
< / div>
我试图用 seaborn
但即使使用 matplolib
我也得不到任何结果。
注意:我不想保存图片并加载它。
解决方案使用
matplotlib
可以这样做: / p> #添加此导入
导入StringIO
导入base64
@devices_blueprint。 route('/ devices / test /')
def test():
img = StringIO.StringIO()
y = [1,2,3,4,5]
x = [0,2,1,3,4]
plt.plot(x,y)
plt.savefig(img,format ='png')
img.seek(0)
plot_url = base64.b64encode(img.getvalue())
return render_template('test.html',plot_url = plot_url)
在您的Html文件中:
< img src =data:image / png; base64,{{plot_url}}>
如果您想使用 seaborn ,你只需要 import seaborn
并设置你想要的样式,例如
...
导入seaborn作为s
...
@ devices_blueprint.route('/ devices / test /')
def test():
$ b $ img = StringIO.StringIO()
sns.set_style(dark)#EG
y = [1,2,3,4,5]
x = [0,2,1,3,4]
plt.plot(x, y)
plt.savefig(img,format ='png')
img.seek(0)
plot_url = base64.b64encode(img.getvalue())
return render_template('test.html',plot_url = plot_url)
How do I render a plot to the view in flask
?
devices.py:
@devices_blueprint.route('/devices/test/')
def test():
y = [1,2,3,4,5]
x = [0,2,1,3,4]
plot_url = plt.plot(x,y)
return render_template('devices/test.html', plot_url=plot_url)
test.html
<div class="container">
<h2>Image</h2>
<img src= {{ resized_img_src('plot_url') }} class="img-rounded" alt="aqui" width="304" height="236">
</div>
Im trying to use seaborn
with this, but even with matplolib
I couldn’t get any result.
Note: I don’t want to save image and load it after.
解决方案
With matplotlib
you can do:
#Add this imports
import StringIO
import base64
@devices_blueprint.route('/devices/test/')
def test():
img = StringIO.StringIO()
y = [1,2,3,4,5]
x = [0,2,1,3,4]
plt.plot(x,y)
plt.savefig(img, format='png')
img.seek(0)
plot_url = base64.b64encode(img.getvalue())
return render_template('test.html', plot_url=plot_url)
In your Html put:
<img src="data:image/png;base64, {{ plot_url }}">
If you want to use seaborn, you just need to import seaborn
and set the styles you want, e.g.
...
import seaborn as sns
...
@devices_blueprint.route('/devices/test/')
def test():
img = StringIO.StringIO()
sns.set_style("dark") #E.G.
y = [1,2,3,4,5]
x = [0,2,1,3,4]
plt.plot(x,y)
plt.savefig(img, format='png')
img.seek(0)
plot_url = base64.b64encode(img.getvalue())
return render_template('test.html', plot_url=plot_url)
这篇关于如何渲染和返回剧情在烧瓶中查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文