Skip to main content
Blocks is Gradio’s low-level API that allows you to create more custom web applications and demos than Interfaces (yet still entirely in Python).

Constructor

gr.Blocks(
    analytics_enabled=None,
    mode="blocks",
    title="Gradio",
    fill_height=False,
    fill_width=False,
    delete_cache=None
)

Parameters

analytics_enabled
bool | None
default:"None"
Whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable or default to True.
mode
str
default:"'blocks'"
A human-friendly name for the kind of Blocks or Interface being created. Used internally for analytics.
title
str | I18nData
default:"'Gradio'"
The tab title to display when this is opened in a browser window.
fill_height
bool
default:"False"
Whether to vertically expand top-level child components to the height of the window. If True, expansion occurs when the scale value of the child components >= 1.
fill_width
bool
default:"False"
Whether to horizontally expand to fill container fully. If False, centers and constrains app to a maximum width. Only applies if this is the outermost Blocks in your Gradio app.
delete_cache
tuple[int, int] | None
default:"None"
A tuple corresponding [frequency, age] both expressed in number of seconds. Every frequency seconds, the temporary files created by this Blocks instance will be deleted if more than age seconds have passed since the file was created.

Class Methods

from_config

gr.Blocks.from_config(config, fns, proxy_url)
Factory method that creates a Blocks from a config and list of functions. Used internally by the gradio.external.load() method.
config
BlocksConfigDict
required
A dictionary containing the configuration of the Blocks.
fns
list[Callable]
required
A list of functions that are used in the Blocks. Must be in the same order as the dependencies in the config.
proxy_url
str
required
An external url to use as a root URL when serving files for components in the Blocks.
returns
Blocks
A Blocks instance.

get_instances

gr.Blocks.get_instances()
Get a list of all current Blocks instances.
returns
list[Blocks]
List of all current instances.

Instance Methods

queue

Enables queueing for the Blocks app, allowing for better handling of concurrent requests.

launch

Launches the Blocks app locally or on a public URL.

integrate

Integrates the Blocks app with various platforms.

load

Sets up an event that runs when the Blocks app is first loaded.

unload

Sets up an event that runs when the user closes the Blocks app.

Example

import gradio as gr

def update(name):
    return f"Welcome to Gradio, {name}!"

with gr.Blocks() as demo:
    gr.Markdown("Start typing below and then click **Run** to see the output.")
    with gr.Row():
        inp = gr.Textbox(placeholder="What is your name?")
        out = gr.Textbox()
    btn = gr.Button("Run")
    btn.click(fn=update, inputs=inp, outputs=out)

demo.launch()

Notes

Compared to the Interface class, Blocks offers more flexibility and control over:
  1. The layout of components
  2. The events that trigger the execution of functions
  3. Data flows (e.g. inputs can trigger outputs, which can trigger the next level of outputs)
Blocks also offers ways to group together related demos such as with tabs.

Build docs developers (and LLMs) love