In [2]:
import folium
In [3]:
from IPython.display import HTML
import folium
def inline_map(map):
"""
Embeds the HTML source of the map directly into the IPython notebook.
This method will not work if the map depends on any files (json data). Also this uses
the HTML5 srcdoc attribute, which may not be supported in all browsers.
"""
map._build_map()
return HTML('<iframe srcdoc="{srcdoc}" style="width: 100%; height: 510px; border: none"></iframe>'.format(srcdoc=map.HTML.replace('"', '"')))
def embed_map(map, path="map.html"):
"""
Embeds a linked iframe to the map into the IPython notebook.
Note: this method will not capture the source of the map into the notebook.
This method should work for all maps (as long as they use relative urls).
"""
map.create_map(path=path)
return HTML('<iframe src="files/{path}" style="width: 100%; height: 510px; border: none"></iframe>'.format(path=path))
In [4]:
def embed_again(fmaps, width='100%', height='510px', *args, **kwargs):
"""
Embeds a folium map in a IPython/Jupyter notebook.
This method will not work if the map depends on any files (json data). Also this uses
the HTML5 srcdoc attribute, which may not be supported in all browsers.
fmaps -- a single folium map or an iterable containing folium maps
"""
from IPython.display import HTML
template = '<iframe srcdoc="{srcdoc}" style="width: {width}; height: {height}; border: none"></iframe>'
html = ''
try:
for fmap in fmaps:
fmap._build_map()
html += template.format(
srcdoc=fmap.HTML.replace('"', '"'),
height=str(height),
width=str(width),
)
except TypeError:
fmap = fmaps
fmap._build_map()
html = template.format(
srcdoc=fmap.HTML.replace('"', '"'),
height=str(height),
width=str(width),
)
return HTML(html)
In [5]:
fmap = folium.Map(location=[37,-122])
embed_map(fmap)
Out[5]:
In [6]:
map_1 = folium.Map(location=[45.372, -121.6972], zoom_start=12,tiles='Stamen Terrain')
map_1.simple_marker([45.3288, -121.6625], popup='Mt. Hood Meadows',marker_icon='cloud')
map_1.simple_marker([45.3311, -121.7113], popup='Timberline Lodge',marker_color='green')
map_1.simple_marker([45.3300, -121.6823], popup='Some Other Location',marker_color='red',marker_icon='info-sign')
In [8]:
map_1.create_map("/tmp/test_icons.html")
In [9]:
embed_map(map_1)
Out[9]:
In [ ]: