Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_realtime.py
2529 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
{% macro script(this, kwargs) %}
31
var {{ this.get_name() }}_options = {{ this.options|tojavascript }};
32
{% for key, value in this.functions.items() %}
33
{{ this.get_name() }}_options["{{key}}"] = {{ value }};
34
{% endfor %}
35
36
{% if this.container -%}
37
{{ this.get_name() }}_options["container"]
38
= {{ this.container.get_name() }};
39
{% endif -%}
40
41
var {{ this.get_name() }} = new L.realtime(
42
{% if this.src is string or this.src is mapping -%}
43
{{ this.src|tojson }},
44
{% else -%}
45
{{ this.src.js_code }},
46
{% endif -%}
47
{{ this.get_name() }}_options
48
);
49
{{ this._parent.get_name() }}.addLayer(
50
{{ this.get_name() }}._container);
51
{% endmacro %}
52
""")
53
expected = normalize(tmpl_for_expected.render(this=rt))
54
55
out = normalize(m._parent.render())
56
57
# We verify that imports
58
assert (
59
'<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet-realtime/2.2.0/leaflet-realtime.js"></script>' # noqa
60
in out
61
) # noqa
62
63
assert expected in out
64
65