6

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

Kristifer Szabo
  • 519
  • 3
  • 7
  • 16

1 Answers1

8

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>
Suever
  • 64,497
  • 14
  • 82
  • 101
  • hey, thanks for the info. however I still can't get it to work. I have first endpoint `@app.route('/')` which ends with `return render_template('index.html')`, then another endpoint `@app.route('/data/map.html')` with `return send_file('data/plot.html')`. However if I load the page I get `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 Feb 28 '18 at 14:06
  • 1
    ah it worked adding `static_folder='data'`to the `Flask(...)` initialization! – lorenzori Feb 28 '18 at 14:39
  • @lorenzori I have a similar error but I'm trying to embed the folium map into my index.html. Currently the html page works fine on its own but under flask's render_template('index.html') it doesn't. Any ideas? – Malcolm Leck Mar 29 '20 at 15:20