diff --git a/folium/map.py b/folium/map.py index e837b25774..08d55efbe6 100644 --- a/folium/map.py +++ b/folium/map.py @@ -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 %} """) diff --git a/tests/selenium/test_fit_bounds_selenium.py b/tests/selenium/test_fit_bounds_selenium.py new file mode 100644 index 0000000000..74e4c9dcad --- /dev/null +++ b/tests/selenium/test_fit_bounds_selenium.py @@ -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("") + ) + 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() diff --git a/tests/test_folium.py b/tests/test_folium.py index 36a8298c4c..3dfdab3a61 100644 --- a/tests/test_folium.py +++ b/tests/test_folium.py @@ -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 %} @@ -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)) @@ -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