Skip to content

Commit 57f2e37

Browse files
committed
Merge pull request #221 from BibMartin/issue219
Fix issue #219
2 parents 4bc20ae + dba5a5e commit 57f2e37

File tree

3 files changed

+54
-49
lines changed

3 files changed

+54
-49
lines changed

folium/element.py

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,23 @@ def __init__(self, url, download=False):
175175
]
176176

177177
class Figure(Element):
178-
def __init__(self, figsize=(17,10)):
178+
def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
179+
"""Create a Figure object, to plot things into it.
180+
181+
Parameters
182+
----------
183+
width : str, default "100%"
184+
The width of the Figure. It may be a percentage or a distance (like "300px").
185+
height : str, default None
186+
The height of the Figure. It may be a percentage or a distance (like "300px").
187+
ratio : str, default "60%"
188+
A percentage defining the aspect ratio of the Figure.
189+
It will be ignored if height is not None.
190+
figsize : tuple of two int, default None
191+
If you're a matplotlib addict, you can overwrite width and height.
192+
Values will be converted into pixels in using 60 dpi.
193+
For example figsize=(10,5) wil result in width="600px", height="300px".
194+
"""
179195
super(Figure, self).__init__()
180196
self._name = 'Figure'
181197
self.header = Element()
@@ -186,7 +202,12 @@ def __init__(self, figsize=(17,10)):
186202
self.html._parent = self
187203
self.script._parent = self
188204

189-
self.figsize = figsize
205+
self.width = width
206+
self.height = height
207+
self.ratio = ratio
208+
if figsize is not None:
209+
self.width = str(60*figsize[0])+'px'
210+
self.height = str(60*figsize[1])+'px'
190211

191212
self._template = Template(u"""
192213
<!DOCTYPE html>
@@ -252,25 +273,27 @@ def _repr_html_(self, **kwargs):
252273
253274
Parameters
254275
----------
255-
self : folium.Map object
256-
The map you want to display
257-
258-
figsize : tuple of length 2, default (17,10)
259-
The size of the output you expect in inches.
260-
Output is 60dpi so that the output has same size as a
261-
matplotlib figure with the same figsize.
262-
263276
"""
264277
html = self.render(**kwargs)
265-
266-
width, height = self.figsize
267-
268-
iframe = '<iframe src="{html}" width="{width}px" height="{height}px"></iframe>'\
278+
html = "data:text/html;base64,"+base64.b64encode(html.encode('utf8')).decode('utf8')
279+
280+
if self.height is None:
281+
iframe = """
282+
<div style="width:{width};">
283+
<div style="position:relative;width:100%;height:0;padding-bottom:{ratio};">
284+
<iframe src="{html}" style="position:absolute;width:100%;height:100%;left:0;top:0;">
285+
</iframe>
286+
</div></div>""".format(
287+
html=html,
288+
width=self.width,
289+
ratio=self.ratio,
290+
)
291+
else:
292+
iframe = '<iframe src="{html}" width="{width}" height="{height}"></iframe>'\
269293
.format(\
270-
html = "data:text/html;base64,"+base64.b64encode(html.encode('utf8')).decode('utf8'),
271-
#html = self.HTML.replace('"','&quot;'),
272-
width = int(60.*width),
273-
height= int(60.*height),
294+
html = html,
295+
width = self.width,
296+
height= self.height,
274297
)
275298
return iframe
276299

@@ -387,26 +410,15 @@ def render(self, **kwargs):
387410
figure.script.add_children(Element(script(self, kwargs)),
388411
name=self.get_name())
389412

390-
def _repr_html_(self, figsize=(17,10), **kwargs):
391-
"""Displays the Map in a Jupyter notebook.
392-
393-
Parameters
394-
----------
395-
self : folium.Map object
396-
The map you want to display
397-
398-
figsize : tuple of length 2, default (17,10)
399-
The size of the output you expect in inches.
400-
Output is 60dpi so that the output has same size as a
401-
matplotlib figure with the same figsize.
402-
413+
def _repr_html_(self, **kwargs):
414+
"""Displays the Div in a Jupyter notebook.
403415
"""
404416
if self._parent is None:
405417
self.add_to(Figure())
406-
out = self._parent._repr_html_(figsize=figsize, **kwargs)
418+
out = self._parent._repr_html_(**kwargs)
407419
self._parent = None
408420
else:
409-
out = self._parent._repr_html_(figsize=figsize, **kwargs)
421+
out = self._parent._repr_html_(**kwargs)
410422
return out
411423

412424
class MacroElement(Element):

folium/map.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,26 +134,15 @@ def __init__(self, location=None, width='100%', height='100%',
134134
{% endmacro %}
135135
""")
136136

137-
def _repr_html_(self, figsize=(17,10), **kwargs):
137+
def _repr_html_(self, **kwargs):
138138
"""Displays the Map in a Jupyter notebook.
139-
140-
Parameters
141-
----------
142-
self : folium.Map object
143-
The map you want to display
144-
145-
figsize : tuple of length 2, default (17,10)
146-
The size of the output you expect in inches.
147-
Output is 60dpi so that the output has same size as a
148-
matplotlib figure with the same figsize.
149-
150139
"""
151140
if self._parent is None:
152141
self.add_to(Figure())
153-
out = self._parent._repr_html_(figsize=figsize, **kwargs)
142+
out = self._parent._repr_html_(**kwargs)
154143
self._parent = None
155144
else:
156-
out = self._parent._repr_html_(figsize=figsize, **kwargs)
145+
out = self._parent._repr_html_(**kwargs)
157146
return out
158147

159148
def add_tile_layer(self, tiles='OpenStreetMap', name=None,

folium/six.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
import urllib
32

43
PY3 = sys.version_info[0] == 3
54

@@ -17,4 +16,9 @@ def iteritems(d, **kw):
1716
def iteritems(d, **kw):
1817
return iter(d.iteritems(**kw))
1918

20-
urlopen = urllib.request.urlopen if PY3 else urllib.urlopen
19+
if PY3:
20+
import urllib.request
21+
urlopen = urllib.request.urlopen
22+
else:
23+
import urllib
24+
urlopen = urllib.urlopen

0 commit comments

Comments
 (0)