欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python flask框架如何显示图像到web页面

时间:2021-01-11 11:02:54|栏目:Python代码|点击:

代码如下

webfig1.py

from flask import Flask
from flask import render_template
import matplotlib.pyplot as plt
import io
import base64

app = Flask(__name__)

@app.route('/')
def build_plot():
  img = io.BytesIO()
  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()).decode()

  return render_template('plot.html', plot_url=plot_url)

if __name__ == '__main__':
  app.debug = True
  app.run()

plot.html

<!DOCTYPE html>
<html>
<title> Plot</title>
<body>
<img src="data:image/png;base64, {{ plot_url }}">
</body>
</html>

先用py绘制了xy的图像,然后经过几个命令,让其转化为plot_url,在传给plot.html,就可以了

代码在github:https://github.com/qingnvsue/flask中的webfig文件夹

我自己的程序是在网页输入sin函数的幅度,频率,自变量范围等,然后绘制这个sin函数,让其显示到web页面,如图

代码在github:https://github.com/qingnvsue/flask中的sin文件夹

上一篇:对Python中DataFrame按照行遍历的方法

栏    目:Python代码

下一篇:TensorFLow 变量命名空间实例

本文标题:Python flask框架如何显示图像到web页面

本文地址:http://www.codeinn.net/misctech/43819.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有