- Global state: Shared among all users, persists while the app is running
- Session state: Unique to each user, persists during their session
- Browser state: Stored in the browser’s localStorage, persists even after page refresh
Global state
Global state is straightforward: any variable created outside a function is shared between all users.Session state
Session state allows you to persist data for each user individually. Data is maintained across multiple interactions but does not transfer between users. If a user refreshes the page, their session state is reset. To use session state, follow these steps:1
Create a gr.State object
Initialize it with a default value if needed. The value must be deepcopy-able.
2
Add State as input/output
Include the State object in your event listener’s
inputs and outputs as needed.3
Use State in your function
Add the state variable to your function’s parameters and return value.
Example: Shopping cart
Here’s a complete example of a simple shopping cart:Think of
gr.State as an invisible Gradio component that can store any value. In this example, cart is not visible in the UI but is used for calculations.Understanding State changes
The.change() listener for a State variable triggers after any event listener modifies the state:
- For sequences (
list,set,dict): Triggers if any elements change - For objects or primitives: Triggers if the hash of the value changes
State persistence
Session state values:- Are cleared when the user refreshes the page
- Are stored in the app backend for 60 minutes after the user closes the tab
- Can have custom retention with the
delete_cacheparameter ingr.Blocks
Learn more about
State in the State documentation.Working with non-deepcopyable objects
Some objects cannot be deepcopied (like threading locks or database connections). For these, use a global dictionary keyed by session hash:1
Create a global dictionary
Store one instance per user, keyed by their
session_hash.2
Initialize on load
Use
demo.load() to create an instance when the user first visits.3
Clean up on unload
Use
demo.unload() to delete the instance when the user leaves.4
Access via session_hash
Use
gr.Request to get the user’s session_hash in your functions.Browser state
Browser state persists data in the browser’s localStorage, allowing it to survive page refreshes and even browser restarts. This is useful for storing user preferences, API keys, or settings.1
Create a gr.BrowserState object
Optionally provide a default value and a storage key.
2
Use like gr.State
Add it to event listeners as inputs and outputs.
Example: Persisting login credentials
BrowserState persistence
Important: Data stored in
gr.BrowserState does not persist if the Gradio app is restarted, unless you:- Hardcode specific values for
storage_keyandsecretingr.BrowserState - Restart the Gradio app on the same server name and port
Choosing the right state type
Next steps
Now that you understand state management, you can:- Build dynamic apps with the render decorator
- Learn about Blocks and event listeners
- Explore custom HTML components for advanced UIs