Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process JS code separately [draft] #1856

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/user_guide/plugins/mouse_position.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ m
```{code-cell} ipython3
m = folium.Map()

formatter = "function(num) {return L.Util.formatNum(num, 3) + ' ° ';};"
formatter = "function(num) {return L.Util.formatNum(num, 3) + ' ° '}"

MousePosition(
position="topright",
Expand Down
3 changes: 2 additions & 1 deletion folium/elements.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import List, Tuple

from branca.element import CssLink, Element, Figure, JavascriptLink, MacroElement
from jinja2 import Template

from folium.template import Template


class JSCSSMixin(Element):
Expand Down
2 changes: 1 addition & 1 deletion folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
from branca.colormap import ColorMap, LinearColormap, StepColormap
from branca.element import Element, Figure, Html, IFrame, JavascriptLink, MacroElement
from branca.utilities import color_brewer
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.folium import Map
from folium.map import FeatureGroup, Icon, Layer, Marker, Popup, Tooltip
from folium.template import Template
from folium.utilities import (
TypeJsonValue,
TypeLine,
Expand Down
2 changes: 1 addition & 1 deletion folium/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from typing import Any, List, Optional, Sequence, Union

from branca.element import Element, Figure, MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.map import FitBounds, Layer
from folium.raster_layers import TileLayer
from folium.template import Template
from folium.utilities import (
TypeBounds,
TypeJsonValue,
Expand Down
27 changes: 8 additions & 19 deletions folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from typing import Dict, List, Optional, Sequence, Tuple, Type, Union

from branca.element import Element, Figure, Html, MacroElement
from jinja2 import Template

from folium.elements import ElementAddToElement
from folium.template import Template
from folium.utilities import (
JsCode,
TypeBounds,
TypeJsonValue,
camelize,
Expand Down Expand Up @@ -147,21 +148,9 @@ class LayerControl(MacroElement):
_template = Template(
"""
{% macro script(this,kwargs) %}
var {{ this.get_name() }}_layers = {
base_layers : {
{%- for key, val in this.base_layers.items() %}
{{ key|tojson }} : {{val}},
{%- endfor %}
},
overlays : {
{%- for key, val in this.overlays.items() %}
{{ key|tojson }} : {{val}},
{%- endfor %}
},
};
let {{ this.get_name() }} = L.control.layers(
{{ this.get_name() }}_layers.base_layers,
{{ this.get_name() }}_layers.overlays,
{{ this.base_layers|tojavascript }},
{{ this.overlays|tojavascript }},
{{ this.options|tojson }}
).addTo({{this._parent.get_name()}});

Expand All @@ -187,8 +176,8 @@ def __init__(
position=position, collapsed=collapsed, autoZIndex=autoZIndex, **kwargs
)
self.draggable = draggable
self.base_layers: OrderedDict[str, str] = OrderedDict()
self.overlays: OrderedDict[str, str] = OrderedDict()
self.base_layers: OrderedDict[str, JsCode] = OrderedDict()
self.overlays: OrderedDict[str, JsCode] = OrderedDict()

def reset(self) -> None:
self.base_layers = OrderedDict()
Expand All @@ -202,9 +191,9 @@ def render(self, **kwargs) -> None:
continue
key = item.layer_name
if not item.overlay:
self.base_layers[key] = item.get_name()
self.base_layers[key] = JsCode(item.get_name())
else:
self.overlays[key] = item.get_name()
self.overlays[key] = JsCode(item.get_name())
super().render()


Expand Down
3 changes: 1 addition & 2 deletions folium/plugins/antpath.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.template import Template
from folium.vector_layers import BaseMultiLocation, path_options


Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/beautify_icon.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from branca.element import MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.template import Template
from folium.utilities import parse_options


Expand Down
3 changes: 1 addition & 2 deletions folium/plugins/boat_marker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.map import Marker
from folium.template import Template
from folium.utilities import parse_options


Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/draw.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from branca.element import Element, Figure, MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.template import Template


class Draw(JSCSSMixin, MacroElement):
Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/dual_map.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from branca.element import Figure, MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.folium import Map
from folium.map import LayerControl
from folium.template import Template
from folium.utilities import deep_copy


Expand Down
3 changes: 1 addition & 2 deletions folium/plugins/fast_marker_cluster.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from jinja2 import Template

from folium.plugins.marker_cluster import MarkerCluster
from folium.template import Template
from folium.utilities import if_pandas_df_convert_to_numpy, validate_location


Expand Down
3 changes: 1 addition & 2 deletions folium/plugins/feature_group_sub_group.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.map import Layer
from folium.template import Template


class FeatureGroupSubGroup(JSCSSMixin, Layer):
Expand Down
3 changes: 2 additions & 1 deletion folium/plugins/float_image.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from branca.element import MacroElement
from jinja2 import Template

from folium.template import Template


class FloatImage(MacroElement):
Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/fullscreen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from branca.element import MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.template import Template
from folium.utilities import parse_options


Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/geocoder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from branca.element import MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.template import Template
from folium.utilities import parse_options


Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/groupedlayercontrol.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from branca.element import MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.template import Template
from folium.utilities import parse_options


Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/heat_map.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import warnings

import numpy as np
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.map import Layer
from folium.template import Template
from folium.utilities import (
if_pandas_df_convert_to_numpy,
none_max,
Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/heat_map_withtime.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from branca.element import Element, Figure
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.map import Layer
from folium.template import Template
from folium.utilities import none_max, none_min


Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/locate_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"""

from branca.element import MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.template import Template
from folium.utilities import parse_options


Expand Down
3 changes: 1 addition & 2 deletions folium/plugins/marker_cluster.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.map import Layer, Marker
from folium.template import Template
from folium.utilities import parse_options, validate_locations


Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/measure_control.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from branca.element import MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.template import Template
from folium.utilities import parse_options


Expand Down
2 changes: 1 addition & 1 deletion folium/plugins/minimap.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from branca.element import MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.raster_layers import TileLayer
from folium.template import Template
from folium.utilities import parse_options


Expand Down
26 changes: 11 additions & 15 deletions folium/plugins/mouse_position.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from branca.element import MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.utilities import parse_options
from folium.template import Template
from folium.utilities import JsCode, TypeJsFunctionArg


class MousePosition(JSCSSMixin, MacroElement):
Expand All @@ -27,14 +27,14 @@ class MousePosition(JSCSSMixin, MacroElement):
longitude and latitude decimal degree values.
prefix : str, default ''
A string to be prepended to the coordinates.
lat_formatter : str, default None
lat_formatter : str or JsCode, optional
Custom Javascript function to format the latitude value.
lng_formatter : str, default None
lng_formatter : str or JsCode, optional
Custom Javascript function to format the longitude value.

Examples
--------
>>> fmtr = "function(num) {return L.Util.formatNum(num, 3) + ' º ';};"
>>> fmtr = "function(num) {return L.Util.formatNum(num, 3) + ' º '}"
>>> MousePosition(
... position="topright",
... separator=" | ",
Expand All @@ -49,12 +49,8 @@ class MousePosition(JSCSSMixin, MacroElement):
"""
{% macro script(this, kwargs) %}
var {{ this.get_name() }} = new L.Control.MousePosition(
{{ this.options|tojson }}
{{ this.options|tojavascript }}
);
{{ this.get_name() }}.options["latFormatter"] =
{{ this.lat_formatter }};
{{ this.get_name() }}.options["lngFormatter"] =
{{ this.lng_formatter }};
{{ this._parent.get_name() }}.addControl({{ this.get_name() }});
{% endmacro %}
"""
Expand All @@ -81,21 +77,21 @@ def __init__(
lng_first=False,
num_digits=5,
prefix="",
lat_formatter=None,
lng_formatter=None,
lat_formatter: TypeJsFunctionArg = None,
lng_formatter: TypeJsFunctionArg = None,
**kwargs
):
super().__init__()
self._name = "MousePosition"

self.options = parse_options(
self.options = dict(
position=position,
separator=separator,
empty_string=empty_string,
lng_first=lng_first,
num_digits=num_digits,
prefix=prefix,
lat_formatter=JsCode.optional_create(lat_formatter),
lng_formatter=JsCode.optional_create(lng_formatter),
**kwargs
)
self.lat_formatter = lat_formatter or "undefined"
self.lng_formatter = lng_formatter or "undefined"
2 changes: 1 addition & 1 deletion folium/plugins/pattern.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from branca.element import MacroElement
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.folium import Map
from folium.template import Template
from folium.utilities import get_obj_in_upper_tree, parse_options


Expand Down
3 changes: 1 addition & 2 deletions folium/plugins/polyline_text_path.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from jinja2 import Template

from folium.elements import JSCSSMixin
from folium.features import MacroElement
from folium.template import Template
from folium.utilities import parse_options


Expand Down
Loading