Skip to content

Instantly share code, notes, and snippets.

@themiurgo
Created May 29, 2015 11:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themiurgo/f136265bdce26fbff704 to your computer and use it in GitHub Desktop.
Save themiurgo/f136265bdce26fbff704 to your computer and use it in GitHub Desktop.
Embed folium maps in iPython
def embed(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('"', '&quot;'),
height=str(height),
width=str(width),
)
except TypeError:
fmap = fmaps
fmap._build_map()
html = template.format(
srcdoc=fmap.HTML.replace('"', '&quot;'),
height=str(height),
width=str(width),
)
return HTML(html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment