1414from branca .element import (Element , Figure , JavascriptLink , MacroElement )
1515from branca .utilities import color_brewer
1616
17+ from folium .elements import JSCSSMixin
1718from folium .folium import Map
1819from folium .map import (FeatureGroup , Icon , Layer , Marker , Tooltip )
1920from folium .utilities import (
3637import requests
3738
3839
39- class RegularPolygonMarker (Marker ):
40+ class RegularPolygonMarker (JSCSSMixin , Marker ):
4041 """
4142 Custom markers using the Leaflet Data Vis Framework.
4243
@@ -69,6 +70,11 @@ class RegularPolygonMarker(Marker):
6970 {% endmacro %}
7071 """ )
7172
73+ default_js = [
74+ ('dvf_js' ,
75+ 'https://cdnjs.cloudflare.com/ajax/libs/leaflet-dvf/0.3.0/leaflet-dvf.markers.min.js' ),
76+ ]
77+
7278 def __init__ (self , location , number_of_sides = 4 , rotation = 0 , radius = 15 ,
7379 popup = None , tooltip = None , ** kwargs ):
7480 super (RegularPolygonMarker , self ).__init__ (
@@ -83,21 +89,8 @@ def __init__(self, location, number_of_sides=4, rotation=0, radius=15,
8389 radius = radius ,
8490 ))
8591
86- def render (self , ** kwargs ):
87- """Renders the HTML representation of the element."""
88- super (RegularPolygonMarker , self ).render ()
89-
90- figure = self .get_root ()
91- assert isinstance (figure , Figure ), ('You cannot render this Element '
92- 'if it is not in a Figure.' )
93-
94- figure .header .add_child (
95- JavascriptLink ('https://cdnjs.cloudflare.com/ajax/libs/leaflet-dvf/0.3.0/leaflet-dvf.markers.min.js' ),
96- # noqa
97- name = 'dvf_js' )
9892
99-
100- class Vega (Element ):
93+ class Vega (JSCSSMixin , Element ):
10194 """
10295 Creates a Vega chart element.
10396
@@ -128,6 +121,15 @@ class Vega(Element):
128121 """
129122 _template = Template (u'' )
130123
124+ default_js = [
125+ ('d3' ,
126+ 'https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js' ),
127+ ('vega' ,
128+ 'https://cdnjs.cloudflare.com/ajax/libs/vega/1.4.3/vega.min.js' ),
129+ ('jquery' ,
130+ 'https://code.jquery.com/jquery-2.1.0.min.js' ),
131+ ]
132+
131133 def __init__ (self , data , width = None , height = None ,
132134 left = '0%' , top = '0%' , position = 'relative' ):
133135 super (Vega , self ).__init__ ()
@@ -147,6 +149,8 @@ def __init__(self, data, width=None, height=None,
147149
148150 def render (self , ** kwargs ):
149151 """Renders the HTML representation of the element."""
152+ super ().render (** kwargs )
153+
150154 self .json = json .dumps (self .data )
151155
152156 self ._parent .html .add_child (Element (Template ("""
@@ -171,18 +175,6 @@ def render(self, **kwargs):
171175 </style>
172176 """ ).render (this = self , ** kwargs )), name = self .get_name ())
173177
174- figure .header .add_child (
175- JavascriptLink ('https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js' ), # noqa
176- name = 'd3' )
177-
178- figure .header .add_child (
179- JavascriptLink ('https://cdnjs.cloudflare.com/ajax/libs/vega/1.4.3/vega.min.js' ), # noqa
180- name = 'vega' )
181-
182- figure .header .add_child (
183- JavascriptLink ('https://code.jquery.com/jquery-2.1.0.min.js' ),
184- name = 'jquery' )
185-
186178 figure .script .add_child (
187179 Template ("""function vega_parse(spec, div) {
188180 vg.parse.spec(spec, function(chart) { chart({el:div}).update(); });}""" ), # noqa
@@ -649,7 +641,7 @@ def _set_default_key(mapping):
649641 del (mapping [key_longest ])
650642
651643
652- class TopoJson (Layer ):
644+ class TopoJson (JSCSSMixin , Layer ):
653645 """
654646 Creates a TopoJson object for plotting into a Map.
655647
@@ -721,6 +713,11 @@ class TopoJson(Layer):
721713 {% endmacro %}
722714 """ ) # noqa
723715
716+ default_js = [
717+ ('topojson' ,
718+ 'https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js' ),
719+ ]
720+
724721 def __init__ (self , data , object_path , style_function = None ,
725722 name = None , overlay = True , control = True , show = True ,
726723 smooth_factor = None , tooltip = None ):
@@ -770,14 +767,6 @@ def render(self, **kwargs):
770767 self .style_data ()
771768 super (TopoJson , self ).render (** kwargs )
772769
773- figure = self .get_root ()
774- assert isinstance (figure , Figure ), ('You cannot render this Element '
775- 'if it is not in a Figure.' )
776-
777- figure .header .add_child (
778- JavascriptLink ('https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js' ), # noqa
779- name = 'topojson' )
780-
781770 def get_bounds (self ):
782771 """
783772 Computes the bounds of the object itself (not including it's children)
0 commit comments