Skip to content
Permalink
Browse files

add function to display folium maps in a notebooks

  • Loading branch information...
sdpython committed Aug 24, 2015
1 parent bfff8d3 commit d7757988ae57a0e4e0efcf02720206961e597539
@@ -0,0 +1,60 @@
"""
@brief test log(time=7s)
"""

import sys
import os
import unittest
import re

try:
import src
import pyquickhelper
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import src
import pyquickhelper


from pyquickhelper import get_temp_folder, fLOG
from src.pyensae.notebook_helper import folium_inline_map


class TestNotebookFolium (unittest.TestCase):

def test_notebook_folium(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

temp = get_temp_folder(__file__, "temp_folium")
outfile = os.path.join(temp, 'osm.html')

import folium
map_osm = folium.Map(location=[48.85, 2.34])
map_osm.create_map(path=outfile)
assert os.path.exists(outfile)
folium_inline_map(map_osm)


if __name__ == "__main__":
unittest.main()
@@ -19,6 +19,7 @@ cloud-sptheme
coverage
dateutils
docutils
folium
hachibee-sphinx-theme
ipykernel
ipyparallel
@@ -0,0 +1,6 @@
"""
@file
@brief Shortcut to notebook_helper
"""

from .folium_helper import folium_inline_map, folium_embed_map
@@ -0,0 +1,52 @@
#-*- coding: utf-8 -*-
"""
@file
@brief Module *folium* does not have any output to a notebook, addresses that issue
"""

from IPython.display import HTML
import folium


def folium_inline_map(map):
"""
Embeds the HTML source of the map directly into the IPython notebook.
@param map folium map
@return HTML (IPython)
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.
Source: `folium_base.py <https://gist.github.com/psychemedia/f7385255f89137c503b5>`_
@exemple(Display an inline map with folium in a notebook)
@code
import folium
map_osm = folium.Map(location=[48.85, 2.34])
from pyensae.notebook_helper import folium_inline_map
map_osm.polygon_marker(location=[48.824338, 2.302641], popup='ENSAE',
fill_color='#132b5e', num_sides=3, radius=10)
folium_inline_map(map_osm)
@endcode
@endexample
"""
map._build_map()
return HTML('<iframe srcdoc="{srcdoc}" style="width: 100%; height: 510px; border: none"></iframe>'.format(srcdoc=map.HTML.replace('"', '&quot;')))


def folium_embed_map(map, path="map.html"):
"""
@param map folium map
@param path where to store the temporary map
@return HTML (IPython)
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).
Source: `folium_base.py <https://gist.github.com/psychemedia/f7385255f89137c503b5>`_
"""
map.create_map(path=path)
return HTML('<iframe src="files/{path}" style="width: 100%; height: 510px; border: none"></iframe>'.format(path=path))

0 comments on commit d775798

Please sign in to comment.
You can’t perform that action at this time.