使用Flask嵌入本地HTML页面 [英] Using Flask to embed a local HTML page

查看:1359
本文介绍了使用Flask嵌入本地HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用这个叫做Folium的酷插件来创建地图。地图被创建为.html,每次更新地图时都会重新生成html。所以为了显示地图和我的导航栏和其他东西在同一页面上,我想我需要将if.html里面的map.html放在里面,它可以随意刷新。



$ p
$ b $ pre $ map $ = =100%,height =100%)
map1.save('./ maps / map.html')

我已经尝试了iframe,因此:

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

但是我得到 404错误



昨天有人建议我为它建立一个端点:$ b​​
$ b pre $ $ $ c $ @ app.route('/http://127.0.0.1:4995/maps/map')
def show_map():
返回flask.send_file('/ maps / map.html')

但是我一直得到404错误

解决方案

你的路线定义不正确。就像你写的那样,你为 http:// yourserver / http://127.0.0.1:4995 / maps / map 定义了路线,而不是我认为你想要的路线是 http://yourserver/maps/map.html 。为了达到这个目的,你需要使用下面的代码:

$ $ $ $ $ $ c $ @ app.route('/ maps / map.html')
def show_map():
返回flask.send_file('/ maps / map.html')

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



另外,在您的HTML模板中,我会使用 url_for 来获取地图的URL,以避免路线变化,需要更改您的模板。

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


So I am using this cool plugin called Folium which creates maps. The map gets created as a .html and every time you update the map it regenerates the html. So in order to display the map and my navbar and other stuff on the same page I think I would need to put map.html inside an iframe cage where it can refresh itself at will.

The map gets created thus:

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

And I have tried iframeing it thus:

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

But I get 404 error

Someone yesterday suggested that I build an endpoint for it like this:

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

But I keep getting 404 error

解决方案

You have your route defined incorrectly. As you had it written, you defined the route for http://yourserver/http://127.0.0.1:4995/maps/map when instead what I think you wanted was http://yourserver/maps/map.html. To achieve this, you will want to use the following

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

Flask will automatically prepend your server's address (http://127.0.0.1:4995) to the beginning of any route that you define.

Also, in the template for your HTML, I would use url_for to get the URL for the map to avoid changes in your routes requiring changes to your templates.

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

这篇关于使用Flask嵌入本地HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆