---
source: https://iinventedlight.com/library/notes/gradio-blocks.md
title: "Gradio Blocks builds custom apps from components wired together by event listeners"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Gradio guides); Apache-2.0 (gradio-app/gradio (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Gradio guides, Blocks and Event Listeners; gradio-app/gradio (GitHub), Blocks and Event Listeners (source page). Apache-2.0 (https://raw.githubusercontent.com/gradio-app/gradio/main/LICENSE). Copyright Gradio authors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: gradio blocks"
tags: [gradio, python, ui, blocks, event-listeners, demos]
client: Librarian
sources:
  - id: S1
    title: "Blocks and Event Listeners"
    publisher: Gradio guides
    url: https://www.gradio.app/guides/blocks-and-event-listeners
    accessed: 2026-11-18
    license: "Apache-2.0"
  - id: S2
    title: "Blocks and Event Listeners (source page)"
    publisher: gradio-app/gradio (GitHub)
    url: https://github.com/gradio-app/gradio/blob/main/guides/03_building-with-blocks/01_blocks-and-event-listeners.md
    accessed: 2026-11-18
    license: "Apache-2.0"
---
# Gradio Blocks builds custom apps from components wired together by event listeners
**Answer:** The Blocks app code is contained within a `with gr.Blocks() as demo:` clause, and event listeners such as `click()` tie input and output components to a function. Event listeners define the data flow within the app; components created inside the clause are added to the Blocks automatically.

## Example

```python
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")
    name.submit(fn=greet, inputs=name, outputs=output).then(lambda: "", outputs=name)

demo.launch()
```

## Event listeners

- `click()` is triggered when a Button is clicked; `change()` is triggered by typing in a Textbox. Different Components support different event listeners, like `play()` on `Video`.
- `.then()` runs the next event after the previous one finishes, regardless of errors. `.success()` runs it only if the previous event succeeded, and `.failure()` only if it raised an error.
- `gr.on(triggers=[...], fn=...)` binds multiple triggers to the same function, such as a submit button and pressing enter. With no triggers, it binds to the `change` events of the input components.

## Inputs and outputs

- A function can take inputs as a list of arguments or as a single dictionary keyed by the component.
- It can return a list of values, one per output component, or a dictionary keyed by the component.

## Interactivity

Any Component that acts as an input to an event listener is made interactive, and an output-only component is not. Override this with `interactive=True` or `interactive=False`. A component that is neither input nor output is non-interactive if constructed with a default value.

## See also

- [hf-transformers-pipeline](https://iinventedlight.com/library/notes/hf-transformers-pipeline/)
- [python-asyncio-basics](https://iinventedlight.com/library/notes/python-asyncio-basics/)

## Claims

- id: C1
  claim: Event listeners define the data flow within the app.
  source: S1
  evidence: "Event listeners define the data flow within the app."

- id: C2
  claim: .then() runs the next event regardless of errors.
  source: S2
  evidence: "executes the subsequent event regardless of whether the previous event raised any errors"

- id: C3
  claim: Input components are made interactive.
  source: S2
  evidence: "any Component that acts as an input to an event listener is made interactive"

## Sources

- [S1] Blocks and Event Listeners. Gradio guides. https://www.gradio.app/guides/blocks-and-event-listeners . License: Apache-2.0. Accessed 2026-11-18.

- [S2] Blocks and Event Listeners (source page). gradio-app/gradio (GitHub). https://github.com/gradio-app/gradio/blob/main/guides/03_building-with-blocks/01_blocks-and-event-listeners.md . License: Apache-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Gradio guides, Blocks and Event Listeners; gradio-app/gradio (GitHub), Blocks and Event Listeners (source page). Apache-2.0 (https://raw.githubusercontent.com/gradio-app/gradio/main/LICENSE). Copyright Gradio authors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
