55
66import warnings
77from collections import OrderedDict
8- from typing import Dict , List , Optional , Sequence , Tuple , Type , Union
8+ from typing import List , Optional , Sequence , Union
99
1010from branca .element import Element , Figure , Html , MacroElement
11- from jinja2 import Template
1211
1312from folium .elements import ElementAddToElement , EventHandler
13+ from folium .template import Template
1414from folium .utilities import (
1515 JsCode ,
1616 TypeBounds ,
1717 TypeJsonValue ,
18- camelize ,
1918 escape_backticks ,
2019 parse_options ,
2120 validate_location ,
@@ -109,7 +108,7 @@ class FeatureGroup(Layer):
109108 """
110109 {% macro script(this, kwargs) %}
111110 var {{ this.get_name() }} = L.featureGroup(
112- {{ this.options|tojson }}
111+ {{ this.options|tojavascript }}
113112 );
114113 {% endmacro %}
115114 """
@@ -126,7 +125,7 @@ def __init__(
126125 super ().__init__ (name = name , overlay = overlay , control = control , show = show )
127126 self ._name = "FeatureGroup"
128127 self .tile_name = name if name is not None else self .get_name ()
129- self .options = parse_options ( ** kwargs )
128+ self .options = kwargs
130129
131130
132131class LayerControl (MacroElement ):
@@ -180,7 +179,7 @@ class LayerControl(MacroElement):
180179 let {{ this.get_name() }} = L.control.layers(
181180 {{ this.get_name() }}_layers.base_layers,
182181 {{ this.get_name() }}_layers.overlays,
183- {{ this.options|tojson }}
182+ {{ this.options|tojavascript }}
184183 ).addTo({{this._parent.get_name()}});
185184
186185 {%- if this.draggable %}
@@ -201,7 +200,7 @@ def __init__(
201200 ):
202201 super ().__init__ ()
203202 self ._name = "LayerControl"
204- self .options = parse_options (
203+ self .options = dict (
205204 position = position , collapsed = collapsed , autoZIndex = autoZIndex , ** kwargs
206205 )
207206 self .draggable = draggable
@@ -263,7 +262,7 @@ class Icon(MacroElement):
263262 """
264263 {% macro script(this, kwargs) %}
265264 var {{ this.get_name() }} = L.AwesomeMarkers.icon(
266- {{ this.options|tojson }}
265+ {{ this.options|tojavascript }}
267266 );
268267 {{ this._parent.get_name() }}.setIcon({{ this.get_name() }});
269268 {% endmacro %}
@@ -307,7 +306,7 @@ def __init__(
307306 f"color argument of Icon should be one of: { self .color_options } ." ,
308307 stacklevel = 2 ,
309308 )
310- self .options = parse_options (
309+ self .options = dict (
311310 marker_color = color ,
312311 icon_color = icon_color ,
313312 icon = icon ,
@@ -356,7 +355,7 @@ class Marker(MacroElement):
356355 {% macro script(this, kwargs) %}
357356 var {{ this.get_name() }} = L.marker(
358357 {{ this.location|tojson }},
359- {{ this.options|tojson }}
358+ {{ this.options|tojavascript }}
360359 ).addTo({{ this._parent.get_name() }});
361360 {% endmacro %}
362361 """
@@ -374,7 +373,7 @@ def __init__(
374373 super ().__init__ ()
375374 self ._name = "Marker"
376375 self .location = validate_location (location ) if location is not None else None
377- self .options = parse_options (
376+ self .options = dict (
378377 draggable = draggable or None , autoPan = draggable or None , ** kwargs
379378 )
380379 if icon is not None :
@@ -424,7 +423,7 @@ class Popup(Element):
424423
425424 _template = Template (
426425 """
427- var {{this.get_name()}} = L.popup({{ this.options|tojson }});
426+ var {{this.get_name()}} = L.popup({{ this.options|tojavascript }});
428427
429428 {% for name, element in this.html._children.items() %}
430429 {% if this.lazy %}
@@ -476,7 +475,7 @@ def __init__(
476475
477476 self .show = show
478477 self .lazy = lazy
479- self .options = parse_options (
478+ self .options = dict (
480479 max_width = max_width ,
481480 autoClose = False if show or sticky else None ,
482481 closeOnClick = False if sticky else None ,
@@ -526,22 +525,11 @@ class Tooltip(MacroElement):
526525 `<div{% if this.style %} style={{ this.style|tojson }}{% endif %}>
527526 {{ this.text }}
528527 </div>`,
529- {{ this.options|tojson }}
528+ {{ this.options|tojavascript }}
530529 );
531530 {% endmacro %}
532531 """
533532 )
534- valid_options : Dict [str , Tuple [Type , ...]] = {
535- "pane" : (str ,),
536- "offset" : (tuple ,),
537- "direction" : (str ,),
538- "permanent" : (bool ,),
539- "sticky" : (bool ,),
540- "interactive" : (bool ,),
541- "opacity" : (float , int ),
542- "attribution" : (str ,),
543- "className" : (str ,),
544- }
545533
546534 def __init__ (
547535 self ,
@@ -556,7 +544,7 @@ def __init__(
556544 self .text = str (text )
557545
558546 kwargs .update ({"sticky" : sticky })
559- self .options = self . parse_options ( kwargs )
547+ self .options = kwargs
560548
561549 if style :
562550 assert isinstance (
@@ -565,23 +553,6 @@ def __init__(
565553 # noqa outside of type checking.
566554 self .style = style
567555
568- def parse_options (
569- self ,
570- kwargs : Dict [str , TypeJsonValue ],
571- ) -> Dict [str , TypeJsonValue ]:
572- """Validate the provided kwargs and return options as json string."""
573- kwargs = {camelize (key ): value for key , value in kwargs .items ()}
574- for key in kwargs .keys ():
575- assert (
576- key in self .valid_options
577- ), "The option {} is not in the available options: {}." .format (
578- key , ", " .join (self .valid_options )
579- )
580- assert isinstance (
581- kwargs [key ], self .valid_options [key ]
582- ), f"The option { key } must be one of the following types: { self .valid_options [key ]} ."
583- return kwargs
584-
585556
586557class FitBounds (MacroElement ):
587558 """Fit the map to contain a bounding box with the
@@ -660,7 +631,7 @@ class FitOverlays(MacroElement):
660631 }
661632 });
662633 if (bounds.isValid()) {
663- {{ this._parent.get_name() }}.{{ this.method }}(bounds, {{ this.options|tojson }});
634+ {{ this._parent.get_name() }}.{{ this.method }}(bounds, {{ this.options|tojavascript }});
664635 }
665636 }
666637 {{ this._parent.get_name() }}.on('overlayadd', customFlyToBounds);
@@ -682,7 +653,7 @@ def __init__(
682653 self ._name = "FitOverlays"
683654 self .method = "flyToBounds" if fly else "fitBounds"
684655 self .fit_on_map_load = fit_on_map_load
685- self .options = parse_options (padding = (padding , padding ), max_zoom = max_zoom )
656+ self .options = dict (padding = (padding , padding ), max_zoom = max_zoom )
686657
687658
688659class CustomPane (MacroElement ):
0 commit comments