When building Gradio applications, you often need to persist data between interactions. This is called state management. Gradio provides three ways to manage state:Documentation Index
Fetch the complete documentation index at: https://mintlify.com/gradio-app/gradio/llms.txt
Use this file to discover all available pages before exploring further.
- 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:Create a gr.State object
Initialize it with a default value if needed. The value must be deepcopy-able.
Add State as input/output
Include the State object in your event listener’s
inputs and outputs as needed.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: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.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
| State Type | Shared Between Users | Persists on Refresh | Persists After Browser Close | Use Cases |
|---|---|---|---|---|
| Global State | ✅ Yes | ✅ Yes (until app restarts) | ✅ Yes (until app restarts) | Visitor counters, global settings, shared resources |
| Session State | ❌ No | ❌ No | ❌ No | Chat history, form data, user progress |
| Browser State | ❌ No | ✅ Yes | ✅ Yes | User preferences, API keys, login credentials |
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