Skip to main content
Gradio provides several event listener functions that can be used to attach multiple events to a single function.

on

Sets up an event listener that triggers a function when the specified event(s) occur. This is especially useful when the same function should be triggered by multiple events. Only a single API endpoint is generated for all events in the triggers list.

Parameters

Sequence[EventListenerCallable] | EventListenerCallable | None
default:"None"
List of triggers to listen to, e.g. [btn.click, number.change]. If None, will run on app load and changes to any inputs.
Callable | None
default:"None"
The function to call when this event is triggered.
Component | Sequence[Component] | None
default:"None"
List of gradio components to use as inputs.
Component | Sequence[Component] | None
default:"None"
List of gradio components to use as outputs.

Example

api

Sets up an API or MCP endpoint for a generic function without needing to define event listeners or components. Derives its typing from type hints in the provided function’s signature rather than the components.

Parameters

Callable
required
The function to call when this event is triggered. The function should be fully typed, and the type hints will be used to derive the typing information for the API/MCP endpoint.
str | None
default:"None"
Defines how the endpoint appears in the API docs.
str | None
default:"None"
Description of the API endpoint.
bool
default:"True"
If True, will place the request on the queue.
bool
default:"False"
If True, then the function should process a batch of inputs.
int
default:"4"
Maximum number of inputs to batch together.
int | None | Literal['default']
default:"'default'"
If set, this is the maximum number of this event that can be running simultaneously.
str | None
default:"None"
If set, this is the id of the concurrency group.
Literal['public', 'private', 'undocumented']
default:"'public'"
Controls the visibility and accessibility of this endpoint.

Example