Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_timeline.py
2531 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
{% macro header(this,kwargs) %}
50
<style>
51
.leaflet-bottom.leaflet-left {
52
width: 100%;
53
}
54
.leaflet-control-container .leaflet-timeline-controls {
55
box-sizing: border-box;
56
width: 100%;
57
margin: 0;
58
margin-bottom: 15px;
59
}
60
</style>
61
{% endmacro %}
62
63
{% macro script(this, kwargs) %}
64
var {{ this.get_name() }}_options = {{ this.options|tojavascript }};
65
{% for key, value in this.functions.items() %}
66
{{ this.get_name() }}_options["{{key}}"] = {{ value }};
67
{% endfor %}
68
69
var {{ this.get_name() }} = L.timelineSliderControl(
70
{{ this.get_name() }}_options
71
);
72
{{ this.get_name() }}.addTo({{ this._parent.get_name() }});
73
74
{% for timeline in this.timelines %}
75
{{ this.get_name() }}.addTimelines({{ timeline.get_name() }});
76
{% endfor %}
77
78
{% endmacro %}
79
""") # noqa
80
expected = normalize(tmpl.render(this=slider))
81
assert expected in out
82
83