Created
May 29, 2015 11:47
-
-
Save themiurgo/f136265bdce26fbff704 to your computer and use it in GitHub Desktop.
Embed folium maps in iPython
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
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('"', '"'), | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment