We briefly described the Blocks class in the Quickstart as a way to build custom demos. Let's dive deeper.
Blocks and Event Listeners
We briefly described the Blocks class in the Quickstart as a way to build custom demos. Let's dive deeper.
Blocks Structure
Take a look at the demo below.
import gradio as gr
def greet(name):
return "Hello " + name + "!"
with gr.Blocks() as demo:
name = gr.Textbox(label="Name")
output = gr.Textbox(label="Output Box")
greet_btn = gr.Button("Greet")
greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet")
if __name__ == "__main__":
demo.launch()
- First, note the
with gr.Blocks() as demo:clause. The Blocks app code will be contained within this clause. - Next come the Components. These are the same Components used in
Interface. However, instead of being passed to some constructor, Components are automatically added to the Blocks as they are created within thewithclause. - Finally, the
click()event listener. Event listeners define the data flow within the app. In the example above, the listener ties the two Textboxes together. The Textboxnameacts as the input and Textboxoutputacts as the output to thegreetmethod. This dataflow is triggered when the Buttongreet_btnis clicked. Like an Interface, an event listener can take multiple inputs or outputs.
You can also attach event listeners using decorators - skip the fn argument and assign inputs and outputs directly:
import gradio as gr
with gr.Blocks() as demo:
name = gr.Textbox(label="Name")
output = gr.Textbox(label="Output Box")
greet_btn = gr.Button("Greet")
@greet_btn.click(inputs=name, outputs=output)
def greet(name):
return "Hello " + name + "!"
if __name__ == "__main__":
demo.launch()
Event Listeners and Interactivity
In the example above, you'll notice that you are able to edit Textbox name, but not Textbox output. This is because any Component that acts as an input to an event listener is made interactive. However, since Textbox output acts only as an output, Gradio determines that it should not be made interactive. You can override the default behavior and directly configure the interactivity of a Component with the boolean interactive keyword argument, e.g. gr.Textbox(interactive=True).
output = gr.Textbox(label="Output", interactive=True)
Note: What happens if a Gradio component is neither an input nor an output? If a component is constructed with a default value, then it is presumed to be displaying content and is rendered non-interactive. Otherwise, it is rendered interactive. Again, this behavior can be overridden by specifying a value for the interactive argument.
Types of Event Listeners
Take a look at the demo below:
import gradio as gr
def welcome(name):
return f"Welcome to Gradio, {name}!"
with gr.Blocks() as demo:
gr.Markdown(
"""
# Hello World!
Start typing below to see the output.
""")
inp = gr.Textbox(placeholder="What is your name?")
out = gr.Textbox()
inp.change(welcome, inp, out)
if __name__ == "__main__":
demo.launch()
Instead of being triggered by a click, the welcome function is triggered by typing in the Textbox inp. This is due to the change() event listener. Different Components support different event listeners. For example, the Video Component supports a play() event listener, triggered when a user presses play. See the Docs for the event listeners for each Component.
Multiple Data Flows
A Blocks app is not limited to a single data flow the way Interfaces are. Take a look at the demo below:
import gradio as gr
def increase(num):
return num + 1
with gr.Blocks() as demo:
a = gr.Number(label="a")
b = gr.Number(label="b")
atob = gr.Button("a > b")
btoa = gr.Button("b > a")
atob.click(increase, a, b)
btoa.click(increase, b, a)
if __name__ == "__main__":
demo.launch()
Note that num1 can act as input to num2, and also vice-versa! As your apps get more complex, you will have many data flows connecting various Components.
Here's an example of a "multi-step" demo, where the output of one model (a speech-to-text model) gets fed into the next model (a sentiment classifier).
from transformers import pipeline
import gradio as gr
asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
classifier = pipeline("text-classification")
def speech_to_text(speech):
text = asr(speech)["text"] # type: ignore
return text
def text_to_sentiment(text):
return classifier(text)[0]["label"] # type: ignore
demo = gr.Blocks()
with demo:
audio_file = gr.Audio(type="filepath")
text = gr.Textbox()
label = gr.Label()
b1 = gr.Button("Recognize Speech")
b2 = gr.Button("Classify Sentiment")
b1.click(speech_to_text, inputs=audio_file, outputs=text)
b2.click(text_to_sentiment, inputs=text, outputs=label)
if __name__ == "__main__":
demo.launch()
Function Input List vs Set
The event listeners you've seen so far have a single input component. If you'd like to have multiple input components pass data to the function, you have two options on how the function can accept input component values:
- as a list of arguments, or
- as a single dictionary of values, keyed by the component
Let's see an example of each:
import gradio as gr
with gr.Blocks() as demo:
a = gr.Number(label="a")
b = gr.Number(label="b")
with gr.Row():
add_btn = gr.Button("Add")
sub_btn = gr.Button("Subtract")
c = gr.Number(label="sum")
def add(num1, num2):
return num1 + num2
add_btn.click(add, inputs=[a, b], outputs=c)
def sub(data):
return data[a] - data[b]
sub_btn.click(sub, inputs={a, b}, outputs=c)
if __name__ == "__main__":
demo.launch()
Both add() and sub() take a and b as inputs. However, the syntax is different between these listeners.
- To the
add_btnlistener, we pass the inputs as a list. The functionadd()takes each of these inputs as arguments. The value ofamaps to the argumentnum1, and the value ofbmaps to the argumentnum2. - To the
sub_btnlistener, we pass the inputs as a set (note the curly brackets!). When you pass a set, the functionsub()receives a single dictionary argumentdata, where the keys are the input components and the values are the values of those components.
It is a matter of preference which syntax you prefer! For functions with many input components, option 2 may be easier to manage.
Passing Inputs by Parameter Name
Use inputs_kwargs when a component value should be passed to an event function by parameter name instead of by position. This is especially useful when calling an existing function with keyword-only parameters: the function can be connected directly without writing a wrapper just to rearrange its arguments.
import gradio as gr
def greet(first_name: str, *, last_name: str) -> str:
return f"Hello, {first_name} {last_name}!"
with gr.Blocks() as demo:
first_name = gr.Textbox(label="First name")
last_name = gr.Textbox(label="Last name")
greeting = gr.Textbox(label="Greeting")
greet_button = gr.Button("Greet", variant="primary")
greet_button.click(
greet,
inputs=[first_name],
inputs_kwargs={"last_name": last_name},
outputs=greeting,
)
if __name__ == "__main__":
demo.launch()
In this example, first_name is passed positionally through inputs, while the last_name component is mapped to the keyword-only last_name parameter. Each key in inputs_kwargs must match a parameter accepted by the event function. Keyword inputs can be combined with a component or list of components in inputs, but not with the set syntax described above, since a set already passes all input values together as one component-keyed dictionary.
If the event uses a validator, the validator receives values with the same positional and keyword mapping as the main function. Its signature must therefore accept every name used in inputs_kwargs; if it does not, the event raises a ValueError when it is defined.
One thing to keep in mind: a parameter annotated with a component type (such as tb: gr.Textbox) receives the full component instead of just its value, but only when Gradio can fill it positionally. A keyword-only parameter with a component annotation receives the plain value, so annotate such parameters with the value type they actually receive.
Named inputs are particularly helpful for larger forms because the mapping remains clear even when controls are arranged differently in the interface. The following live inventory dashboard connects five filter controls to explicit keyword-only parameters. Because gr.on() has no explicit triggers, it listens to changes from both inputs and inputs_kwargs, as well as the app's load event.
Código (104 líneas)
from typing import Any
import gradio as gr
INVENTORY: list[dict[str, Any]] = [
{"name": "Studio Headphones", "category": "Audio", "price": 149, "stock": 18},
{"name": "Podcast Microphone", "category": "Audio", "price": 89, "stock": 0},
{
"name": "Mechanical Keyboard",
"category": "Accessories",
"price": 129,
"stock": 7,
},
{"name": "Ergonomic Mouse", "category": "Accessories", "price": 79, "stock": 24},
{"name": "4K Monitor", "category": "Displays", "price": 499, "stock": 5},
{"name": "Portable Monitor", "category": "Displays", "price": 219, "stock": 0},
{"name": "Developer Laptop", "category": "Computers", "price": 1599, "stock": 3},
{"name": "Mini Desktop", "category": "Computers", "price": 749, "stock": 11},
{"name": "USB-C Dock", "category": "Accessories", "price": 189, "stock": 14},
{"name": "Conference Speaker", "category": "Audio", "price": 249, "stock": 6},
]
def filter_inventory(
query: str,
*,
category: str,
max_price: float,
in_stock_only: bool,
sort_by: str,
result_limit: int,
) -> tuple[list[list[Any]], str]:
"""Filter a product catalog while keeping every option explicit and named."""
normalized_query = query.strip().lower()
matches = [
item
for item in INVENTORY
if (not normalized_query or normalized_query in item["name"].lower())
and (category == "All" or item["category"] == category)
and item["price"] <= max_price
and (not in_stock_only or item["stock"] > 0)
]
if sort_by == "Price: low to high":
matches.sort(key=lambda item: item["price"])
elif sort_by == "Price: high to low":
matches.sort(key=lambda item: item["price"], reverse=True)
else:
matches.sort(key=lambda item: item["name"])
visible_matches = matches[: int(result_limit)]
rows = [
[item["name"], item["category"], item["price"], item["stock"]]
for item in visible_matches
]
status = (
f"Showing **{len(visible_matches)}** of **{len(matches)}** matching products."
)
return rows, status
with gr.Blocks() as demo:
gr.Markdown(
"# Inventory explorer\n"
"Search and filter a catalog with controls mapped to named function parameters."
)
search = gr.Textbox(label="Search", placeholder="Try 'monitor' or 'audio'")
with gr.Row():
category = gr.Dropdown(
["All", "Accessories", "Audio", "Computers", "Displays"],
value="All",
label="Category",
)
max_price = gr.Slider(0, 2000, value=2000, step=25, label="Maximum price")
in_stock_only = gr.Checkbox(value=False, label="In stock only")
with gr.Row():
sort_by = gr.Dropdown(
["Name", "Price: low to high", "Price: high to low"],
value="Name",
label="Sort by",
)
result_limit = gr.Slider(1, 10, value=10, step=1, label="Result limit")
results = gr.Dataframe(
headers=["Product", "Category", "Price ($)", "In stock"],
datatype=("str", "str", "number", "number"),
interactive=False,
)
status = gr.Markdown()
gr.on(
fn=filter_inventory,
inputs=[search],
inputs_kwargs={
"category": category,
"in_stock_only": in_stock_only,
"max_price": max_price,
"result_limit": result_limit,
"sort_by": sort_by,
},
outputs=[results, status],
trigger_mode="always_last",
)
if __name__ == "__main__":
demo.launch()Function Return List vs Dict
Similarly, you may return values for multiple output components either as:
- a list of values, or
- a dictionary keyed by the component
Let's first see an example of (1), where we set the values of two output components by returning two values:
with gr.Blocks() as demo:
food_box = gr.Number(value=10, label="Food Count")
status_box = gr.Textbox()
def eat(food):
if food > 0:
return food - 1, "full"
else:
return 0, "hungry"
gr.Button("Eat").click(
fn=eat,
inputs=food_box,
outputs=[food_box, status_box]
)
Above, each return statement returns two values corresponding to food_box and status_box, respectively.
Note: if your event listener has a single output component, you should not return it as a single-item list. This will not work, since Gradio does not know whether to interpret that outer list as part of your return value. You should instead just return that value directly.
Now, let's see option (2). Instead of returning a list of values corresponding to each output component in order, you can also return a dictionary, with the key corresponding to the output component and the value as the new value. This also allows you to skip updating some output components.
with gr.Blocks() as demo:
food_box = gr.Number(value=10, label="Food Count")
status_box = gr.Textbox()
def eat(food):
if food > 0:
return {food_box: food - 1, status_box: "full"}
else:
return {status_box: "hungry"}
gr.Button("Eat").click(
fn=eat,
inputs=food_box,
outputs=[food_box, status_box]
)
Notice how when there is no food, we only update the status_box element. We skipped updating the food_box component.
Dictionary returns are helpful when an event listener affects many components on return, or conditionally affects outputs and not others.
Keep in mind that with dictionary returns, we still need to specify the possible outputs in the event listener.
Updating Component Configurations
The return value of an event listener function is usually the updated value of the corresponding output Component. Sometimes we want to update the configuration of the Component as well, such as the visibility. In this case, we return a new Component, setting the properties we want to change.
import gradio as gr
def change_textbox(choice):
if choice == "short":
return gr.Textbox(lines=2, visible=True)
elif choice == "long":
return gr.Textbox(lines=8, visible=True, value="Lorem ipsum dolor sit amet")
else:
return gr.Textbox(visible=False)
with gr.Blocks() as demo:
radio = gr.Radio(
["short", "long", "none"], label="What kind of essay would you like to write?"
)
text = gr.Textbox(lines=2, interactive=True, buttons=["copy"])
radio.change(fn=change_textbox, inputs=radio, outputs=text)
if __name__ == "__main__":
demo.launch()
See how we can configure the Textbox itself through a new gr.Textbox() method. The value= argument can still be used to update the value along with Component configuration. Any arguments we do not set will preserve their previous values.
Not Changing a Component's Value
In some cases, you may want to leave a component's value unchanged. Gradio includes a special function, gr.skip(), which can be returned from your function. Returning this function will keep the output component (or components') values as is. Let us illustrate with an example:
import random
import gradio as gr
with gr.Blocks() as demo:
with gr.Row():
clear_button = gr.Button("Clear")
skip_button = gr.Button("Skip")
random_button = gr.Button("Random")
numbers = [gr.Number(), gr.Number()]
clear_button.click(lambda : (None, None), outputs=numbers)
skip_button.click(lambda : [gr.skip(), gr.skip()], outputs=numbers)
random_button.click(lambda : (random.randint(0, 100), random.randint(0, 100)), outputs=numbers)
if __name__ == "__main__":
demo.launch()
Note the difference between returning None (which generally resets a component's value to an empty state) versus returning gr.skip(), which leaves the component value unchanged.
Tip: if you have multiple output components, and you want to leave all of their values unchanged, you can just return a single gr.skip() instead of returning a tuple of skips, one for each element.
Running Events Consecutively
You can also run events consecutively by using the then method of an event listener. This will run an event after the previous event has finished running. This is useful for running events that update components in multiple steps.
For example, in the chatbot example below, we first update the chatbot with the user message immediately, and then update the chatbot with the computer response after a simulated delay.
import gradio as gr
import random
import time
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
def user(user_message, history):
return "", history + [{"role": "user", "content": user_message}]
def bot(history):
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
time.sleep(2)
history.append({"role": "assistant", "content": bot_message})
return history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
if __name__ == "__main__":
demo.launch()
The .then() method of an event listener executes the subsequent event regardless of whether the previous event raised any errors. If you'd like to only run subsequent events if the previous event executed successfully, use the .success() method, which takes the same arguments as .then(). Conversely, if you'd like to only run subsequent events if the previous event failed (i.e., raised an error), use the .failure() method. This is particularly useful for error handling workflows, such as displaying error messages or restoring previous states when an operation fails.
Binding Multiple Triggers to a Function
Often times, you may want to bind multiple triggers to the same function. For example, you may want to allow a user to click a submit button, or press enter to submit a form. You can do this using the gr.on method and passing a list of triggers to the trigger.
import gradio as gr
with gr.Blocks() as demo:
name = gr.Textbox(label="Name")
output = gr.Textbox(label="Output Box")
greet_btn = gr.Button("Greet")
trigger = gr.Textbox(label="Trigger Box")
def greet(name, evt_data: gr.EventData):
return "Hello " + name + "!", evt_data.target.__class__.__name__
def clear_name(evt_data: gr.EventData):
return ""
gr.on(
triggers=[name.submit, greet_btn.click],
fn=greet,
inputs=name,
outputs=[output, trigger],
).then(clear_name, outputs=[name])
if __name__ == "__main__":
demo.launch()
You can use decorator syntax as well:
import gradio as gr
with gr.Blocks() as demo:
name = gr.Textbox(label="Name")
output = gr.Textbox(label="Output Box")
greet_btn = gr.Button("Greet")
@gr.on(triggers=[name.submit, greet_btn.click], inputs=name, outputs=output)
def greet(name):
return "Hello " + name + "!"
if __name__ == "__main__":
demo.launch()
You can use gr.on to create "live" events by binding to the change event of components that implement it. If you do not specify any triggers, the function will automatically bind to all change event of all input components that include a change event (for example gr.Textbox has a change event whereas gr.Button does not).
import gradio as gr
with gr.Blocks() as demo:
with gr.Row():
num1 = gr.Slider(1, 10)
num2 = gr.Slider(1, 10)
num3 = gr.Slider(1, 10)
output = gr.Number(label="Sum")
@gr.on(inputs=[num1, num2, num3], outputs=output)
def sum(a, b, c):
return a + b + c
if __name__ == "__main__":
demo.launch()
You can follow gr.on with .then, just like any regular event listener. This handy method should save you from having to write a lot of repetitive code!
Binding a Component Value Directly to a Function of Other Components
If you want to set a Component's value to always be a function of the value of other Components, you can use the following shorthand:
with gr.Blocks() as demo:
num1 = gr.Number()
num2 = gr.Number()
product = gr.Number(lambda a, b: a * b, inputs=[num1, num2])
This functionally the same as:
with gr.Blocks() as demo:
num1 = gr.Number()
num2 = gr.Number()
product = gr.Number()
gr.on(
[num1.change, num2.change, demo.load],
lambda a, b: a * b,
inputs=[num1, num2],
outputs=product
)
Source and license
Source: Gradio documentation, adapted for this site. Licensing and attribution follow the terms of the upstream project.