Skip to content

Commit 046976b

Browse files
author
Martin Journois
committed
A Div element for simpler subplots
1 parent f5cc852 commit 046976b

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

folium/features.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,105 @@ def __init__(self, url, download=False):
277277
{% endif %}
278278
""")
279279

280+
class Div(Figure):
281+
def __init__(self, width='100%', height='100%',
282+
left="0%", top="0%", position='relative'):
283+
"""Create a Map with Folium and Leaflet.js
284+
"""
285+
super(Figure, self).__init__()
286+
self._name = 'Div'
287+
288+
# Size Parameters.
289+
self.width = _parse_size(width)
290+
self.height = _parse_size(height)
291+
self.left = _parse_size(left)
292+
self.top = _parse_size(top)
293+
self.position = position
294+
295+
self.header = Element()
296+
self.html = Element("""
297+
{% for name, element in this._children.items() %}
298+
{{element.render(**kwargs)}}
299+
{% endfor %}
300+
""")
301+
self.script = Element()
302+
303+
self.header._parent = self
304+
self.html._parent = self
305+
self.script._parent = self
306+
307+
self._template = Template(u"""
308+
{% macro header(this, kwargs) %}
309+
<style> #{{this.get_name()}} {
310+
position : {{this.position}};
311+
width : {{this.width[0]}}{{this.width[1]}};
312+
height: {{this.height[0]}}{{this.height[1]}};
313+
left: {{this.left[0]}}{{this.left[1]}};
314+
top: {{this.top[0]}}{{this.top[1]}};
315+
</style>
316+
{% endmacro %}
317+
{% macro html(this, kwargs) %}
318+
<div id="{{this.get_name()}}">
319+
{{this.html.render(**kwargs)}}
320+
</div>
321+
{% endmacro %}
322+
""")
323+
324+
def get_root(self):
325+
return self
326+
327+
def render(self, **kwargs):
328+
"""TODO : docstring here."""
329+
figure = self._parent
330+
assert isinstance(figure,Figure), ("You cannot render this Element "
331+
"if it's not in a Figure.")
332+
333+
for name, element in self._children.items():
334+
element.render(**kwargs)
335+
336+
for name, element in self.header._children.items():
337+
figure.header.add_children(element, name=name)
338+
339+
for name, element in self.script._children.items():
340+
figure.script.add_children(element, name=name)
341+
342+
header = self._template.module.__dict__.get('header',None)
343+
if header is not None:
344+
figure.header.add_children(Element(header(self, kwargs)),
345+
name=self.get_name())
346+
347+
html = self._template.module.__dict__.get('html',None)
348+
if html is not None:
349+
figure.html.add_children(Element(html(self, kwargs)),
350+
name=self.get_name())
351+
352+
script = self._template.module.__dict__.get('script',None)
353+
if script is not None:
354+
figure.script.add_children(Element(script(self, kwargs)),
355+
name=self.get_name())
356+
357+
def _repr_html_(self, figsize=(17,10), **kwargs):
358+
"""Displays the Map in a Jupyter notebook.
359+
360+
Parameters
361+
----------
362+
self : folium.Map object
363+
The map you want to display
364+
365+
figsize : tuple of length 2, default (17,10)
366+
The size of the output you expect in inches.
367+
Output is 60dpi so that the output has same size as a
368+
matplotlib figure with the same figsize.
369+
370+
"""
371+
if self._parent is None:
372+
self.add_to(Figure())
373+
out = self._parent._repr_html_(figsize=figsize, **kwargs)
374+
self._parent = None
375+
else:
376+
out = self._parent._repr_html_(figsize=figsize, **kwargs)
377+
return out
378+
280379
class MacroElement(Element):
281380
"""This is a parent class for Elements defined by a macro template.
282381
To compute your own element, all you have to do is:

0 commit comments

Comments
 (0)