Skip to main content
Events are the mechanism that makes Gradio applications interactive. An event listener connects a component interaction (like a button click) to a Python function, allowing you to respond to user actions in real-time.

What are events?

In Gradio, events are triggered by user interactions with components. When an event occurs, Gradio can:
  1. Run a Python function
  2. Pass component values as inputs to that function
  3. Update other components with the function’s outputs
This creates the interactive loop that powers Gradio applications.

Basic event structure

Every event listener follows this pattern:
For example:

Common event types

Click events

Triggered when a button is clicked:

Change events

Triggered when a component’s value changes:
The .change() event fires both when users type AND when the value is updated programmatically. Use .input() for events triggered only by user input.

Submit events

Triggered when users press Enter in a textbox:

Input events

Triggered only by direct user input (not programmatic updates):

Select events

Triggered when selecting items in components like Gallery, Dataframe, or highlighted text:

Event data objects

Some events provide additional context through event data classes:

SelectData

Contains information about selected items:
SelectData attributes:
  • evt.index - Index of selected item (or tuple for 2D components)
  • evt.value - Value of selected item
  • evt.selected - True if selected, False if deselected
  • evt.target - The component that triggered the event

LikeData

For chatbot like/dislike events:
LikeData attributes:
  • evt.index - Index of the liked/disliked message
  • evt.value - Content of the message
  • evt.liked - True if liked, False if disliked

KeyUpData

For key press events:

Event chaining

Chain events together using .then(), .success(), and .failure():

Sequential execution with .then()

Success and failure handlers

The gr.on() function

Use gr.on() to trigger a function from multiple events:
This is more efficient than setting up separate listeners - it creates only one API endpoint.

Event parameters

Controlling behavior

Trigger modes

  • “once” (default): Prevents new submissions while one is running
  • “multiple”: Allows unlimited simultaneous submissions
  • “always_last”: Queues the most recent submission, cancelling intermediate ones

Cancelling events

Cancel running events when a new event starts:

Preprocessing and postprocessing

Control how data flows through events:
Set to False to work with raw component data:

Generator functions

Use generator functions for streaming outputs:
Outputs update in real-time as the generator yields values.

JavaScript events

Run JavaScript before or after Python functions:

Load events

Run functions when the page loads:

Component-specific events

Different components support different events:
  • Button: click
  • Textbox: change, input, submit, focus, blur
  • Slider: change, release
  • File: upload, delete, download, change
  • Image: select, change, clear, upload
  • Chatbot: change, select, like, retry, undo, clear
  • Gallery: select, change
  • Dataframe: select, change, input
Check each component’s documentation for its supported events.

Event best practices

Performance: Use gr.on() instead of multiple individual listeners when the same function responds to multiple events.
Responsiveness: Use show_progress="minimal" or "hidden" for fast operations to reduce visual clutter.
State isolation: Remember that State components are session-specific. Different users don’t share state values.
Queueing: Long-running or generator functions require the queue to be enabled (it’s enabled by default).

See also

  • Interface - High-level event handling
  • Blocks - Advanced event patterns
  • Components - Which events each component supports
  • State - Managing persistent data across events