2016-03-21 360 views
3

所以我使用這個很酷的插件叫Folium創建地圖。地圖以.html格式創建,每次更新地圖時都會重新生成html。因此,爲了在同一頁面上顯示地圖和我的導航欄以及其他內容,我想我需要將map.html放在iframe的籠子裏,它可以隨意刷新。使用Flask嵌入本地HTML頁面

的地圖被由此產生:

map1 = folium.Map(location=[45.5, -73.61], width="100%", height="100%") 
map1.save('./maps/map.html') 

我試圖這樣iframeing它:

<iframe src="/maps/map.html"></iframe> 

,但我得到404 error

有人昨天建議我建造了它的終點像這樣:

@app.route('/http://127.0.0.1:4995/maps/map') 
def show_map(): 
return flask.send_file('/maps/map.html') 

但我總是收到404錯誤

回答

6

您的路由定義不正確。正如你寫的那樣,你定義了http://yourserver/http://127.0.0.1:4995/maps/map的路線,而我認爲你想要的路線是http://yourserver/maps/map.html。要做到這一點,你將要使用以下

@app.route('/maps/map.html') 
def show_map(): 
    return flask.send_file('/maps/map.html') 

瓶會自動在前面加上你的服務器的地址(http://127.0.0.1:4995)您定義的任何路線的開始。

另外,在您的HTML模板中,我將使用url_for來獲取地圖的URL,以避免需要更改模板的路線更改。

<iframe src="{{ url_for('show_map') }}"></iframe> 
+0

嘿,感謝您的信息。但是我仍然無法讓它工作。我有第一個端點'@ app.route('/')',它以'return render_template('index.html')'結尾,然後是另一個端點'@ app.route('/ data/map.html')' '返回send_file('data/plot.html')'。然而,如果我加載頁面,我得到'127.0.0.1 - - [28/Feb/2018 15:03:48]「GET/HTTP/1.1」200 - 127.0.0.1 - - [28/Feb/2018 15:03 :48]「GET /data/plot.html HTTP/1.1」404 -' – lorenzori

+0

啊,它在'Flask(...)'初始化中加入'static_folder ='data''! – lorenzori