Skip to content
Open
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
17 changes: 13 additions & 4 deletions folium/plugins/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,9 +82,6 @@ class Realtime(JSCSSMixin, FeatureGroup):
{% endif -%}
{{ this.options | tojavascript }}
);
{{ this._parent.get_name() }}.addLayer(
{{ this.get_name() }}._container
);
{% endmacro %}
""")

Expand All @@ -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

Expand Down
19 changes: 19 additions & 0 deletions tests/plugins/test_realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]