2
2
from jinja2 import Template
3
3
4
4
from folium .elements import JSCSSMixin
5
- from folium .utilities import JsCode , parse_options
5
+ from folium .utilities import JsCode , camelize , parse_options
6
6
7
7
8
8
class Realtime (JSCSSMixin , MacroElement ):
@@ -13,33 +13,45 @@ class Realtime(JSCSSMixin, MacroElement):
13
13
14
14
Parameters
15
15
----------
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.
16
22
start : bool, default True
17
23
Should automatic updates be enabled when layer is added
18
24
on the map and stopped when layer is removed from the map
19
25
interval : int, default 60000
20
26
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`
22
30
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
24
33
Used to update an existing feature's layer;
25
34
by default, points (markers) are updated, other layers are discarded
26
35
and replaced with a new, updated layer.
27
36
Allows to create more complex transitions,
28
37
for example, when a feature is updated
29
- removeMissing : bool, default False
38
+ remove_missing : bool, default False
30
39
Should missing features between updates been automatically
31
40
removed from the layer
32
41
33
42
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 `.
35
44
36
45
Examples
37
46
--------
38
47
>>> from folium.utilities import JsCode
39
- >>> m = folium.Map()
48
+ >>> m = folium.Map(location=[40.73, -73.94], zoom_start=12 )
40
49
>>> 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
+ ... ),
43
55
... interval=10000,
44
56
... )
45
57
>>> rt.add_to(m)
@@ -52,8 +64,13 @@ class Realtime(JSCSSMixin, MacroElement):
52
64
{% for key, value in this.functions.items() %}
53
65
options["{{key}}"] = {{ value }};
54
66
{% endfor %}
67
+
55
68
var {{ this.get_name() }} = new L.realtime(
69
+ {% if this.src is string or this.src is mapping -%}
56
70
{{ this.src|tojson }},
71
+ {% else -%}
72
+ {{ this.src.js_code }},
73
+ {% endif -%}
57
74
options
58
75
);
59
76
{{ this._parent.get_name() }}.addLayer(
@@ -69,22 +86,36 @@ class Realtime(JSCSSMixin, MacroElement):
69
86
)
70
87
]
71
88
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
+ ):
73
99
super ().__init__ ()
74
100
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
76
113
77
114
# extract JsCode objects
78
115
self .functions = {}
79
- for key , value in kwargs .items ():
116
+ for key , value in list ( kwargs .items () ):
80
117
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 )
86
120
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