Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_realtime.py
1601 views
1
"""
2
Test Realtime
3
------------------
4
"""
5
6
import folium
7
from folium.plugins import MarkerCluster, Realtime
8
from folium.template import Template
9
from folium.utilities import JsCode, normalize
10
11
12
def test_realtime():
13
m = folium.Map(location=[40.73, -73.94], zoom_start=12)
14
source = "https://raw.githubusercontent.com/python-visualization/folium-example-data/main/subway_stations.geojson"
15
16
container = MarkerCluster().add_to(m)
17
18
rt = Realtime(
19
source,
20
get_feature_id=JsCode("(f) => { return f.properties.objectid }"),
21
point_to_layer=JsCode(
22
"(f, latlng) => { return L.circleMarker(latlng, {radius: 8, fillOpacity: 0.2})}"
23
),
24
container=container,
25
interval=10000,
26
)
27
rt.add_to(m)
28
29
tmpl_for_expected = Template(
30
"""
31
{% macro script(this, kwargs) %}
32
var {{ this.get_name() }}_options = {{ this.options|tojavascript }};
33
{% for key, value in this.functions.items() %}
34
{{ this.get_name() }}_options["{{key}}"] = {{ value }};
35
{% endfor %}
36
37
{% if this.container -%}
38
{{ this.get_name() }}_options["container"]
39
= {{ this.container.get_name() }};
40
{% endif -%}
41
42
var {{ this.get_name() }} = new L.realtime(
43
{% if this.src is string or this.src is mapping -%}
44
{{ this.src|tojson }},
45
{% else -%}
46
{{ this.src.js_code }},
47
{% endif -%}
48
{{ this.get_name() }}_options
49
);
50
{{ this._parent.get_name() }}.addLayer(
51
{{ this.get_name() }}._container);
52
{% endmacro %}
53
"""
54
)
55
expected = normalize(tmpl_for_expected.render(this=rt))
56
57
out = normalize(m._parent.render())
58
59
# We verify that imports
60
assert (
61
'<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet-realtime/2.2.0/leaflet-realtime.js"></script>' # noqa
62
in out
63
) # noqa
64
65
assert expected in out
66
67