#!/usr/bin/env python # coding: utf-8 # In[1]: import os import folium print(folium.__version__) # # 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, 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 # ## 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[3]: 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[4]: m = folium.Map([43, -100], zoom_start=4) # Let's create a Vega popup based on scatter_chart. popup = folium.Popup(max_width=800) 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=800) 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=800) 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 # ## Fancy HTML popup # Now, you can put any HTML code inside of a Popup, thaks to the `IFrame` object. # In[5]: import branca m = folium.Map([43, -100], zoom_start=4) html = """
from numpy import *
exp(-2*pi)