8
8
from uuid import uuid4
9
9
10
10
from jinja2 import Environment , PackageLoader , Template
11
- ENV = Environment (loader = PackageLoader ('folium' , 'templates' ))
12
11
from collections import OrderedDict
13
12
import json
14
13
import base64
15
14
16
15
from .six import urlopen , text_type , binary_type
17
16
from .utilities import _camelify , _parse_size
18
17
18
+
19
+ ENV = Environment (loader = PackageLoader ('folium' , 'templates' ))
20
+
21
+
19
22
class Element (object ):
20
23
"""Basic Element object that does nothing.
21
24
Other Elements may inherit from this one."""
@@ -35,7 +38,7 @@ def __init__(self, template=None, template_name=None):
35
38
""" )
36
39
37
40
def get_name (self ):
38
- return _camelify (self ._name ) + '_' + self ._id
41
+ return _camelify (self ._name ) + '_' + self ._id
39
42
40
43
def add_children (self , child , name = None , index = None ):
41
44
"""Add a children."""
@@ -44,8 +47,9 @@ def add_children(self, child, name=None, index=None):
44
47
if index is None :
45
48
self ._children [name ] = child
46
49
else :
47
- items = [item for item in self ._children .items () if item [0 ] != name ]
48
- items .insert (int (index ),(name ,child ))
50
+ items = [item for item in self ._children .items ()
51
+ if item [0 ] != name ]
52
+ items .insert (int (index ), (name , child ))
49
53
self ._children = items
50
54
child ._parent = self
51
55
@@ -62,8 +66,8 @@ def to_dict(self, depth=-1, ordered=True, **kwargs):
62
66
out ['name' ] = self ._name
63
67
out ['id' ] = self ._id
64
68
if depth != 0 :
65
- out ['children' ] = dict_fun ([(name , child .to_dict (depth = depth - 1 ))\
66
- for name ,child in self ._children .items ()])
69
+ out ['children' ] = dict_fun ([(name , child .to_dict (depth = depth - 1 ))
70
+ for name , child in self ._children .items ()])
67
71
return out
68
72
69
73
def to_json (self , depth = - 1 , ** kwargs ):
@@ -86,11 +90,11 @@ def save(self, outfile, close_file=True, **kwargs):
86
90
Parameters
87
91
----------
88
92
outfile : str or file object
89
- The file (or filename) where you want to ouput the html.
93
+ The file (or filename) where you want to output the html.
90
94
close_file : bool, default True
91
95
Whether the file has to be closed after write.
92
96
"""
93
- if isinstance (outfile ,text_type ) or isinstance (outfile ,binary_type ):
97
+ if isinstance (outfile , text_type ) or isinstance (outfile , binary_type ):
94
98
fid = open (outfile , 'wb' )
95
99
else :
96
100
fid = outfile
@@ -101,16 +105,19 @@ def save(self, outfile, close_file=True, **kwargs):
101
105
if close_file :
102
106
fid .close ()
103
107
108
+
104
109
class Link (Element ):
105
110
def get_code (self ):
106
111
if self .code is None :
107
112
self .code = urlopen (self .url ).read ()
108
113
return self .code
114
+
109
115
def to_dict (self , depth = - 1 , ** kwargs ):
110
116
out = super (Link , self ).to_dict (depth = - 1 , ** kwargs )
111
117
out ['url' ] = self .url
112
118
return out
113
119
120
+
114
121
class JavascriptLink (Link ):
115
122
def __init__ (self , url , download = False ):
116
123
"""Create a JavascriptLink object based on a url.
@@ -136,6 +143,7 @@ def __init__(self, url, download=False):
136
143
{% endif %}
137
144
""" )
138
145
146
+
139
147
class CssLink (Link ):
140
148
def __init__ (self , url , download = False ):
141
149
"""Create a CssLink object based on a url.
@@ -195,6 +203,7 @@ def __init__(self, url, download=False):
195
203
"https://raw.githubusercontent.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css" ),
196
204
]
197
205
206
+
198
207
class Figure (Element ):
199
208
def __init__ (self , width = "100%" , height = None , ratio = "60%" , figsize = None ):
200
209
"""Create a Figure object, to plot things into it.
@@ -216,7 +225,7 @@ def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
216
225
super (Figure , self ).__init__ ()
217
226
self ._name = 'Figure'
218
227
self .header = Element ()
219
- self .html = Element ()
228
+ self .html = Element ()
220
229
self .script = Element ()
221
230
222
231
self .header ._parent = self
@@ -246,7 +255,7 @@ def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
246
255
# Create the meta tag
247
256
self .header .add_children (Element (
248
257
'<meta http-equiv="content-type" content="text/html; charset=UTF-8" />' ),
249
- name = 'meta_http' )
258
+ name = 'meta_http' )
250
259
251
260
# Import Javascripts
252
261
for name , url in _default_js :
@@ -296,7 +305,7 @@ def _repr_html_(self, **kwargs):
296
305
----------
297
306
"""
298
307
html = self .render (** kwargs )
299
- html = "data:text/html;base64," + base64 .b64encode (html .encode ('utf8' )).decode ('utf8' )
308
+ html = "data:text/html;base64," + base64 .b64encode (html .encode ('utf8' )).decode ('utf8' )
300
309
301
310
if self .height is None :
302
311
iframe = """
@@ -310,22 +319,19 @@ def _repr_html_(self, **kwargs):
310
319
ratio = self .ratio ,
311
320
)
312
321
else :
313
- iframe = '<iframe src="{html}" width="{width}" height="{height}"></iframe>' \
314
- .format (\
315
- html = html ,
316
- width = self .width ,
317
- height = self .height ,
318
- )
322
+ iframe = ('<iframe src="{html}" width="{width}" '
323
+ 'height="{height}"></iframe>' ).format
324
+ iframe = iframe (html = html , width = self .width , height = self .height )
319
325
return iframe
320
326
321
- def add_subplot (self , x ,y , n , margin = 0.05 ):
327
+ def add_subplot (self , x , y , n , margin = 0.05 ):
322
328
width = 1. / y
323
329
height = 1. / x
324
- left = ((n - 1 )% y )* width
330
+ left = ((n - 1 ) % y )* width
325
331
top = ((n - 1 )// y )* height
326
332
327
333
left = left + width * margin
328
- top = top + height * margin
334
+ top = top + height * margin
329
335
width = width * (1 - 2. * margin )
330
336
height = height * (1 - 2. * margin )
331
337
@@ -338,39 +344,40 @@ def add_subplot(self, x,y,n,margin=0.05):
338
344
self .add_children (div )
339
345
return div
340
346
347
+
341
348
class Html (Element ):
342
349
def __init__ (self , data , width = "100%" , height = "100%" ):
343
350
"""TODO : docstring here"""
344
351
super (Html , self ).__init__ ()
345
352
self ._name = 'Html'
346
353
self .data = data
347
354
348
- self .width = _parse_size (width )
349
- self .height = _parse_size (height )
355
+ self .width = _parse_size (width )
356
+ self .height = _parse_size (height )
350
357
351
358
self ._template = Template (u"""
352
359
<div id="{{this.get_name()}}"
353
360
style="width: {{this.width[0]}}{{this.width[1]}}; height: {{this.height[0]}}{{this.height[1]}};">
354
361
{{this.data}}</div>
355
362
""" )
356
363
364
+
357
365
class Div (Figure ):
358
366
def __init__ (self , width = '100%' , height = '100%' ,
359
367
left = "0%" , top = "0%" , position = 'relative' ):
360
- """Create a Map with Folium and Leaflet.js
361
- """
368
+ """Create a Map with Folium and Leaflet.js."""
362
369
super (Figure , self ).__init__ ()
363
370
self ._name = 'Div'
364
371
365
372
# Size Parameters.
366
- self .width = _parse_size (width )
373
+ self .width = _parse_size (width )
367
374
self .height = _parse_size (height )
368
375
self .left = _parse_size (left )
369
- self .top = _parse_size (top )
376
+ self .top = _parse_size (top )
370
377
self .position = position
371
378
372
379
self .header = Element ()
373
- self .html = Element ("""
380
+ self .html = Element ("""
374
381
{% for name, element in this._children.items() %}
375
382
{{element.render(**kwargs)}}
376
383
{% endfor %}
@@ -404,8 +411,8 @@ def get_root(self):
404
411
def render (self , ** kwargs ):
405
412
"""TODO : docstring here."""
406
413
figure = self ._parent
407
- assert isinstance (figure ,Figure ), ("You cannot render this Element "
408
- "if it's not in a Figure." )
414
+ assert isinstance (figure , Figure ), ("You cannot render this Element "
415
+ "if it's not in a Figure." )
409
416
410
417
for name , element in self ._children .items ():
411
418
element .render (** kwargs )
@@ -416,24 +423,23 @@ def render(self, **kwargs):
416
423
for name , element in self .script ._children .items ():
417
424
figure .script .add_children (element , name = name )
418
425
419
- header = self ._template .module .__dict__ .get ('header' ,None )
426
+ header = self ._template .module .__dict__ .get ('header' , None )
420
427
if header is not None :
421
428
figure .header .add_children (Element (header (self , kwargs )),
422
429
name = self .get_name ())
423
430
424
- html = self ._template .module .__dict__ .get ('html' ,None )
431
+ html = self ._template .module .__dict__ .get ('html' , None )
425
432
if html is not None :
426
433
figure .html .add_children (Element (html (self , kwargs )),
427
- name = self .get_name ())
434
+ name = self .get_name ())
428
435
429
- script = self ._template .module .__dict__ .get ('script' ,None )
436
+ script = self ._template .module .__dict__ .get ('script' , None )
430
437
if script is not None :
431
438
figure .script .add_children (Element (script (self , kwargs )),
432
439
name = self .get_name ())
433
440
434
441
def _repr_html_ (self , ** kwargs ):
435
- """Displays the Div in a Jupyter notebook.
436
- """
442
+ """Displays the Div in a Jupyter notebook."""
437
443
if self ._parent is None :
438
444
self .add_to (Figure ())
439
445
out = self ._parent ._repr_html_ (** kwargs )
@@ -442,6 +448,7 @@ def _repr_html_(self, **kwargs):
442
448
out = self ._parent ._repr_html_ (** kwargs )
443
449
return out
444
450
451
+
445
452
class MacroElement (Element ):
446
453
"""This is a parent class for Elements defined by a macro template.
447
454
To compute your own element, all you have to do is:
@@ -469,20 +476,20 @@ def __init__(self):
469
476
470
477
def render (self , ** kwargs ):
471
478
figure = self .get_root ()
472
- assert isinstance (figure ,Figure ), ("You cannot render this Element "
473
- "if it's not in a Figure." )
479
+ assert isinstance (figure , Figure ), ("You cannot render this Element "
480
+ "if it's not in a Figure." )
474
481
475
- header = self ._template .module .__dict__ .get ('header' ,None )
482
+ header = self ._template .module .__dict__ .get ('header' , None )
476
483
if header is not None :
477
484
figure .header .add_children (Element (header (self , kwargs )),
478
485
name = self .get_name ())
479
486
480
- html = self ._template .module .__dict__ .get ('html' ,None )
487
+ html = self ._template .module .__dict__ .get ('html' , None )
481
488
if html is not None :
482
489
figure .html .add_children (Element (html (self , kwargs )),
483
- name = self .get_name ())
490
+ name = self .get_name ())
484
491
485
- script = self ._template .module .__dict__ .get ('script' ,None )
492
+ script = self ._template .module .__dict__ .get ('script' , None )
486
493
if script is not None :
487
494
figure .script .add_children (Element (script (self , kwargs )),
488
495
name = self .get_name ())
0 commit comments