使用Flask嵌入本地HTML页面

Kri*_*abo 5 flask python-3.x folium

所以我使用这个名为Folium的酷插件来创建地图.地图被创建为.html,每次更新地图时都会重新生成html.因此,为了在同一页面上显示地图和我的导航栏以及其他内容,我认为我需要将map.html放在iframe笼子里,它可以随意刷新.

因此创建了地图:

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

我已经尝试过iframeing它:

<iframe src="/maps/map.html"></iframe>
Run Code Online (Sandbox Code Playgroud)

但我明白了 404 error

有人昨天建议我为它构建一个端点,如下所示:

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

但我一直收到404错误

Sue*_*ver 8

您的路线定义不正确.正如你所写的那样,你定义了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')
Run Code Online (Sandbox Code Playgroud)

Flask会自动将服务器的地址(http://127.0.0.1:4995)添加到您定义的任何路径的开头.

此外,在HTML的模板中,我将使用url_for获取地图的URL来避免路线中的更改需要更改模板.

<iframe src="{{ url_for('show_map') }}"></iframe>
Run Code Online (Sandbox Code Playgroud)