Coverage for src/pyensae/notebookhelper/folium_helper.py: 89%
27 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-03 02:16 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-03 02:16 +0200
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Module *folium* does not have any output to a notebook, addresses that issue. The module
5does not explicitely import *folium*.
6"""
8from IPython.display import HTML
11def folium_html_map(mapf, width=None, height=None, asobj=True):
12 """
13 Embeds the HTML source of the map directly into the IPython notebook.
15 @param mapf folium map
16 @param width width
17 @param height height
18 @param asobj return an object which implements ``_repr_html_``
19 @return HTML (IPython)
21 This method will not work if the map depends on any files (json data). Also this uses
22 the HTML5 srcdoc attribute, which may not be supported in all browsers.
24 Source: `folium_base.py <https://gist.github.com/psychemedia/f7385255f89137c503b5>`_
26 .. exref::
27 :title: Display an inline map with folium in a notebook
29 ::
31 import folium
32 map_osm = folium.Map(location=[48.85, 2.34])
33 from pyensae.notebook_helper import folium_html_map
34 map_osm.polygon_marker(location=[48.824338, 2.302641], popup='ENSAE',
35 fill_color='#132b5e', num_sides=3, radius=10)
36 folium_html_map(map_osm)
38 With folium version 0.2, this becomes easier:
40 ::
42 import folium
43 map_osm = folium.Map(location=[48.85, 2.34])
44 from pyensae.notebook_helper import folium_html_map
45 map_osm.polygon_marker(location=[48.824338, 2.302641], popup='ENSAE',
46 fill_color='#132b5e', num_sides=3, radius=10)
47 map_osm
49 .. versionchanged:: 1.1
50 Add parameters *width* and *height* to change the size of the map within a notebook.
51 Hopefully, they will be added in folium.
52 """
53 res = mapf._repr_html_()
54 if width or height:
55 look = '<div style="width:100%;">'
56 if not res.startswith(look):
57 raise ValueError(
58 "Folium has changed its HTML form, it used to start with: '{0}'.\n{1}".format(look, res))
59 size = ""
60 if width:
61 size += "width:" + width + ";"
62 if height:
63 size += "height:" + height + ";"
64 newlook = '<div style="{size}">'.format(size=size)
65 res = newlook + res[len(look):]
66 if asobj:
67 class CustomFoliumMap:
69 def __init__(self, res, map):
70 self.res = res
71 self.map = map
73 def _repr_html_(self):
74 return self.res
76 return CustomFoliumMap(res, map)
77 else:
78 return res
81def folium_embed_map(mapf, path="map.html", width="100%", height="510px"):
82 """
83 @param mapf folium map
84 @param path where to store the temporary map
85 @return HTML (IPython)
87 Embeds a linked iframe to the map into the IPython notebook.
89 Note: this method will not capture the source of the map into the notebook.
90 This method should work for all maps (as long as they use relative urls).
92 Source: `folium_base.py <https://gist.github.com/psychemedia/f7385255f89137c503b5>`_
93 """
94 mapf.save(path)
95 return HTML('<iframe src="files/{path}" style="width: {width}; height: {height}; border: none">' +
96 '</iframe>'.format(path=path, width=width, height=height))