File tree Expand file tree Collapse file tree 2 files changed +70
-8
lines changed
Expand file tree Collapse file tree 2 files changed +70
-8
lines changed Original file line number Diff line number Diff line change 11Using folium with flask
22=======================
33
4- A very common use case is to use folium with in a flask app.
5- The trick is to return folium's HTML representation.
6- Here is an example on how to do that:
4+ A common use case is to use folium in a flask app. There are multiple ways you
5+ can do that. The simplest is to return the maps html representation. If instead
6+ you want to embed a map on an existing page, you can either embed an iframe
7+ or extract the map components and use those.
78
9+ Below is a script containing examples for all three use cases:
810
911.. literalinclude :: ../examples/flask_example.py
Original file line number Diff line number Diff line change 1414
1515"""
1616
17- from flask import Flask
17+ from flask import Flask , render_template_string
1818
1919import folium
2020
2121app = Flask (__name__ )
2222
2323
2424@app .route ("/" )
25- def index ():
26- start_coords = (46.9540700 , 142.7360300 )
27- folium_map = folium .Map (location = start_coords , zoom_start = 14 )
28- return folium_map ._repr_html_ ()
25+ def fullscreen ():
26+ """Simple example of a fullscreen map."""
27+ m = folium .Map ()
28+ return m .get_root ().render ()
29+
30+
31+ @app .route ("/iframe" )
32+ def iframe ():
33+ """Embed a map as an iframe on a page."""
34+ m = folium .Map ()
35+
36+ # set the iframe width and height
37+ m .get_root ().width = "800px"
38+ m .get_root ().height = "600px"
39+ iframe = m .get_root ()._repr_html_ ()
40+
41+ return render_template_string (
42+ """
43+ <!DOCTYPE html>
44+ <html>
45+ <head></head>
46+ <body>
47+ <h1>Using an iframe</h1>
48+ {{ iframe|safe }}
49+ </body>
50+ </html>
51+ """ ,
52+ iframe = iframe ,
53+ )
54+
55+
56+ @app .route ("/components" )
57+ def components ():
58+ """Extract map components and put those on a page."""
59+ m = folium .Map (
60+ width = 800 ,
61+ height = 600 ,
62+ )
63+
64+ m .get_root ().render ()
65+ header = m .get_root ().header .render ()
66+ body_html = m .get_root ().html .render ()
67+ script = m .get_root ().script .render ()
68+
69+ return render_template_string (
70+ """
71+ <!DOCTYPE html>
72+ <html>
73+ <head>
74+ {{ header|safe }}
75+ </head>
76+ <body>
77+ <h1>Using components</h1>
78+ {{ body_html|safe }}
79+ <script>
80+ {{ script|safe }}
81+ </script>
82+ </body>
83+ </html>
84+ """ ,
85+ header = header ,
86+ body_html = body_html ,
87+ script = script ,
88+ )
2989
3090
3191if __name__ == "__main__" :
You can’t perform that action at this time.
0 commit comments