Skip to content

Commit

Permalink
realtime plugin: allow str for JsCode arg (#1862)
Browse files Browse the repository at this point in the history
* realtime plugin: allow str for JsCode arg

* JsCode: don't inherit from str

* ruff

* add tests for JsCode

* run black
  • Loading branch information
Conengmo authored Feb 28, 2024
1 parent 9475ace commit 36f2ff6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
17 changes: 9 additions & 8 deletions folium/plugins/realtime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Union

from branca.element import MacroElement
from jinja2 import Template
Expand Down Expand Up @@ -27,11 +27,11 @@ class Realtime(JSCSSMixin, MacroElement):
on the map and stopped when layer is removed from the map
interval: int, default 60000
Automatic update interval, in milliseconds
get_feature_id: JsCode, optional
get_feature_id: str or JsCode, optional
A JS function with a geojson `feature` as parameter
default returns `feature.properties.id`
Function to get an identifier to uniquely identify a feature over time
update_feature: JsCode, optional
update_feature: str or JsCode, optional
A JS function with a geojson `feature` as parameter
Used to update an existing feature's layer;
by default, points (markers) are updated, other layers are discarded
Expand All @@ -44,7 +44,8 @@ class Realtime(JSCSSMixin, MacroElement):
Other keyword arguments are passed to the GeoJson layer, so you can pass
`style`, `point_to_layer` and/or `on_each_feature`.
`style`, `point_to_layer` and/or `on_each_feature`. Make sure to wrap
Javascript functions in the JsCode class.
Examples
--------
Expand Down Expand Up @@ -95,8 +96,8 @@ def __init__(
source: Union[str, dict, JsCode],
start: bool = True,
interval: int = 60000,
get_feature_id: Optional[JsCode] = None,
update_feature: Optional[JsCode] = None,
get_feature_id: Union[JsCode, str, None] = None,
update_feature: Union[JsCode, str, None] = None,
remove_missing: bool = False,
**kwargs
):
Expand All @@ -107,9 +108,9 @@ def __init__(
kwargs["start"] = start
kwargs["interval"] = interval
if get_feature_id is not None:
kwargs["get_feature_id"] = get_feature_id
kwargs["get_feature_id"] = JsCode(get_feature_id)
if update_feature is not None:
kwargs["update_feature"] = update_feature
kwargs["update_feature"] = JsCode(update_feature)
kwargs["remove_missing"] = remove_missing

# extract JsCode objects
Expand Down
7 changes: 5 additions & 2 deletions folium/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,8 @@ def get_and_assert_figure_root(obj: Element) -> Figure:
class JsCode:
"""Wrapper around Javascript code."""

def __init__(self, js_code: str):
self.js_code = js_code
def __init__(self, js_code: Union[str, "JsCode"]):
if isinstance(js_code, JsCode):
self.js_code: str = js_code.js_code
else:
self.js_code = js_code
14 changes: 14 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from folium import FeatureGroup, Map, Marker, Popup
from folium.utilities import (
JsCode,
_is_url,
camelize,
deep_copy,
Expand Down Expand Up @@ -216,3 +217,16 @@ def test_escape_double_quotes(text, result):
)
def test_javascript_identifier_path_to_array_notation(text, result):
assert javascript_identifier_path_to_array_notation(text) == result


def test_js_code_init_str():
js_code = JsCode("hi")
assert isinstance(js_code, JsCode)
assert isinstance(js_code.js_code, str)


def test_js_code_init_js_code():
js_code = JsCode("hi")
js_code_2 = JsCode(js_code)
assert isinstance(js_code_2, JsCode)
assert isinstance(js_code_2.js_code, str)

0 comments on commit 36f2ff6

Please sign in to comment.