-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a custom container to the realtime plugin (#1869)
* Add cluster override * Add tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add container argument to documentation * Fix incorrect import * Update realtime.py No idea why this was gone in this branch. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
309aa08
commit 964cf97
Showing
3 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Test Realtime | ||
------------------ | ||
""" | ||
|
||
from jinja2 import Template | ||
|
||
import folium | ||
from folium.plugins import MarkerCluster, Realtime | ||
from folium.utilities import JsCode, normalize | ||
|
||
|
||
def test_realtime(): | ||
m = folium.Map(location=[40.73, -73.94], zoom_start=12) | ||
source = "https://raw.githubusercontent.com/python-visualization/folium-example-data/main/subway_stations.geojson" | ||
|
||
container = MarkerCluster().add_to(m) | ||
|
||
rt = Realtime( | ||
source, | ||
get_feature_id=JsCode("(f) => { return f.properties.objectid }"), | ||
point_to_layer=JsCode( | ||
"(f, latlng) => { return L.circleMarker(latlng, {radius: 8, fillOpacity: 0.2})}" | ||
), | ||
container=container, | ||
interval=10000, | ||
) | ||
rt.add_to(m) | ||
|
||
tmpl_for_expected = Template( | ||
""" | ||
{% macro script(this, kwargs) %} | ||
var {{ this.get_name() }}_options = {{ this.options|tojson }}; | ||
{% for key, value in this.functions.items() %} | ||
{{ this.get_name() }}_options["{{key}}"] = {{ value }}; | ||
{% endfor %} | ||
{% if this.container -%} | ||
{{ this.get_name() }}_options["container"] | ||
= {{ this.container.get_name() }}; | ||
{% endif -%} | ||
var {{ this.get_name() }} = new L.realtime( | ||
{% if this.src is string or this.src is mapping -%} | ||
{{ this.src|tojson }}, | ||
{% else -%} | ||
{{ this.src.js_code }}, | ||
{% endif -%} | ||
{{ this.get_name() }}_options | ||
); | ||
{{ this._parent.get_name() }}.addLayer( | ||
{{ this.get_name() }}._container); | ||
{% endmacro %} | ||
""" | ||
) | ||
expected = normalize(tmpl_for_expected.render(this=rt)) | ||
|
||
out = normalize(m._parent.render()) | ||
|
||
# We verify that imports | ||
assert ( | ||
'<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet-realtime/2.2.0/leaflet-realtime.js"></script>' # noqa | ||
in out | ||
) # noqa | ||
|
||
assert expected in out |