Read through a number of streamlit-folium posts and the documentation…is there a method to scroll around on the map, but not have to reload the page every time?
For example, lets look at the Dynamic Updates documentation page. Each time a user zooms or scrolls on the map, the entire page reloads.
A colleague suggested that if there was a key parameter that could help avoid having to reload the entire page. I’ve found examples using the key parameter, but have not been able to find any documentation on how it works or how/when to use it.
One effective approach is to leverage st.session_state to preserve the state of the map and other parts of your application between reruns. By doing this, you can avoid unnecessary recalculations or reloads of data that haven’t changed.
import streamlit as st
import folium
from streamlit_folium import folium_static
from folium.plugins import MarkerCluster
def create_map():
if 'map' not in st.session_state or st.session_state.map is None:
m = folium.Map(location=[45.372, -121.6972], zoom_start=12, tiles="Stamen Terrain")
marker_cluster = MarkerCluster().add_to(m)
folium.Marker(location=[45.372, -121.6972], popup="Mt. Hood Meadows").add_to(marker_cluster)
st.session_state.map = m # Save the map in the session state
return st.session_state.map
def show_map():
m = create_map() # Get or create the map
folium_static(m)
show_map()
Hope this helps!
Kind Regards,
Sahir Maharaj
Data Scientist | AI Engineer
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.