forked from python-visualization/folium
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
62 changed files
with
3,753 additions
and
480 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> Consonants</p> | ||
<p><a style="color:#19e6b4;font-size:150%;margin-left:20px;">◼</a> 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 | ||
``` |
Oops, something went wrong.