你必须将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>