folium
folium copied to clipboard
fit_bounds seems to fail when multiple maps/cells are rendered in a notebook
Describe the bug Since about September of 2024 I've noticed that folium seems to sometimes fail to correctly "apply" fit_bounds to maps when multiple maps are rendered back-to-back. Manually running map generation cells a second time seems to cause the map to zoom/fit correctly. Have others experienced this?
To Reproduce
[update] I'm happy to report that I now have a concise notebook the reproduces this issue for me consistently.
FitBoundsTest.txt Rename as .ipynb
The issue seems to be related to whether the call to the map rendering function is on or off screen at the time it gets executed. In my browsers, calling "restart kernel and run all" on this notebook causes the notebook to scroll to the start of the last cell where I have some filler_text which fills the screen, yet the last call to test() is barely visible. If I don't scroll during the execution of the cells, once the run is complete, when I do scroll up almost always the first map has not "zoomed" (fit to bounds correctly),
Sometimes the second map is also not "zoomed" correctly, but always the third is. If I remove the filler text (or move it to the top) and restart and run all again, all 3 maps "zoom" correctly. Similarly, if I scroll up to the calls to test() while it is "running all", it also succeeds every time. And, if I manually re-run a single cell that failed to zoom correctly during run all, it then zooms correctly. I hope that helps find and fix the issue. Please let me know how I can be of more assistance.
Expected behavior All maps correctly fit to bounds as expected.
Environment (please complete the following information):
Current setup, though I've seen it in other settings as well. I believe on windows.
- Browser: Firefox and Chrome
- Jupyter Notebook or html files? Jupyter
- Python version: 3.11.8
- folium version: 0.19.4
- branca version: 0.8.1
Additional context Never saw this before the end of September 2024, but now see it many places.
Possible solutions ?
folium is maintained by volunteers. Can you help making a fix for this issue? Maybe
Do you have some example code? It would really help to reproduce the issue even if only intermittently.
I have some example code now added to the original post. Thank you for looking into this, and let me know how I can help further.
Has anyone been able to reproduce this issue?
@bdnewto Sorry, I missed your reply. I will have a look at your issue over the weekend.
@bdnewto I did a quick check with your notebook, but I was unable to reproduce your issue. For me all three maps are rendered correctly. I tried a couple of times. Here's what I did.
- Start the notebook.
- Manually clicked though the cells.
- Scroll to the top of the page.
- Restart kernel and run all cells
- Scroll to the top of the page.
- Restart kernel and run all cells
Any suggestions what we might try next?
Below is a link to a screen capture showing the issue on Firefox 128.7.0esr, with Jupyter 7.2.1 and Folium 0.19.4.
Depending on how it is run sometimes the map that fails to render changes, but one or two of them consistently fail for me using this notebook. Same issue is present on Edge 133.0.3065.69.
Hans, Thank you so much for looking into this!
I'm hopeful the video I sent a link to will help you see how the issue appears to me. I'm wondering if it is specific to certain Jupyter versions. Let me know if you have further questions or the video doesn't help.
Ben
From: Hans Then @.> Sent: Friday, February 7, 2025 9:03 AM To: python-visualization/folium @.> Cc: Newton, Ben @.>; Mention @.> Subject: [EXTERNAL] Re: [python-visualization/folium] fit_bounds seems to fail when multiple maps/cells are rendered in a notebook (Issue #2080)
You don't often get email from @.*** Learn why this is importanthttps://aka.ms/LearnAboutSenderIdentification
@bdnewtohttps://github.com/bdnewto I did a quick check with your notebook, but I was unable to reproduce your issue. For me all three maps are rendered correctly. I tried a couple of times. Here's what I did.
- Start the notebook.
- Manually clicked though the cells.
- Scroll to the top of the page.
- Restart kernel and run all cells
- Scroll to the top of the page.
- Restart kernel and run all cells
Any suggestions what we might try next?
Reply to this email directly, view it on GitHubhttps://github.com/python-visualization/folium/issues/2080#issuecomment-2643336552, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AWSAW7PHRXCZ7S4G4CQ4PVD2OTKKJAVCNFSM6AAAAABVIO6PXSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNBTGMZTMNJVGI. You are receiving this because you were mentioned.Message ID: @.***>
Thanks for the video. I am still baffled what could be causing this. I am using jupyter 7.2.2, Brave Browser 130.1.71.123, and folium 0.19.5.
I don't have much time to investigate this especially, but perhaps I can suggest a workaround.
In a recent version of Folium we added map event handlers via the on method. You could intercept the load method and using a JsCode object (in javascript) make the map object do something like map.fitBounds(map.getBounds()).