我有一个烧瓶视图功能如下:
@app.route('/myfunc', methods = ['POST', 'GET'])
def myfunc():
var = request.form["samplename"]
selected_ecg=ecg.loc[ecg['Patient ID'].isin([var])]
selected_ecg = selected_ecg.drop('Patient ID', 1)
arr = np.array(selected_ecg)
y = arr.T
x=np.array(range(1,189))
plot.plot(x,y)
#Remove the old file
os.remove("static\graph.png")
#Now save the new image file
plot.savefig("static\graph.png")
return render_template("outputs.html")
Run Code Online (Sandbox Code Playgroud)
Outputs.html:
<html>
<head>
</head>
<body>
<h1>Output page</h1>
<img src="static/graph.png" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我使用flask视图功能通过outputs.html文件显示图像.这里的问题是,服务的静态图像文件每次都会根据用户输入而不断变化.换句话说,我会根据用户选择的输入覆盖图像文件.
但问题是没有提供更改的图像文件.用于第一次渲染的旧图像文件仅针对用户的每个新输入显示.
我已经提到了有关在烧瓶中提供动态内容的旧帖子.但它们都没有用.
thebjorn的解决方案是有效的。我在 Stack Overflow 上发现了多个帖子,其中提出了相同的解决方案。要查看它们,请how to not cache images在 Google 上搜索。链接 链接2 LINK3
以下是我对您的问题的解决方案。这将删除图形文件并在对/myfunc 的每个 GET 请求上使用 plot.savefig 创建新的图形文件。我不确定您想要这种行为的请求。
@app.route('/myfunc', methods = ['POST', 'GET'])
def myfunc():
var = request.form["samplename"]
selected_ecg=ecg.loc[ecg['Patient ID'].isin([var])]
selected_ecg = selected_ecg.drop('Patient ID', 1)
arr = np.array(selected_ecg)
y = arr.T
x=np.array(range(1,189))
plot.plot(x,y)
new_graph_name = "graph" + str(time.time()) + ".png"
for filename in os.listdir('static/'):
if filename.startswith('graph_'): # not to remove other images
os.remove('static/' + filename)
plot.savefig('static/' + new_graph_name)
return render_template("outputs.html", graph=new_graph_name)
Run Code Online (Sandbox Code Playgroud)
输出.html
<html>
<head>
</head>
<body>
<h1>Output page</h1>
<img src="{{ url_for('static', filename=graph) }}" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
711 次 |
| 最近记录: |