Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/docs/_static/flask_example.py
1601 views
1
"""flask_example.py
2
3
Required packages:
4
- flask
5
- folium
6
7
Usage:
8
9
Start the flask server by running:
10
11
$ python flask_example.py
12
13
And then head to http://127.0.0.1:5000/ in your browser to see the map displayed
14
15
"""
16
17
from flask import Flask, render_template_string
18
19
import folium
20
21
app = Flask(__name__)
22
23
24
@app.route("/")
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
)
89
90
91
if __name__ == "__main__":
92
app.run(debug=True)
93
94