marimo
marimo copied to clipboard
Leafmap map does not rerender after code changes
Describe the bug
When writing Leafmap code to design maps in Marimo, I'm not able to get an updated version of the map rendered in the notebook without reloading the entire notebook. After making a code change, I rerun the cell and no update occurs. Needing to reload the entire notebook obviously makes the map development process even slower than a Jupyter notebook. I'd love to see this fixed as being able to run the notebook as an app straightaway makes the process of sharing maps extremely easy!
A simple code snippet that helps highlight the issue is the following. If you make changes to the viz height and rerun the cell, the resulting map does not change.
m = leafmap.Map(
center=(-98.5795,39.8283), # Center of the US
zoom=3, # Initial zoom level
height='500px'
)
m
Running this code with leafmap==0.39.2.
Environment
{ "marimo": "0.9.31", "OS": "Darwin", "OS Version": "23.6.0", "Processor": "i386", "Python Version": "3.11.10", "Binaries": { "Browser": "131.0.6778.109", "Node": "v23.2.0" }, "Dependencies": { "click": "8.1.7", "docutils": "0.21.2", "itsdangerous": "2.2.0", "jedi": "0.19.2", "markdown": "3.6", "narwhals": "1.13.5", "packaging": "24.2", "psutil": "6.1.0", "pygments": "2.18.0", "pymdown-extensions": "10.12", "pyyaml": "6.0.2", "ruff": "0.7.4", "starlette": "0.41.2", "tomlkit": "0.13.2", "typing-extensions": "4.12.2", "uvicorn": "0.32.0", "websockets": "12.0" }, "Optional Dependencies": { "anywidget": "0.9.13", "duckdb": "1.1.3", "pandas": "2.2.3", "pyarrow": "18.0.0" } }
Code to reproduce
import leafmap
# Making updates to the viz height and rerunning the cell leads to no update in the map
m = leafmap.Map(center=(-98.5795,39.8283), zoom=3, height='500px')
m
@diehl updates work for me, but i do see height not being respected. unsure but might be the internals of leafmap?
@mscolnick Roger that. FYI I noticed similarly when I was adding a logo to the map that my changes to the logo size were also not updating.
If you inspect your maps .to_html(), do you see the changes in there. If not, then it might be in the map, if you do see the changes, then marimo is probably responsible for its staleness.
This map is also the folium map (instead of the jupyter leaflet), so it may have a slight difference in features.
@mscolnick I just realized I left out two key details! I'm using the MapLibre backend. So when installing the package, I am usingpip install "leafmap[maplibre]". And the import is import leafmap.maplibregl as leafmap. Apologies for these omissions!
hey @diehl - happy to look at this. can you provide a full minimal repo? best would be a notebook run with marimo new --sandbox so the packages are also in the notebook
thank you @mscolnick! your message is so timely as I was thinking about circling back on this issue. being able to design maps interactively with Leafmap would be such a huge win! happy to help however I can make this happen. I'll circle back on this when I get some time to put this together! more soon...
@mscolnick as promised, here's a github repo that contains a Marimo notebook with a Leafmap example that will demonstrate how the map fails to rerender properly as you comment out elements of the code producing the map https://github.com/lightfield-ag/blm-sma-map hope this helps! please let me know if you need anything else. it will be a gamechanger to get this to work!
@diehl the marimo notebook looks empty in that repo at the moment
@mscolnick apologies for that! not sure why it didn't save. it's fixed now.
@diehl can you help explain what doesn't work? it looks like its rendering fine for me
@mscolnick The map renders just fine initially. The issue is that as you make changes to the code, the map will not update properly. If you comment out lines of the code that instantiate different elements of the map, the map will not rerender. When I first opened this issue, it was because the only way I could get the map to update properly was to reload the entire notebook, which makes the map development process slower than what you can accomplish in a Jupyter notebook.
I was re-running and commenting out the code, and it still re-rendered for me. What version of marimo are you on?
I'm on 0.11.26 now. What I'm noticing still with the latest version is that as I comment out key lines that add elements to the map, the map grays out a bit but it does not update. If I hit the button that reruns the cell manually, it rerenders so that is progress. But the comment action doesn't lead to the cell being automatically rerun successfully.
@diehl, the graying out is because the cell is now "stale" since it has been edited (which has always been around).
We don't re-run when you change code or comment our code. You need to re-run the cell by clicking the run button or hotkey.
But the comment action doesn't lead to the cell being automatically rerun successfully.
Yes, we don't do this because re-running on keystroke would be pretty jarring.
@mscolnick yeah that makes total sense in retrospect. I'm realizing that I misunderstood what "on cell change: autorun" meant, which is what led to my confusion. thank you for taking a look at this! I'm in a great position now to leverage Marimo for map development and publishing.
sounds good!
fwiw, this was a bug, but we have various improvements to the anywidget rendering (mostly by forcing re-renders/re-initialization), which did fix your original bug, hence the upgrading.
the "on cell change: autorun" is a bit confusing at face value, but the docs to explain it more can be found here.
@mscolnick that's great to hear! I read through those docs prior to responding to make sure I understood what was actually implied by the setting. thank you again for your timely responses and assistance here. I'm incredibly bullish on the work you all are doing and the impact it will have on accelerating the process of geospatial data science.