Skip to content

Commit a2af509

Browse files
committed
Updated after review comments
1 parent 85afba5 commit a2af509

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

folium/plugins/realtime.py

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from jinja2 import Template
33

44
from folium.elements import JSCSSMixin
5-
from folium.utilities import JsCode, parse_options
5+
from folium.utilities import JsCode, camelize, parse_options
66

77

88
class Realtime(JSCSSMixin, MacroElement):
@@ -13,33 +13,45 @@ class Realtime(JSCSSMixin, MacroElement):
1313
1414
Parameters
1515
----------
16+
source :
17+
The source can be one of:
18+
* a string with the URL to get data from
19+
* a dict that is passed to javascript's `fetch` function
20+
for fetching the data
21+
* a folium.utilities.JsCode object in case you need more freedom.
1622
start : bool, default True
1723
Should automatic updates be enabled when layer is added
1824
on the map and stopped when layer is removed from the map
1925
interval : int, default 60000
2026
Automatic update interval, in milliseconds
21-
getFeatureId : function, default returns `feature.properties.id`
27+
get_feature_id : folium.utilities.JsCode
28+
A function with a geojson `feature` as parameter
29+
default returns `feature.properties.id`
2230
Function to get an identifier uniquely identify a feature over time
23-
updateFeature : function
31+
update_feature : folium.utilities.JsCode
32+
A function with a geojson `feature` as parameter
2433
Used to update an existing feature's layer;
2534
by default, points (markers) are updated, other layers are discarded
2635
and replaced with a new, updated layer.
2736
Allows to create more complex transitions,
2837
for example, when a feature is updated
29-
removeMissing : bool, default False
38+
remove_missing : bool, default False
3039
Should missing features between updates been automatically
3140
removed from the layer
3241
3342
Other parameters are passed to the GeoJson layer, so you can pass
34-
`style`, `pointToLayer` and/or `onEachFeature`.
43+
`style`, `point_to_layer` and/or `on_each_feature`.
3544
3645
Examples
3746
--------
3847
>>> from folium.utilities import JsCode
39-
>>> m = folium.Map()
48+
>>> m = folium.Map(location=[40.73, -73.94], zoom_start=12)
4049
>>> rt = Realtime(
41-
... "https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_10m_geography_regions_elevation_points.geojson",
42-
... getFeatureId=JsCode("function(f) { return f.properties.name; }"),
50+
... "https://raw.githubusercontent.com/python-visualization/folium-example-data/main/subway_stations.geojson",
51+
... get_feature_id=JsCode("(f) => { return f.properties.objectid; }"),
52+
... point_to_layer=JsCode(
53+
... "(f, latlng) => { return L.circleMarker(latlng, {radius: 8, fillOpacity: 0.2})}"
54+
... ),
4355
... interval=10000,
4456
... )
4557
>>> rt.add_to(m)
@@ -52,8 +64,13 @@ class Realtime(JSCSSMixin, MacroElement):
5264
{% for key, value in this.functions.items() %}
5365
options["{{key}}"] = {{ value }};
5466
{% endfor %}
67+
5568
var {{ this.get_name() }} = new L.realtime(
69+
{% if this.src is string or this.src is mapping -%}
5670
{{ this.src|tojson }},
71+
{% else -%}
72+
{{ this.src.js_code }},
73+
{% endif -%}
5774
options
5875
);
5976
{{ this._parent.get_name() }}.addLayer(
@@ -69,22 +86,36 @@ class Realtime(JSCSSMixin, MacroElement):
6986
)
7087
]
7188

72-
def __init__(self, src, **kwargs):
89+
def __init__(
90+
self,
91+
source,
92+
start=None,
93+
interval=None,
94+
get_feature_id=None,
95+
update_feature=None,
96+
remove_missing=None,
97+
**kwargs
98+
):
7399
super().__init__()
74100
self._name = "Realtime"
75-
self.src = src
101+
self.src = source
102+
103+
if start is not None:
104+
kwargs["start"] = start
105+
if interval is not None:
106+
kwargs["interval"] = interval
107+
if get_feature_id is not None:
108+
kwargs["get_feature_id"] = get_feature_id
109+
if update_feature is not None:
110+
kwargs["update_feature"] = update_feature
111+
if remove_missing is not None:
112+
kwargs["remove_missing"] = remove_missing
76113

77114
# extract JsCode objects
78115
self.functions = {}
79-
for key, value in kwargs.items():
116+
for key, value in list(kwargs.items()):
80117
if isinstance(value, JsCode):
81-
self.functions[key] = value.js_code
82-
83-
# and remove them from kwargs
84-
for key in self.functions:
85-
kwargs.pop(key)
118+
self.functions[camelize(key)] = value.js_code
119+
kwargs.pop(key)
86120

87-
# the container is special, as we
88-
# do not allow it to be set (yet)
89-
# from python
90-
self.options = parse_options(container=None, **kwargs)
121+
self.options = parse_options(**kwargs)

0 commit comments

Comments
 (0)