Skip to content

Commit

Permalink
move over all example notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Conengmo committed May 19, 2023
1 parent 81a6f65 commit 8cf9690
Show file tree
Hide file tree
Showing 62 changed files with 3,753 additions and 480 deletions.
92 changes: 92 additions & 0 deletions docs/_static/flask_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
""" flask_example.py
Required packages:
- flask
- folium
Usage:
Start the flask server by running:
$ python flask_example.py
And then head to http://127.0.0.1:5000/ in your browser to see the map displayed
"""

from flask import Flask, render_template_string

import folium

app = Flask(__name__)


@app.route("/")
def fullscreen():
"""Simple example of a fullscreen map."""
m = folium.Map()
return m.get_root().render()


@app.route("/iframe")
def iframe():
"""Embed a map as an iframe on a page."""
m = folium.Map()

# set the iframe width and height
m.get_root().width = "800px"
m.get_root().height = "600px"
iframe = m.get_root()._repr_html_()

return render_template_string(
"""
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>Using an iframe</h1>
{{ iframe|safe }}
</body>
</html>
""",
iframe=iframe,
)


@app.route("/components")
def components():
"""Extract map components and put those on a page."""
m = folium.Map(
width=800,
height=600,
)

m.get_root().render()
header = m.get_root().header.render()
body_html = m.get_root().html.render()
script = m.get_root().script.render()

return render_template_string(
"""
<!DOCTYPE html>
<html>
<head>
{{ header|safe }}
</head>
<body>
<h1>Using components</h1>
{{ body_html|safe }}
<script>
{{ script|safe }}
</script>
</body>
</html>
""",
header=header,
body_html=body_html,
script=script,
)


if __name__ == "__main__":
app.run(debug=True)
3 changes: 3 additions & 0 deletions docs/advanced_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ Advanced guide

advanced_guide/flask
advanced_guide/choropleth with Jenks natural breaks optimization
advanced_guide/subplots
advanced_guide/colormaps
advanced_guide/world_copy
advanced_guide/custom_panes
advanced_guide/geodedetic_image_overlay
advanced_guide/custom_tiles
advanced_guide/piechart_icons
advanced_guide/polygons_from_list_of_points
2 changes: 1 addition & 1 deletion docs/advanced_guide/flask.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ or extract the map components and use those.

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

.. literalinclude:: ../examples/flask_example.py
.. literalinclude:: /_static/flask_example.py
107 changes: 107 additions & 0 deletions docs/advanced_guide/piechart_icons.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Piechart icons

In this example we show how you can make mini-charts on several locations.
We'll make little piecharts showing the number of consonants and vowels in
a couple of languages. Those piecharts will be included as icons on the map.

```{code-cell} ipython3
import os
import ast
import pandas
data = pandas.read_csv(
"../data/consonants_vowels.csv",
# To ensure that tuples are read as tuples
converters={"coordinates": ast.literal_eval},
)
data.head()
```

## Pie charts

```{code-cell} ipython3
import io
import matplotlib.pyplot as plt
pie_charts_data = zip(data.consonants, data.vowels)
fig = plt.figure(figsize=(0.5, 0.5))
fig.patch.set_alpha(0)
ax = fig.add_subplot(111)
plots = []
for sizes in pie_charts_data:
ax.pie(sizes, colors=("#e6194b", "#19e6b4"))
buff = io.StringIO()
plt.savefig(buff, format="SVG")
buff.seek(0)
svg = buff.read()
svg = svg.replace("\n", "")
plots.append(svg)
plt.cla()
plt.clf()
plt.close()
```

## Legend

```{code-cell} ipython3
import branca
legend_html = """
{% macro html(this, kwargs) %}
<div style="
position: fixed;
bottom: 50px;
left: 50px;
width: 250px;
height: 80px;
z-index:9999;
font-size:14px;
">
<p><a style="color:#e6194b;font-size:150%;margin-left:20px;">◼</a>&emsp;Consonants</p>
<p><a style="color:#19e6b4;font-size:150%;margin-left:20px;">◼</a>&emsp;Vowels</p>
</div>
<div style="
position: fixed;
bottom: 50px;
left: 50px;
width: 150px;
height: 80px;
z-index:9998;
font-size:14px;
background-color: #ffffff;
filter: blur(8px);
-webkit-filter: blur(8px);
opacity: 0.7;
">
</div>
{% endmacro %}
"""
legend = branca.element.MacroElement()
legend._template = branca.element.Template(legend_html)
```

## Map

```{code-cell} ipython3
import folium
m = folium.Map(location=(0, 0), zoom_start=2)
for i, coord in enumerate(data.coordinates):
marker = folium.Marker(coord)
icon = folium.DivIcon(html=plots[i])
marker.add_child(icon)
popup = folium.Popup(
"Consonants: {}<br>\nVowels: {}".format(data.consonants[i], data.vowels[i])
)
marker.add_child(popup)
m.add_child(marker)
m.get_root().add_child(legend)
m
```
Loading

0 comments on commit 8cf9690

Please sign in to comment.