In [1]:
import os
import folium

print(folium.__version__)
0.7.0+14.gf4d024d.dirty

How to create Popups

Simple popups

You can define your popup at the feature creation, but you can also overwrite them afterwards:

In [2]:
m = folium.Map([45, 0], zoom_start=4)

folium.Marker([45, -30], popup='inline implicit popup').add_to(m)

folium.CircleMarker(
    location=[45, -10],
    radius=25,
    fill=True,
    popup=folium.Popup('inline explicit Popup')
).add_to(m)

ls = folium.PolyLine(
    locations=[[43, 7], [43, 13], [47, 13], [47, 7], [43, 7]],
    color='red'
)

ls.add_child(folium.Popup('outline Popup on Polyline'))
ls.add_to(m)

gj = folium.GeoJson(
    data={
        'type': 'Polygon',
        'coordinates': [[[27, 43], [33, 43], [33, 47], [27, 47]]]
    }
)

gj.add_child(folium.Popup('outline Popup on GeoJSON'))
gj.add_to(m)

m.save(os.path.join('results', 'simple_popups.html'))

m
Out[2]:
In [3]:
m = folium.Map([45, 0], zoom_start=2)

folium.Marker(
    location=[45, -10],
    popup=folium.Popup("Let's try quotes", parse_html=True, max_width=100)
).add_to(m)

folium.Marker(
    location=[45, -30],
    popup=folium.Popup(u"Ça c'est chouette", parse_html=True, max_width='100%')
).add_to(m)

m
Out[3]:

Vega Popup

You may know that it's possible to create awesome Vega charts with (or without) vincent. If you're willing to put one inside a popup, it's possible thanks to folium.Vega.

In [4]:
import json
import numpy as np
import vincent

scatter_points = {
    'x': np.random.uniform(size=(100,)),
    'y': np.random.uniform(size=(100,)),
}

# Let's create the vincent chart.
scatter_chart = vincent.Scatter(scatter_points,
                                iter_idx='x',
                                width=600,
                                height=300)

# Let's convert it to JSON.
scatter_json = scatter_chart.to_json()

# Let's convert it to dict.
scatter_dict = json.loads(scatter_json)
In [5]:
m = folium.Map([43, -100], zoom_start=4)

popup = folium.Popup()
folium.Vega(scatter_chart, height=350, width=650).add_to(popup)
folium.Marker([30, -120], popup=popup).add_to(m)

# Let's create a Vega popup based on scatter_json.
popup = folium.Popup(max_width=0)
folium.Vega(scatter_json, height=350, width=650).add_to(popup)
folium.Marker([30, -100], popup=popup).add_to(m)

# Let's create a Vega popup based on scatter_dict.
popup = folium.Popup(max_width=650)
folium.Vega(scatter_dict, height=350, width=650).add_to(popup)
folium.Marker([30, -80], popup=popup).add_to(m)

m.save(os.path.join('results', 'vega_popups.html'))

m
Out[5]:

Fancy HTML popup

In [6]:
import branca


m = folium.Map([43, -100], zoom_start=4)

html = """
    <h1> This is a big popup</h1><br>
    With a few lines of code...
    <p>
    <code>
        from numpy import *<br>
        exp(-2*pi)
    </code>
    </p>
    """


folium.Marker([30, -100], popup=html).add_to(m)

m.save(os.path.join('results', 'html_popups.html'))

m
Out[6]:

You can also put any HTML code inside of a Popup, thaks to the IFrame object.

In [7]:
m = folium.Map([43, -100], zoom_start=4)

html = """
    <h1> This popup is an Iframe</h1><br>
    With a few lines of code...
    <p>
    <code>
        from numpy import *<br>
        exp(-2*pi)
    </code>
    </p>
    """

iframe = branca.element.IFrame(html=html, width=500, height=300)
popup = folium.Popup(iframe, max_width=500)

folium.Marker([30, -100], popup=popup).add_to(m)

m.save(os.path.join('results', 'html_popups.html'))

m
Out[7]:
In [8]:
import pandas as pd

df = pd.DataFrame(data=[['apple', 'oranges'], ['other', 'stuff']], columns=['cats', 'dogs'])

m = folium.Map([43, -100], zoom_start=4)

html = df.to_html(classes='table table-striped table-hover table-condensed table-responsive')

popup = folium.Popup(html)

folium.Marker([30, -100], popup=popup).add_to(m)

m.save(os.path.join('results', 'html_popups.html'))

m
Out[8]:

Note that you can put another Figure into an IFrame ; this should let you do stange things...

In [9]:
# Let's create a Figure, with a map inside.
f = branca.element.Figure()
folium.Map([-25, 150], zoom_start=3).add_to(f)

# Let's put the figure into an IFrame.
iframe = branca.element.IFrame(width=500, height=300)
f.add_to(iframe)

# Let's put the IFrame in a Popup
popup = folium.Popup(iframe, max_width=2650)

# Let's create another map.
m = folium.Map([43, -100], zoom_start=4)

# Let's put the Popup on a marker, in the second map.
folium.Marker([30, -100], popup=popup).add_to(m)

# We get a map in a Popup. Not really useful, but powerful.
m.save(os.path.join('results', 'map_popups.html'))

m
Out[9]: