From 6ee44274a1f496e46b5595933a62a9cb85d86b70 Mon Sep 17 00:00:00 2001 From: Volodymyr Borysenko Date: Wed, 25 Nov 2026 00:24:45 -0800 Subject: [PATCH] Pass layer options through in Realtime --- folium/plugins/realtime.py | 17 +++++++++++++---- tests/plugins/test_realtime.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/folium/plugins/realtime.py b/folium/plugins/realtime.py index d953bb3cb5..dc79faa0a5 100644 --- a/folium/plugins/realtime.py +++ b/folium/plugins/realtime.py @@ -44,6 +44,14 @@ class Realtime(JSCSSMixin, FeatureGroup): `GeoJson`, but it can be anything that generates a javascript L.LayerGroup object, i.e. something that has the methods `addLayer` and `removeLayer`. + name : string, default None + The name of the Layer, as it will appear in LayerControls. + overlay : bool, default True + Adds the layer as an optional overlay (True) or the base layer (False). + control : bool, default True + Whether the Layer will be included in LayerControls. + show: bool, default True + Whether the layer will be shown on opening. Other keyword arguments are passed to the GeoJson layer, so you can pass `style`, `point_to_layer` and/or `on_each_feature`. Make sure to wrap @@ -74,9 +82,6 @@ class Realtime(JSCSSMixin, FeatureGroup): {% endif -%} {{ this.options | tojavascript }} ); - {{ this._parent.get_name() }}.addLayer( - {{ this.get_name() }}._container - ); {% endmacro %} """) @@ -96,9 +101,13 @@ def __init__( update_feature: JsCode | str | None = None, remove_missing: bool = False, container: FeatureGroup | GeoJson | None = None, + name: str | None = None, + overlay: bool = True, + control: bool = True, + show: bool = True, **kwargs, ): - super().__init__() + super().__init__(name=name, overlay=overlay, control=control, show=show) self._name = "Realtime" self.src = source diff --git a/tests/plugins/test_realtime.py b/tests/plugins/test_realtime.py index 109598ba95..a10a8adacc 100644 --- a/tests/plugins/test_realtime.py +++ b/tests/plugins/test_realtime.py @@ -61,3 +61,22 @@ def test_realtime(): ) # noqa assert expected in out + + +def test_realtime_layer_options(): + m = folium.Map() + source = "https://example.com/data.geojson" + hidden = Realtime(source, name="Hidden", show=False).add_to(m) + shown = Realtime(source, name="Shown").add_to(m) + Realtime(source, name="Not in control", control=False).add_to(m) + layer_control = folium.LayerControl().add_to(m) + + out = normalize(m._parent.render()) + + assert hidden.layer_name == "Hidden" + assert '"name"' not in out + assert '"show"' not in out + assert f"{hidden.get_name()}.addTo(" not in out + assert f"{hidden.get_name()}._container" not in out + assert out.count(f"{shown.get_name()}.addTo(") == 1 + assert list(layer_control.overlays) == ["Hidden", "Shown"]