What are events?
In Gradio, events are triggered by user interactions with components. When an event occurs, Gradio can:- Run a Python function
- Pass component values as inputs to that function
- Update other components with the function’s outputs
Basic event structure
Every event listener follows this pattern: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:evt.index- Index of selected item (or tuple for 2D components)evt.value- Value of selected itemevt.selected- True if selected, False if deselectedevt.target- The component that triggered the event
LikeData
For chatbot like/dislike events:evt.index- Index of the liked/disliked messageevt.value- Content of the messageevt.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
Usegr.on() to trigger a function from multiple events:
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:False to work with raw component data:
Generator functions
Use generator functions for streaming outputs: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
Event best practices
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