您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将matplotlib图形传递给HTML(flask)

将matplotlib图形传递给HTML(flask)

你必须将HTML和图像分成两个不同的路径。

你的/images/<cropzonekey>路线将仅投放该页面,并且该页面的HTML内容中将引用第二条路线,即用于提供图片的路线。

映像是通过你使用生成的内存文件以其自己的路径提供的savefig()

我显然没有对此进行测试,但是我相信以下示例将按原样运行或使你接近可行的解决方案:

@app.route('/images/<cropzonekey>')
def images(cropzonekey):
    return render_template("images.html", title=cropzonekey)

@app.route('/fig/<cropzonekey>')
def fig(cropzonekey):
    fig = draw_polygons(cropzonekey)
    img = StringIO()
    fig.savefig(img)
    img.seek(0)
    return send_file(img, mimetype='image/png')

你的images.html模板将变为:

<html>
  <head>
    <title>{{ title }} - image</title>
  </head>
  <body>
    <img src="{{ url_for('fig', cropzonekey = title) }}" alt="Image Placeholder" height="100">
  </body>
</html>
Python 2022/1/1 18:25:01 有302人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶