Skip to content

Commit 46f820e

Browse files
authored
Use synchronous ajax to load data in GeoJson (#1353)
* Use synchronous ajax in GeoJson * Add test case
1 parent 776d774 commit 46f820e

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

folium/features.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,17 +428,20 @@ class GeoJson(Layer):
428428
{% if this.style %}
429429
style: {{ this.get_name() }}_styler,
430430
{%- endif %}
431-
}).addTo({{ this._parent.get_name() }});
431+
});
432432
433433
function {{ this.get_name() }}_add (data) {
434-
{{ this.get_name() }}.addData(data);
434+
{{ this.get_name() }}
435+
.addData(data)
436+
.addTo({{ this._parent.get_name() }});
435437
}
436438
{%- if this.embed %}
437439
{{ this.get_name() }}_add({{ this.data|tojson }});
438440
{%- else %}
439-
$.ajax({{ this.embed_link|tojson }}, {dataType: 'json'})
441+
$.ajax({{ this.embed_link|tojson }}, {dataType: 'json', async: false})
440442
.done({{ this.get_name() }}_add);
441443
{%- endif %}
444+
442445
{% endmacro %}
443446
""") # noqa
444447

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import folium
2+
import folium.plugins
3+
from folium.utilities import temp_html_filepath
4+
5+
6+
def test_geojson(driver):
7+
"""Verify that loading data in GeoJson works well for different use cases.
8+
9+
Prevent two regressions:
10+
- https://github.com/python-visualization/folium/pull/1190
11+
- https://github.com/python-visualization/folium/pull/1289
12+
13+
"""
14+
data_url = 'https://cdn.jsdelivr.net/gh/python-visualization/folium@master/examples/data/search_bars_rome.json'
15+
16+
m = folium.Map((41.9, 12.5), zoom_start=10, tiles='cartodbpositron')
17+
marker_cluster = folium.plugins.MarkerCluster(name='cluster').add_to(m)
18+
folium.GeoJson(data_url, embed=False).add_to(marker_cluster)
19+
folium.GeoJson(data_url, embed=False, show=False, name='geojson').add_to(m)
20+
folium.LayerControl(collapsed=False).add_to(m)
21+
22+
html = m.get_root().render()
23+
with temp_html_filepath(html) as filepath:
24+
driver.get_file(filepath)
25+
assert driver.wait_until('.folium-map')
26+
driver.verify_js_logs()
27+
# Verify the marker cluster is shown, it's a yellow icon with '18' in it.
28+
icon = driver.wait_until('.leaflet-marker-icon.marker-cluster > div > span')
29+
assert icon.text == '18'
30+
# Verify the second GeoJson layer is not shown, because we used show=False.
31+
control_label = driver.wait_until(
32+
'.leaflet-control-layers-overlays > label:nth-of-type(2)'
33+
)
34+
assert control_label.text == 'geojson'
35+
control_input = control_label.find_element_by_css_selector('input')
36+
assert control_input.get_attribute('checked') is None

0 commit comments

Comments
 (0)