@@ -175,7 +175,23 @@ def __init__(self, url, download=False):
175
175
]
176
176
177
177
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
+ """
179
195
super (Figure , self ).__init__ ()
180
196
self ._name = 'Figure'
181
197
self .header = Element ()
@@ -186,7 +202,12 @@ def __init__(self, figsize=(17,10)):
186
202
self .html ._parent = self
187
203
self .script ._parent = self
188
204
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'
190
211
191
212
self ._template = Template (u"""
192
213
<!DOCTYPE html>
@@ -252,25 +273,27 @@ def _repr_html_(self, **kwargs):
252
273
253
274
Parameters
254
275
----------
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
-
263
276
"""
264
277
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>' \
269
293
.format (\
270
- html = "data:text/html;base64," + base64 .b64encode (html .encode ('utf8' )).decode ('utf8' ),
271
- #html = self.HTML.replace('"','"'),
272
- width = int (60. * width ),
273
- height = int (60. * height ),
294
+ html = html ,
295
+ width = self .width ,
296
+ height = self .height ,
274
297
)
275
298
return iframe
276
299
@@ -387,26 +410,15 @@ def render(self, **kwargs):
387
410
figure .script .add_children (Element (script (self , kwargs )),
388
411
name = self .get_name ())
389
412
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.
403
415
"""
404
416
if self ._parent is None :
405
417
self .add_to (Figure ())
406
- out = self ._parent ._repr_html_ (figsize = figsize , ** kwargs )
418
+ out = self ._parent ._repr_html_ (** kwargs )
407
419
self ._parent = None
408
420
else :
409
- out = self ._parent ._repr_html_ (figsize = figsize , ** kwargs )
421
+ out = self ._parent ._repr_html_ (** kwargs )
410
422
return out
411
423
412
424
class MacroElement (Element ):
0 commit comments