Streamlit State in Snowflake: Sync Every Chart with Query Params and Local Storage
In this walkthrough, Rob Dominguez, Data Platform Architect at Snowflake, explains how session state works and he's covering two important topics. The first is query parameters, which work with no extra dependencies. The key/value pair lives in the URL, the app parses it on load, initializes one variable from it, and every component reads that same variable. Change the selector and the value cascades back out to all four charts. Because the state lives in the URL, you can copy the link and hand it to anyone with access and they get the same view. The second is local storage, which fits user preferences better than shared links. It needs a Python package, which inside Snowflake means an external access integration so the package repository can be reached. The URL stays clean, the key/value pair lives in the browser instead, and you can watch it in the developer tools under Application. That's also where preferences like a saved theme or a default sidebar tab end up, scoped to that person's machine and browser. 📖 Read Rob's blog post: https://medium.com/@rob.dominguez_27061/state-and-streamlit-43a7f99e10d3 🐙 Code repository: https://github.com/sfc-gh-rdominguez/streamlit-state-persistence-demo/tree/main ⏳ CHAPTERS 0:00 Why Streamlit state keeps coming up 1:08 The whiteboard example 1:20 Four charts, four independent states 1:56 Option 1: query parameters 2:41 How a query param becomes one shared variable 3:12 Sharing state by sharing the URL 3:30 Option 2: local storage 4:26 Finding local storage in DevTools 5:00 Demo: the unsynced app 5:30 Demo: syncing with query params 5:49 Demo: syncing with local storage 6:15 Wrap-up 📚 DOCUMENTATION st.query_params (Streamlit API reference) https://docs.streamlit.io/develop/api-reference/caching-and-state/st.query_params Session State (Streamlit API reference) https://docs.streamlit.io/develop/api-reference/caching-and-state/st.session_state Manage dependencies for your Streamlit app (Streamlit in Snowflake) https://docs.snowflake.com/en/developer-guide/streamlit/app-development/dependency-management External network access in Streamlit in Snowflake https://docs.snowflake.com/en/developer-guide/streamlit/features/external-access #Streamlit #Snowflake #DataApps #Python