2016-12-21 45 views
-1

我的函數調用Matplotlib根據用戶輸入的日期創建和保存一個圖形。Matplotlib:圖像不會刷新一個新的無花果()

我保存在我的靜態文件夾中的新的數字是這樣的:

plt.savefig('insights/static/chart.png') 
return render_template("chart.html") 

我的模板調用圖是這樣的:

<img src="/static/chart.png"> 

當用戶輸入新日期Matplotlib創建一個新的數字,以靜態方式覆蓋現有的圖形文件。服務器上的圖形已更新,但我的模板中顯示的圖像不是。要更新新圖,我需要保存模板。

我認爲我有一個緩存問題...任何人都可以提出一個解決方法嗎?

+0

在圖像名稱中使用日期和時間 - 並將此名稱發送到模板。或者至少嘗試'src =「/ static/chart.png?var = some_random_value」',瀏覽器應該把這個名字當作新文件。 – furas

+1

我不錯過一些框架上下文嗎?模板,緩存,靜態文件夾......背景是什麼? –

+0

服務器將'src =「/ static/chart.png?var = some_random_value」'作爲src =「/ static/chart.png」'(並且它什麼都不會改變),但是對於瀏覽器來說它是不同的鏈接(不同的文件)它沒有緩存 - 所以它必須再次下載。 – furas

回答

0

我認爲有很多解決方案可以強制瀏覽器刷新圖像或整個頁面內容。

Is there a way to force browsers to refresh/download images?

Refresh image with a new one at the same url

What is the best way to force an image refresh on a webpage?

How to force a web browser NOT to cache images

PHP force refresh image

一些使用JavaScript,但別人不一樣。兩個主要概念是:

  1. 使用不同的文件名,每次或使用GET變量(?var=...
  2. 將HTML Meta標籤以防止緩存。
+0

非常感謝。我想出瞭如何使用你提到的一些例子來做到這一點。我決定使用時間戳命名我的文件: – aviss

+0

'timestr = time.strftime(「%Y%m%d-%H%M%S」) plt.savefig('insights/static/charts /'+ timestr +'。 png') return render_template(「chart.html」,timestr = timestr)' – aviss