Skip to content

Commit 36f2ff6

Browse files
authored
realtime plugin: allow str for JsCode arg (#1862)
* realtime plugin: allow str for JsCode arg * JsCode: don't inherit from str * ruff * add tests for JsCode * run black
1 parent 9475ace commit 36f2ff6

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

folium/plugins/realtime.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, Union
1+
from typing import Union
22

33
from branca.element import MacroElement
44
from jinja2 import Template
@@ -27,11 +27,11 @@ class Realtime(JSCSSMixin, MacroElement):
2727
on the map and stopped when layer is removed from the map
2828
interval: int, default 60000
2929
Automatic update interval, in milliseconds
30-
get_feature_id: JsCode, optional
30+
get_feature_id: str or JsCode, optional
3131
A JS function with a geojson `feature` as parameter
3232
default returns `feature.properties.id`
3333
Function to get an identifier to uniquely identify a feature over time
34-
update_feature: JsCode, optional
34+
update_feature: str or JsCode, optional
3535
A JS function with a geojson `feature` as parameter
3636
Used to update an existing feature's layer;
3737
by default, points (markers) are updated, other layers are discarded
@@ -44,7 +44,8 @@ class Realtime(JSCSSMixin, MacroElement):
4444
4545
4646
Other keyword arguments are passed to the GeoJson layer, so you can pass
47-
`style`, `point_to_layer` and/or `on_each_feature`.
47+
`style`, `point_to_layer` and/or `on_each_feature`. Make sure to wrap
48+
Javascript functions in the JsCode class.
4849
4950
Examples
5051
--------
@@ -95,8 +96,8 @@ def __init__(
9596
source: Union[str, dict, JsCode],
9697
start: bool = True,
9798
interval: int = 60000,
98-
get_feature_id: Optional[JsCode] = None,
99-
update_feature: Optional[JsCode] = None,
99+
get_feature_id: Union[JsCode, str, None] = None,
100+
update_feature: Union[JsCode, str, None] = None,
100101
remove_missing: bool = False,
101102
**kwargs
102103
):
@@ -107,9 +108,9 @@ def __init__(
107108
kwargs["start"] = start
108109
kwargs["interval"] = interval
109110
if get_feature_id is not None:
110-
kwargs["get_feature_id"] = get_feature_id
111+
kwargs["get_feature_id"] = JsCode(get_feature_id)
111112
if update_feature is not None:
112-
kwargs["update_feature"] = update_feature
113+
kwargs["update_feature"] = JsCode(update_feature)
113114
kwargs["remove_missing"] = remove_missing
114115

115116
# extract JsCode objects

folium/utilities.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,5 +423,8 @@ def get_and_assert_figure_root(obj: Element) -> Figure:
423423
class JsCode:
424424
"""Wrapper around Javascript code."""
425425

426-
def __init__(self, js_code: str):
427-
self.js_code = js_code
426+
def __init__(self, js_code: Union[str, "JsCode"]):
427+
if isinstance(js_code, JsCode):
428+
self.js_code: str = js_code.js_code
429+
else:
430+
self.js_code = js_code

tests/test_utilities.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from folium import FeatureGroup, Map, Marker, Popup
66
from folium.utilities import (
7+
JsCode,
78
_is_url,
89
camelize,
910
deep_copy,
@@ -216,3 +217,16 @@ def test_escape_double_quotes(text, result):
216217
)
217218
def test_javascript_identifier_path_to_array_notation(text, result):
218219
assert javascript_identifier_path_to_array_notation(text) == result
220+
221+
222+
def test_js_code_init_str():
223+
js_code = JsCode("hi")
224+
assert isinstance(js_code, JsCode)
225+
assert isinstance(js_code.js_code, str)
226+
227+
228+
def test_js_code_init_js_code():
229+
js_code = JsCode("hi")
230+
js_code_2 = JsCode(js_code)
231+
assert isinstance(js_code_2, JsCode)
232+
assert isinstance(js_code_2.js_code, str)

0 commit comments

Comments
 (0)