Skip to main content
Event data classes provide additional information about events that triggered a listener. When added as a type hint to an event listener function parameter, the corresponding EventData object will automatically be passed.

EventData

gr.EventData
The base event data class. Contains information about the event that triggered the listener.

Attributes

target
Block | None
The component object that triggered the event. Can be used to distinguish multiple components bound to the same listener.

Example

import gradio as gr

with gr.Blocks() as demo:
    table = gr.Dataframe([[1, 2, 3], [4, 5, 6]])
    gallery = gr.Gallery([("cat.jpg", "Cat"), ("dog.jpg", "Dog")])
    textbox = gr.Textbox("Hello World!")
    statement = gr.Textbox()
    
    def on_select(value, evt: gr.EventData):
        return f"The {evt.target} component was selected, and its value was {value}."
    
    table.select(on_select, table, statement)
    gallery.select(on_select, gallery, statement)
    textbox.select(on_select, textbox, statement)

demo.launch()

SelectData

gr.SelectData
Carries information about the .select() event.

Attributes

target
Block | None
The component object that triggered the event.
index
Any
The index of the selected item. Is a tuple if the component is two dimensional or selection is a range.
value
Any
The value of the selected item.
row_value
Any
The value of the entire row that the selected item belongs to, as a 1-D list. Only implemented for the Dataframe component, returns None for other components.
col_value
Any
The value of the entire column that the selected item belongs to, as a 1-D list. Only implemented for the Dataframe component, returns None for other components.
selected
bool
True if the item was selected, False if deselected.

Example

import gradio as gr

with gr.Blocks() as demo:
    table = gr.Dataframe([[1, 2, 3], [4, 5, 6]])
    statement = gr.Textbox()
    
    def on_select(evt: gr.SelectData):
        return f"You selected {evt.value} at {evt.index} from {evt.target}"
    
    table.select(on_select, table, statement)

demo.launch()

LikeData

gr.LikeData
Carries information about the .like() event.

Attributes

target
Block | None
The component object that triggered the event.
index
int | tuple[int, int]
The index of the liked/disliked item. Is a tuple if the component is two dimensional.
value
Any
The value of the liked/disliked item.
liked
bool | str
True if the item was liked, False if disliked, or string value if any other feedback.

Example

import gradio as gr

def test(value, like_data: gr.LikeData):
    return {
        "chatbot_value": value,
        "liked_message": like_data.value,
        "liked_index": like_data.index,
        "liked_or_disliked": like_data.liked
    }

with gr.Blocks() as demo:
    c = gr.Chatbot([("abc", "def")])
    t = gr.JSON()
    c.like(test, c, t)

demo.launch()

KeyUpData

gr.KeyUpData
Carries information about the .key_up() event.

Attributes

target
Block | None
The component object that triggered the event.
key
str
The key that was pressed.
input_value
str
The displayed value in the input textbox after the key was pressed. This may be different than the value attribute of the component itself.

DeletedFileData

gr.DeletedFileData
Carries information about the .delete() event.

Attributes

target
Block | None
The component object that triggered the event.
file
FileData
The file that was deleted, as a FileData object.

RetryData

gr.RetryData
Carries information about the .retry() event.

Attributes

target
Block | None
The component object that triggered the event.
index
int | tuple[int, int]
The index of the user message that should be retried.
value
Any
The value of the user message that should be retried.

UndoData

gr.UndoData
Carries information about the .undo() event.

Attributes

target
Block | None
The component object that triggered the event.
index
int | tuple[int, int]
The index of the user message that should be undone.
value
Any
The value of the user message that should be undone.

EditData

gr.EditData
Carries information about the .edit() event.

Attributes

target
Block | None
The component object that triggered the event.
index
int | tuple[int, int]
The index of the message that was edited.
previous_value
Any
The previous content of the message that was edited.
value
Any
The new content of the message that was edited.

DownloadData

gr.DownloadData
Carries information about the .download() event.

Attributes

target
Block | None
The component object that triggered the event.
file
FileData
The file that was downloaded, as a FileData object.

CopyData

gr.CopyData
Carries information about the .copy() event.

Attributes

target
Block | None
The component object that triggered the event.
value
Any
The value that was copied.

Build docs developers (and LLMs) love