我想弄清楚如何使用 Flask 来显示一个简单的图像并使用新图像进行更新。我有以下似乎适用于图像的内容(几乎来自 MPL 网站):
@app.route("/image")
def show_image():
to_show = do_stuff(os.listdir( current_dir )[-1] ) #turn file into 2d np.array
fig, ax = plt.subplots()
ax.imshow( to_show, **plot_pars)
buf = BytesIO()
fig.savefig(buf, format = "png")
plot_url = base64.b64encode(buf.getvalue().decode("utf8")
return render_template("index.html", plot_url = plot_url)
index.html
文件当前设置为每 15 秒刷新一次
<!DOCTYPE html>
<html>
<head>
<title> Plot</title>
<meta http-equiv='refresh' content="15>
</head>
<body>
<img src="data:image/png;base64, {{ plot_url }}">
</body>
</html>
我很好奇,如果我的 current_dir
的长度发生变化,我是否可以设置一些基本上可以运行 show_image 的东西。如果我在我的 show_image
函数 (if os.listdir( current_dir) != nfiles
) 中尝试它,但没有任何变化,所以我试图找出从这个“触发器”开始的最佳方式。