Python 在Flask中动态渲染具有相同文件名的更改图像
我有一个烧瓶视图功能,如下所示:Python 在Flask中动态渲染具有相同文件名的更改图像,python,html,flask,Python,Html,Flask,我有一个烧瓶视图功能,如下所示: @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(sele
@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")
Outputs.html:
<html>
<head>
</head>
<body>
<h1>Output page</h1>
<img src="static/graph.png" />
</body>
</html>
输出页
我使用flask view函数通过outputs.html文件显示图像。这里需要注意的是,所提供的静态图像文件每次都会根据用户输入而不断变化。换句话说,我一直根据用户选择的输入覆盖图像文件
但问题是没有提供更改的图像文件。首次渲染时使用的旧图像文件仅针对用户的每个新输入显示
我已经提到了关于在flask中提供动态内容的老帖子。但是没有一个有用。您遇到了缓存问题。静态资源(如图像)缓存在服务器和浏览器之间链的每个点上。这是一件好事。大多数合理的系统设置为在服务器上缓存图像至少1年(如果图像未缓存在浏览器中) 要解决此缓存问题,您需要(i)为文件提供新名称,(ii)重新配置
Vary
头以指示它们不应被缓存,或者(iii)添加唯一性片段——例如,不使用static/graph.png
,而是添加时间戳static/graph.png?v='+(new Date()).valueOf()
或md5哈希
更新:丁科给了你一个很好的答案(一定要阅读他提供的链接)。要在服务器端添加缓存破坏,而不创建新文件,可以计算md5校验和(缺点:需要读取整个文件):
或者使用上次修改的属性(不总是可靠):
只要文件不变,这两种方法都将提供缓存版本。的解决方案有效。我在Stack Overflow上发现了多篇文章,它们都提出了相同的解决方案。要查看它们,请在谷歌上搜索如何不缓存图像
下面是我对你问题的解决方案。这将删除图形文件,并在对/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)
Outputs.html
<html>
<head>
</head>
<body>
<h1>Output page</h1>
<img src="{{ url_for('static', filename=graph) }}" />
</body>
</html>
输出页
路径中的反斜杠“\”可能应该转义。@b约恩我已更改了该行的格式。您提到的唯一性语句('static/graph.png?v='+(new Date()).valueOf())抛出语法错误。你确定这是一个有效的python语句吗。你能再详细一点吗?我不想更改文件名,因为我不愿意转储内存。
@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)
<html>
<head>
</head>
<body>
<h1>Output page</h1>
<img src="{{ url_for('static', filename=graph) }}" />
</body>
</html>