Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_timeline.py
1601 views
1
"""
2
Test Timeline
3
-----------------------
4
5
"""
6
7
import json
8
9
import folium
10
from folium import plugins
11
from folium.features import GeoJsonPopup
12
from folium.template import Template
13
from folium.utilities import normalize
14
15
16
def test_timeline():
17
m = folium.Map()
18
19
data = json.load(open("./examples/data/historical_country_borders.json"))
20
timeline = plugins.Timeline(
21
data,
22
).add_to(m)
23
GeoJsonPopup(fields=["name"], labels=True).add_to(timeline)
24
slider = (
25
plugins.TimelineSlider(
26
auto_play=False,
27
show_ticks=True,
28
enable_keyboard_controls=True,
29
playback_duration=30000,
30
)
31
.add_timelines(timeline)
32
.add_to(m)
33
)
34
35
out = normalize(m._parent.render())
36
37
# Verify the imports.
38
assert (
39
'<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.timeline.min.js"></script>'
40
in out
41
)
42
assert (
43
'<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>'
44
in out
45
)
46
47
# Verify that the script is okay.
48
tmpl = Template(
49
"""
50
{% macro header(this,kwargs) %}
51
<style>
52
.leaflet-bottom.leaflet-left {
53
width: 100%;
54
}
55
.leaflet-control-container .leaflet-timeline-controls {
56
box-sizing: border-box;
57
width: 100%;
58
margin: 0;
59
margin-bottom: 15px;
60
}
61
</style>
62
{% endmacro %}
63
64
{% macro script(this, kwargs) %}
65
var {{ this.get_name() }}_options = {{ this.options|tojavascript }};
66
{% for key, value in this.functions.items() %}
67
{{ this.get_name() }}_options["{{key}}"] = {{ value }};
68
{% endfor %}
69
70
var {{ this.get_name() }} = L.timelineSliderControl(
71
{{ this.get_name() }}_options
72
);
73
{{ this.get_name() }}.addTo({{ this._parent.get_name() }});
74
75
{% for timeline in this.timelines %}
76
{{ this.get_name() }}.addTimelines({{ timeline.get_name() }});
77
{% endfor %}
78
79
{% endmacro %}
80
"""
81
) # noqa
82
expected = normalize(tmpl.render(this=slider))
83
assert expected in out
84
85