Skip to content

Refit bounds once a hidden map gets a size - #2360

Open
KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:fit-bounds-offscreen
Open

KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:fit-bounds-offscreen

Conversation

@KhadeerBasha1232

@KhadeerBasha1232 KhadeerBasha1232 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Refit bounds once a hidden map gets a size

Closes #2080

The problem

In a notebook, fit_bounds sometimes has no effect. The map shows the whole
world 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
windowingMode is "full". Cells outside the viewport get display: 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 fitBounds falls back to
zoom 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

FitBounds still calls fitBounds right away. If the map container has no
size at that moment, a ResizeObserver watches the container. As soon as it
has a width and height, the observer disconnects, calls invalidateSize(),
and fits the bounds again.

  • Maps that are visible on load behave exactly as before. No observer is
    created for them.
  • The observer runs once and then disconnects, so a user who later pans or
    zooms isn't snapped back.
  • The code is wrapped in an IIFE, so no extra globals are added to the page.

Tests

  • New Selenium test test_fit_bounds_on_hidden_map. It renders a map with
    display: none, checks that it's at zoom 0, shows it, then checks that the
    zoom equals Leaflet's getBoundsZoom for the requested bounds.
  • test_fit_bounds and test_fit_bounds_2 are 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 and fit_bounds on Rome), prints 400 lines of
filler, then calls test() in three cells. I used "Restart Kernel and Run All
Cells" without scrolling, then scrolled down to the maps:

Map 1 (in view) Maps 2 and 3 (out of view while running)
main, right after running zoom 11 display: none, size 0×0, zoom 0
main, after scrolling to them zoom 11 size 557×335, still zoom 0 (whole world)
This PR, after scrolling to them zoom 11 zoom 11, fitted to Rome

Maps 2 and 3 after scrolling down to them:

main This PR
Maps 2 and 3 on main: zoomed out to the whole world Maps 2 and 3 with this PR: fitted to Rome

In standalone pages, with three maps in a row:

Case main This PR
Visible on load zoom 12 zoom 12
Inside display: none, then shown zoom 0 zoom 12
Inside content-visibility: auto, scrolled into view zoom 0 zoom 12
Top-level page, map hidden, then shown zoom 0 zoom 12

No console errors in any of these.

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

1 active deployment
pypi — 837b947d Deployed Nov 19, 2026 by KhadeerBasha1232 via packages #2114
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fit_bounds seems to fail when multiple maps/cells are rendered in a notebook

1 participant