Repository navigation
Refit bounds once a hidden map gets a size - #2360
Open
KhadeerBasha1232 wants to merge 1 commit into
Open
KhadeerBasha1232 wants to merge 1 commit into
KhadeerBasha1232 wants to merge 1 commit into
Conversation
A map that is hidden while it loads, like a notebook cell outside the viewport in JupyterLab 4.2+, has no size, so fitBounds zooms all the way out and the map stays like that. Watch the container and fit again once it has a size. Closes python-visualization#2080 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch was successfully deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refit bounds once a hidden map gets a size
Closes #2080
The problem
In a notebook,
fit_boundssometimes has no effect. The map shows the wholeworld instead of the requested area. It depends on scrolling: maps whose cell
is outside the viewport while the notebook runs come out wrong.
The cause
Since JupyterLab 4.2 (and Notebook 7.2, which is built on it), the default
windowingModeis"full". Cells outside the viewport getdisplay: none.A folium map rendered in such a cell has a size of 0×0 when its script runs.
Leaflet can't fit bounds into a map with no size, so
fitBoundsfalls back tozoom 0. When the cell is shown later, Leaflet updates the map size but doesn't
fit again, so the map stays zoomed out.
That explains the details in the issue: it started around autumn 2024, it
depends on what's on screen while the cells run, and re-running a single
visible cell fixes it.
The same thing happens outside notebooks, for any map that is hidden when the
page loads, such as a map in an inactive tab.
The fix
FitBoundsstill callsfitBoundsright away. If the map container has nosize at that moment, a
ResizeObserverwatches the container. As soon as ithas a width and height, the observer disconnects, calls
invalidateSize(),and fits the bounds again.
created for them.
zooms isn't snapped back.
Tests
test_fit_bounds_on_hidden_map. It renders a map withdisplay: none, checks that it's at zoom 0, shows it, then checks that thezoom equals Leaflet's
getBoundsZoomfor the requested bounds.test_fit_boundsandtest_fit_bounds_2are updated for the new template.How I checked it
In JupyterLab 4.2.5, with a notebook that defines a
test()helper(
folium.Map()plus a marker andfit_boundson Rome), prints 400 lines offiller, then calls
test()in three cells. I used "Restart Kernel and Run AllCells" without scrolling, then scrolled down to the maps:
main, right after runningdisplay: none, size 0×0, zoom 0main, after scrolling to themMaps 2 and 3 after scrolling down to them:
mainIn standalone pages, with three maps in a row:
maindisplay: none, then showncontent-visibility: auto, scrolled into viewNo console errors in any of these.