Skip to content

Commit 8cf9690

Browse files
committed
move over all example notebooks
1 parent 81a6f65 commit 8cf9690

62 files changed

Lines changed: 3753 additions & 480 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/_static/flask_example.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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)

docs/advanced_guide.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ Advanced guide
66

77
advanced_guide/flask
88
advanced_guide/choropleth with Jenks natural breaks optimization
9+
advanced_guide/subplots
910
advanced_guide/colormaps
1011
advanced_guide/world_copy
1112
advanced_guide/custom_panes
1213
advanced_guide/geodedetic_image_overlay
1314
advanced_guide/custom_tiles
15+
advanced_guide/piechart_icons
16+
advanced_guide/polygons_from_list_of_points

docs/advanced_guide/flask.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ or extract the map components and use those.
88

99
Below is a script containing examples for all three use cases:
1010

11-
.. literalinclude:: ../examples/flask_example.py
11+
.. literalinclude:: /_static/flask_example.py
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Piechart icons
2+
3+
In this example we show how you can make mini-charts on several locations.
4+
We'll make little piecharts showing the number of consonants and vowels in
5+
a couple of languages. Those piecharts will be included as icons on the map.
6+
7+
```{code-cell} ipython3
8+
import os
9+
import ast
10+
11+
import pandas
12+
13+
data = pandas.read_csv(
14+
"../data/consonants_vowels.csv",
15+
# To ensure that tuples are read as tuples
16+
converters={"coordinates": ast.literal_eval},
17+
)
18+
19+
data.head()
20+
```
21+
22+
## Pie charts
23+
24+
```{code-cell} ipython3
25+
import io
26+
27+
import matplotlib.pyplot as plt
28+
29+
pie_charts_data = zip(data.consonants, data.vowels)
30+
31+
fig = plt.figure(figsize=(0.5, 0.5))
32+
fig.patch.set_alpha(0)
33+
ax = fig.add_subplot(111)
34+
plots = []
35+
for sizes in pie_charts_data:
36+
ax.pie(sizes, colors=("#e6194b", "#19e6b4"))
37+
buff = io.StringIO()
38+
plt.savefig(buff, format="SVG")
39+
buff.seek(0)
40+
svg = buff.read()
41+
svg = svg.replace("\n", "")
42+
plots.append(svg)
43+
plt.cla()
44+
plt.clf()
45+
plt.close()
46+
```
47+
48+
## Legend
49+
50+
```{code-cell} ipython3
51+
import branca
52+
53+
legend_html = """
54+
{% macro html(this, kwargs) %}
55+
<div style="
56+
position: fixed;
57+
bottom: 50px;
58+
left: 50px;
59+
width: 250px;
60+
height: 80px;
61+
z-index:9999;
62+
font-size:14px;
63+
">
64+
<p><a style="color:#e6194b;font-size:150%;margin-left:20px;">◼</a>&emsp;Consonants</p>
65+
<p><a style="color:#19e6b4;font-size:150%;margin-left:20px;">◼</a>&emsp;Vowels</p>
66+
</div>
67+
<div style="
68+
position: fixed;
69+
bottom: 50px;
70+
left: 50px;
71+
width: 150px;
72+
height: 80px;
73+
z-index:9998;
74+
font-size:14px;
75+
background-color: #ffffff;
76+
filter: blur(8px);
77+
-webkit-filter: blur(8px);
78+
opacity: 0.7;
79+
">
80+
</div>
81+
{% endmacro %}
82+
"""
83+
84+
legend = branca.element.MacroElement()
85+
legend._template = branca.element.Template(legend_html)
86+
```
87+
88+
## Map
89+
90+
```{code-cell} ipython3
91+
import folium
92+
93+
94+
m = folium.Map(location=(0, 0), zoom_start=2)
95+
96+
for i, coord in enumerate(data.coordinates):
97+
marker = folium.Marker(coord)
98+
icon = folium.DivIcon(html=plots[i])
99+
marker.add_child(icon)
100+
popup = folium.Popup(
101+
"Consonants: {}<br>\nVowels: {}".format(data.consonants[i], data.vowels[i])
102+
)
103+
marker.add_child(popup)
104+
m.add_child(marker)
105+
m.get_root().add_child(legend)
106+
m
107+
```

0 commit comments

Comments
 (0)