Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Blocks and Layouts

Rows, Columns, Tabs and Accordions

Control a Gradio page with layout blocks: Row, Column with scale and min_width, Tabs, Accordion, Group, Sidebar and nesting. Includes responsive behaviour and common layout mistakes.

Once you use Blocks, layout is code too. A handful of container classes (Row, Column, Tab, Accordion, Group, Sidebar) are used as with blocks, and whatever you create inside them lands in that container. Nesting these gives almost any form or dashboard layout. Gradio code is read-only; stand-ins draw the layout tree as text so you can test nesting rules.

Row and Column

By default, components stack vertically. A Row puts its children side by side, and a Column stacks them vertically (useful inside a row).

Python
import gradio as gr

with gr.Blocks() as demo:
    gr.Markdown("# Resizer")
    with gr.Row():
        with gr.Column(scale=1):
            image = gr.Image(label="Input")
            width = gr.Slider(10, 500, value=200, label="Width")
            go = gr.Button("Resize")
        with gr.Column(scale=2):
            result = gr.Image(label="Result")

The page shows a heading, then two columns side by side: a narrow one (the form) and a wide one (the result), in a 1:2 ratio. Options worth knowing:

Option On Meaning
scale Row children, Column Relative width share (integer); 0 means "shrink to content"
min_width Column Pixel width below which the column wraps onto its own line
equal_height Row Make all children the same height
variant Column "panel" draws a grey card, "compact" removes gaps
visible all Hide or show the container (see dynamic UI)

On narrow phone screens, columns whose width would fall under min_width wrap and stack, which is Gradio's responsive behaviour. A good rule is to set min_width to a value such as 320 for columns that contain forms.

Tabs

Python
with gr.Blocks() as demo:
    with gr.Tabs():
        with gr.Tab("Text"):
            gr.Textbox(label="Input text")
        with gr.Tab("Image"):
            gr.Image(label="Input image")
        with gr.Tab("Help", id="help"):
            gr.Markdown("Read the manual.")

The page shows a tab strip with three labels; only one panel is visible at a time. Tab also works without Tabs around it, but Tabs(selected="help") lets you pick the starting tab. Tabs fire a select event when switched, which is handy for loading data lazily. Use tabs for alternatives or stages of one task, such as "Upload", "Webcam", "Examples". Do not hide required inputs in a tab nobody opens.

Accordion

Python
with gr.Accordion("Advanced settings", open=False):
    temperature = gr.Slider(0, 1, value=0.7, label="Temperature")
    seed = gr.Number(value=42, label="Seed")

A collapsed bar titled "Advanced settings" expands to show the two controls. This pattern is the best way to keep a clean first view while still offering model options. Defaults still apply to hidden inputs, so a collapsed Accordion does not remove its components from the function call.

Group, Sidebar and Column tricks

  • gr.Group() joins components visually with no gaps, like a single card (ideal for a text box plus a button).
  • gr.Sidebar() (in recent versions) adds a collapsible panel on the left, common for settings and navigation.
  • gr.Row(equal_height=True) aligns tall and short components.
  • gr.HTML and gr.Markdown between containers add headings and explanations.
  • gr.Accordion can sit inside a Column inside a Tab: containers nest as deeply as you need.

Layout containers

Row Side by side
Column Stacked
Tab One at a time
Accordion Collapsible

Nest them freely.

1 / 2

How layout is represented

Every with block adds a node to a tree: the Blocks object at the root, containers as branches, components as leaves. The browser draws that tree with CSS flexbox. Because the structure is nested, the indentation of your with blocks is the layout, which is easy to read and easy to break by mis-indenting. A component accidentally created outside a with gr.Row(): simply lands below it.

Stand-in 1: print the layout tree

This stand-in builds a layout tree with context managers, like Blocks does, and prints it.

Python Stand-in: layout as a tree

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The printout is the structure Gradio sends to the browser. Move a line out of its with block and see how the tree changes.

Stand-in 2: column widths from scale

Python Stand-in: how scale divides the row

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When a column would be narrower than its min_width (False in the second value of the output, as in the second run), the real layout wraps it below the previous one instead of squeezing it.

Layout mistakes to avoid

  • Mis-indented with blocks: components end up in the wrong place.
  • Too many columns: three or four columns of text are unreadable on laptops.
  • No min_width: forms squeeze into thin strips on phones.
  • Hiding required inputs in collapsed accordions without telling users.
  • Everything in Tabs: switching costs a click. Prefer one clean page when it fits.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you place two components side by side?

    Row lays its children horizontally. Column stacks them vertically.
  2. 2. In two columns with scale=1 and scale=2, how much width does the second get?

    Scales are relative shares: 1 and 2 make 1/3 and 2/3.
  3. 3. What happens to inputs inside a collapsed Accordion when a function runs?

    Collapsing only changes visibility; the components still exist and provide values.
  4. 4. What defines the layout in Blocks code?

    Each with block adds a container node, so indentation is the structure.

Summary

  • Row is horizontal, Column is vertical, and scale and min_width size them.
  • Tabs hold alternatives, Accordion hides advanced options, Group joins components.
  • The nesting of with blocks is the layout tree.
  • Set min_width so phones wrap sensibly and avoid hiding required inputs.

Next: Themes and Custom CSS