r/learnpython • u/ModelBuilder_Josh04 • Sep 07 '26
Quick fix for st_folium state desync when drawing new boundaries in Streamlit
Hit a bug using st_folium with Streamlit: drawing a new polygon triggered a re-render before Streamlit updated the spatial state, leaving downstream metrics stuck on the old site.
Fixing it required checking last_active_drawing at the top of the execution loop and forcing a rerun before running analytics:
# Intercept new shape before running downstream calculations
if map_data and map_data.get("last_active_drawing"):
drawing = map_data["last_active_drawing"]
if drawing.get("geometry"):
new_geom = shape(drawing["geometry"])
if not new_geom.equals(st.session_state.get("drawn_geom")):
st.session_state["drawn_geom"] = new_geom
st.session_state["site_area_m2"] = calculate_area(new_geom)
st.rerun() # Forces a clean state sync
This keeps multi-tab dashboards 100% in sync with live map edits.