2016-03-21 166 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