Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,33 @@ class FitBounds(MacroElement):

_template = Template("""
{% macro script(this, kwargs) %}
{{ this._parent.get_name() }}.fitBounds(
{{ this.bounds|tojson }},
{{ this.options|tojson }}
);
(function (map) {
function fit() {
map.fitBounds(
{{ this.bounds|tojson }},
{{ this.options|tojson }}
);
}
fit();
// A map that is hidden while it loads, like a notebook cell
// outside the viewport or an inactive tab, has no size yet,
// so fitBounds zooms all the way out. Fit again once the map
// gets a size.
var container = map.getContainer();
if (
!(container.clientWidth && container.clientHeight)
&& typeof ResizeObserver !== "undefined"
) {
var observer = new ResizeObserver(function () {
if (container.clientWidth && container.clientHeight) {
observer.disconnect();
map.invalidateSize();
fit();
}
});
observer.observe(container);
}
})({{ this._parent.get_name() }});
{% endmacro %}
""")

Expand Down
40 changes: 40 additions & 0 deletions tests/selenium/test_fit_bounds_selenium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from selenium.webdriver.support.ui import WebDriverWait

import folium
from folium.utilities import temp_html_filepath


def test_fit_bounds_on_hidden_map(driver):
"""fit_bounds must apply once a map that was hidden while loading is shown.

A map without a size, for example in a notebook cell outside the
viewport, can't be fitted. Leaflet then zooms all the way out, and the
map stays like that when it becomes visible.

https://github.com/python-visualization/folium/issues/2080
"""
m = folium.Map()
m.get_root().header.add_child(
folium.Element("<style id='hide'>.folium-map { display: none; }</style>")
)
m.fit_bounds([[41.85, 12.40], [41.95, 12.60]])

html = m.get_root().render()
with temp_html_filepath(html) as filepath:
driver.get_file(filepath)
driver.verify_js_logs()
name = m.get_name()
get_zoom = f"return {name}.getZoom();"
# While hidden the map can't be fitted and is zoomed out completely.
assert driver.execute_script(get_zoom) == 0

driver.execute_script("document.getElementById('hide').remove();")
driver.wait_until(".folium-map")
WebDriverWait(driver, timeout=10).until(
lambda d: d.execute_script(get_zoom) > 0
)
expected_zoom = driver.execute_script(
f"return {name}.getBoundsZoom([[41.85, 12.40], [41.95, 12.60]]);"
)
assert driver.execute_script(get_zoom) == expected_zoom
driver.verify_js_logs()
4 changes: 3 additions & 1 deletion tests/test_folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def setup_method(self):
{% endif %}
{% endif %}
{% if bounds %}
{{this._parent.get_name()}}.fitBounds({{ bounds }},
map.fitBounds({{ bounds }},
{{ fit_bounds_options }}
);
{% endif %}
Expand Down Expand Up @@ -439,6 +439,7 @@ def test_fit_bounds(self):
)

assert "".join(fit_bounds_rendered.split()) in "".join(out.split())
assert f"}})({self.m.get_name()});" in "".join(out.split())

def test_fit_bounds_2(self):
bounds = ((52.193636, -2.221575), (52.636878, -1.139759))
Expand All @@ -465,6 +466,7 @@ def test_fit_bounds_2(self):
)

assert "".join(fit_bounds_rendered.split()) in "".join(out.split())
assert f"}})({self.m.get_name()});" in "".join(out.split())

bounds = self.m.get_bounds()
assert bounds == [[None, None], [None, None]], bounds
Expand Down
Loading