88from uuid import uuid4
99
1010from jinja2 import Environment , PackageLoader , Template
11- ENV = Environment (loader = PackageLoader ('folium' , 'templates' ))
1211from collections import OrderedDict
1312import json
1413import base64
1514
1615from .six import urlopen , text_type , binary_type
1716from .utilities import _camelify , _parse_size
1817
18+
19+ ENV = Environment (loader = PackageLoader ('folium' , 'templates' ))
20+
21+
1922class Element (object ):
2023 """Basic Element object that does nothing.
2124 Other Elements may inherit from this one."""
@@ -35,7 +38,7 @@ def __init__(self, template=None, template_name=None):
3538 """ )
3639
3740 def get_name (self ):
38- return _camelify (self ._name ) + '_' + self ._id
41+ return _camelify (self ._name ) + '_' + self ._id
3942
4043 def add_children (self , child , name = None , index = None ):
4144 """Add a children."""
@@ -44,8 +47,9 @@ def add_children(self, child, name=None, index=None):
4447 if index is None :
4548 self ._children [name ] = child
4649 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 ))
4953 self ._children = items
5054 child ._parent = self
5155
@@ -62,8 +66,8 @@ def to_dict(self, depth=-1, ordered=True, **kwargs):
6266 out ['name' ] = self ._name
6367 out ['id' ] = self ._id
6468 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 ()])
6771 return out
6872
6973 def to_json (self , depth = - 1 , ** kwargs ):
@@ -86,11 +90,11 @@ def save(self, outfile, close_file=True, **kwargs):
8690 Parameters
8791 ----------
8892 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.
9094 close_file : bool, default True
9195 Whether the file has to be closed after write.
9296 """
93- if isinstance (outfile ,text_type ) or isinstance (outfile ,binary_type ):
97+ if isinstance (outfile , text_type ) or isinstance (outfile , binary_type ):
9498 fid = open (outfile , 'wb' )
9599 else :
96100 fid = outfile
@@ -101,16 +105,19 @@ def save(self, outfile, close_file=True, **kwargs):
101105 if close_file :
102106 fid .close ()
103107
108+
104109class Link (Element ):
105110 def get_code (self ):
106111 if self .code is None :
107112 self .code = urlopen (self .url ).read ()
108113 return self .code
114+
109115 def to_dict (self , depth = - 1 , ** kwargs ):
110116 out = super (Link , self ).to_dict (depth = - 1 , ** kwargs )
111117 out ['url' ] = self .url
112118 return out
113119
120+
114121class JavascriptLink (Link ):
115122 def __init__ (self , url , download = False ):
116123 """Create a JavascriptLink object based on a url.
@@ -136,6 +143,7 @@ def __init__(self, url, download=False):
136143 {% endif %}
137144 """ )
138145
146+
139147class CssLink (Link ):
140148 def __init__ (self , url , download = False ):
141149 """Create a CssLink object based on a url.
@@ -195,6 +203,7 @@ def __init__(self, url, download=False):
195203 "https://raw.githubusercontent.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css" ),
196204 ]
197205
206+
198207class Figure (Element ):
199208 def __init__ (self , width = "100%" , height = None , ratio = "60%" , figsize = None ):
200209 """Create a Figure object, to plot things into it.
@@ -216,7 +225,7 @@ def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
216225 super (Figure , self ).__init__ ()
217226 self ._name = 'Figure'
218227 self .header = Element ()
219- self .html = Element ()
228+ self .html = Element ()
220229 self .script = Element ()
221230
222231 self .header ._parent = self
@@ -246,7 +255,7 @@ def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
246255 # Create the meta tag
247256 self .header .add_children (Element (
248257 '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />' ),
249- name = 'meta_http' )
258+ name = 'meta_http' )
250259
251260 # Import Javascripts
252261 for name , url in _default_js :
@@ -296,7 +305,7 @@ def _repr_html_(self, **kwargs):
296305 ----------
297306 """
298307 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' )
300309
301310 if self .height is None :
302311 iframe = """
@@ -310,22 +319,19 @@ def _repr_html_(self, **kwargs):
310319 ratio = self .ratio ,
311320 )
312321 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 )
319325 return iframe
320326
321- def add_subplot (self , x ,y , n , margin = 0.05 ):
327+ def add_subplot (self , x , y , n , margin = 0.05 ):
322328 width = 1. / y
323329 height = 1. / x
324- left = ((n - 1 )% y )* width
330+ left = ((n - 1 ) % y )* width
325331 top = ((n - 1 )// y )* height
326332
327333 left = left + width * margin
328- top = top + height * margin
334+ top = top + height * margin
329335 width = width * (1 - 2. * margin )
330336 height = height * (1 - 2. * margin )
331337
@@ -338,39 +344,40 @@ def add_subplot(self, x,y,n,margin=0.05):
338344 self .add_children (div )
339345 return div
340346
347+
341348class Html (Element ):
342349 def __init__ (self , data , width = "100%" , height = "100%" ):
343350 """TODO : docstring here"""
344351 super (Html , self ).__init__ ()
345352 self ._name = 'Html'
346353 self .data = data
347354
348- self .width = _parse_size (width )
349- self .height = _parse_size (height )
355+ self .width = _parse_size (width )
356+ self .height = _parse_size (height )
350357
351358 self ._template = Template (u"""
352359 <div id="{{this.get_name()}}"
353360 style="width: {{this.width[0]}}{{this.width[1]}}; height: {{this.height[0]}}{{this.height[1]}};">
354361 {{this.data}}</div>
355362 """ )
356363
364+
357365class Div (Figure ):
358366 def __init__ (self , width = '100%' , height = '100%' ,
359367 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."""
362369 super (Figure , self ).__init__ ()
363370 self ._name = 'Div'
364371
365372 # Size Parameters.
366- self .width = _parse_size (width )
373+ self .width = _parse_size (width )
367374 self .height = _parse_size (height )
368375 self .left = _parse_size (left )
369- self .top = _parse_size (top )
376+ self .top = _parse_size (top )
370377 self .position = position
371378
372379 self .header = Element ()
373- self .html = Element ("""
380+ self .html = Element ("""
374381 {% for name, element in this._children.items() %}
375382 {{element.render(**kwargs)}}
376383 {% endfor %}
@@ -404,8 +411,8 @@ def get_root(self):
404411 def render (self , ** kwargs ):
405412 """TODO : docstring here."""
406413 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." )
409416
410417 for name , element in self ._children .items ():
411418 element .render (** kwargs )
@@ -416,24 +423,23 @@ def render(self, **kwargs):
416423 for name , element in self .script ._children .items ():
417424 figure .script .add_children (element , name = name )
418425
419- header = self ._template .module .__dict__ .get ('header' ,None )
426+ header = self ._template .module .__dict__ .get ('header' , None )
420427 if header is not None :
421428 figure .header .add_children (Element (header (self , kwargs )),
422429 name = self .get_name ())
423430
424- html = self ._template .module .__dict__ .get ('html' ,None )
431+ html = self ._template .module .__dict__ .get ('html' , None )
425432 if html is not None :
426433 figure .html .add_children (Element (html (self , kwargs )),
427- name = self .get_name ())
434+ name = self .get_name ())
428435
429- script = self ._template .module .__dict__ .get ('script' ,None )
436+ script = self ._template .module .__dict__ .get ('script' , None )
430437 if script is not None :
431438 figure .script .add_children (Element (script (self , kwargs )),
432439 name = self .get_name ())
433440
434441 def _repr_html_ (self , ** kwargs ):
435- """Displays the Div in a Jupyter notebook.
436- """
442+ """Displays the Div in a Jupyter notebook."""
437443 if self ._parent is None :
438444 self .add_to (Figure ())
439445 out = self ._parent ._repr_html_ (** kwargs )
@@ -442,6 +448,7 @@ def _repr_html_(self, **kwargs):
442448 out = self ._parent ._repr_html_ (** kwargs )
443449 return out
444450
451+
445452class MacroElement (Element ):
446453 """This is a parent class for Elements defined by a macro template.
447454 To compute your own element, all you have to do is:
@@ -469,20 +476,20 @@ def __init__(self):
469476
470477 def render (self , ** kwargs ):
471478 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." )
474481
475- header = self ._template .module .__dict__ .get ('header' ,None )
482+ header = self ._template .module .__dict__ .get ('header' , None )
476483 if header is not None :
477484 figure .header .add_children (Element (header (self , kwargs )),
478485 name = self .get_name ())
479486
480- html = self ._template .module .__dict__ .get ('html' ,None )
487+ html = self ._template .module .__dict__ .get ('html' , None )
481488 if html is not None :
482489 figure .html .add_children (Element (html (self , kwargs )),
483- name = self .get_name ())
490+ name = self .get_name ())
484491
485- script = self ._template .module .__dict__ .get ('script' ,None )
492+ script = self ._template .module .__dict__ .get ('script' , None )
486493 if script is not None :
487494 figure .script .add_children (Element (script (self , kwargs )),
488495 name = self .get_name ())
0 commit comments