Skip to main content
When building Gradio applications, you often need to persist data between interactions. This is called state management. Gradio provides three ways to manage state:
  • 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.
Every user who opens the app increases the visitor count. The value is shared globally across all sessions.
When not to use global state: If you need values that are unique to each user (like chat history, user preferences, or individual progress), use session state or browser state instead.

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
If you create a custom class and use it with gr.State, make sure the class includes a sensible __hash__ implementation.

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_cache parameter in gr.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:
  1. Hardcode specific values for storage_key and secret in gr.BrowserState
  2. Restart the Gradio app on the same server name and port
Only do this if you’re running trusted Gradio apps, as different apps on the same domain could access the same localStorage data.

Choosing the right state type

Next steps

Now that you understand state management, you can: