State Management Explained
Key Concepts
- Session State: Persistent data storage during a user session.
- Query Parameters: URL parameters that can be used to store state.
- Cookies: Small pieces of data stored on the client-side.
- Local Storage: Persistent data storage on the client-side.
- Global State: Shared state across multiple sessions or users.
Session State
Session state in Streamlit refers to data that persists during a user's session. This data is stored in memory and is lost when the session ends. It is useful for storing temporary data that needs to be accessed throughout the session.
import streamlit as st if 'counter' not in st.session_state: st.session_state.counter = 0 st.session_state.counter += 1 st.write(f"Counter: {st.session_state.counter}")
Query Parameters
Query parameters are URL parameters that can be used to store state. They are useful for sharing state between different pages or applications. Streamlit allows you to access and modify query parameters using the st.experimental_get_query_params and st.experimental_set_query_params functions.
import streamlit as st params = st.experimental_get_query_params() if 'name' in params: st.write(f"Hello, {params['name'][0]}!") else: st.write("Hello, World!")
Cookies
Cookies are small pieces of data stored on the client-side. They can be used to store state that persists across sessions. Streamlit does not natively support cookies, but you can use external libraries like requests or http.cookies to manage cookies.
import streamlit as st import http.cookies cookie = http.cookies.SimpleCookie() cookie['user'] = 'John Doe' st.write(f"User: {cookie['user'].value}")
Local Storage
Local storage is a client-side storage mechanism that allows you to store data persistently. Unlike cookies, local storage does not have an expiration time and can store larger amounts of data. You can use JavaScript to interact with local storage in Streamlit.
import streamlit as st st.write(""" """, unsafe_allow_html=True)
Global State
Global state refers to shared state across multiple sessions or users. This is useful for applications that need to maintain state across different users or sessions. You can use external databases or services like Redis to manage global state in Streamlit.
import streamlit as st import redis r = redis.Redis(host='localhost', port=6379, db=0) r.set('global_counter', 0) global_counter = int(r.get('global_counter')) st.write(f"Global Counter: {global_counter}")
Examples
Example 1: Session State
import streamlit as st if 'counter' not in st.session_state: st.session_state.counter = 0 st.session_state.counter += 1 st.write(f"Counter: {st.session_state.counter}")
Example 2: Query Parameters
import streamlit as st params = st.experimental_get_query_params() if 'name' in params: st.write(f"Hello, {params['name'][0]}!") else: st.write("Hello, World!")
Example 3: Cookies
import streamlit as st import http.cookies cookie = http.cookies.SimpleCookie() cookie['user'] = 'John Doe' st.write(f"User: {cookie['user'].value}")
Example 4: Local Storage
import streamlit as st st.write(""" """, unsafe_allow_html=True)
Example 5: Global State
import streamlit as st import redis r = redis.Redis(host='localhost', port=6379, db=0) r.set('global_counter', 0) global_counter = int(r.get('global_counter')) st.write(f"Global Counter: {global_counter}")
Analogies
Think of session state as a notepad that you use during a meeting. It holds temporary notes that are discarded after the meeting. Query parameters are like sticky notes attached to a document, allowing you to pass information between different documents. Cookies are like small reminders you write on your hand, which you can check later. Local storage is like a notebook you keep at home, where you store important information that you can access anytime. Global state is like a shared whiteboard in a conference room, where everyone can see and update the information.
By mastering state management in Streamlit, you can create more dynamic and interactive applications that maintain state across different sessions and users, enhancing the overall user experience.