kmail.at
contact

elsewhere

EnglishEN
← learning

gradio · difficulty ◆◆

gradio Accordion — fold the knobs nobody needs behind one click

A closed accordion isn't hidden UI — it's UI that was never built, holding values that are still submitted.

Every demo has three controls that matter to you and eleven that only matter to one reviewer. Gradio put a folding door on them in 3.3 — and only gave that door an event in 5.5.

fully AI-generatedThis text is fully AI-generated and was published without human editorial review.

2026-10-27 · 5 min read

$ gr.Accordion()
3.3
First release

2022-09-08 — label, open, elem_id, and no events

10
Constructor params

counted from the 6.30.0 signature, `self` excluded

25 months
Eventless for

3.3 (2022-09-08) → 5.5.0 (2024-11-04), when expand/collapse arrived

6.29.0
height / max_height

2026-09-29 — the release before 6.30.0

What it does

`with gr.Accordion("Advanced settings", open=False):` opens a layout container: a header row with a ▶ that rotates to ▼, and a body you can fold away. Everything inside keeps working normally — sliders submit values, listeners fire — it simply isn't in the DOM until the first click. The 6.30.0 signature takes ten arguments: `label`, `open`, `height`, `max_height`, `visible`, `elem_id`, `elem_classes`, `render`, `key`, `preserved_by_key`. Since 5.5.0 it also has two events: `.expand()` and `.collapse()`.

Why it matters

A demo has a happy path and an escape hatch. The happy path is one text field and one Run button; the escape hatch is temperature, top_p, seed, model variant, retry count. Stacked in the open they read as homework, so nobody types anything. Folded into one 41-pixel row labelled 'Advanced settings' they read as optional — and the reader who does want them gets a full panel. It is also the cleanest place for the 'How this was scored' prose that makes a demo credible without pushing the actual output off-screen.

Example

$ import gradio as gr

def summarize(prompt, temperature, top_p, lock_seed):
    return f"**{len(prompt.split())} words in** — temperature={temperature}, top_p={top_p}, seed locked={lock_seed}"

with gr.Blocks(title="Report summarizer") as demo:
    prompt = gr.Textbox("Summarize the incident report", label="prompt")
    with gr.Accordion("Advanced settings", open=False):
        temperature = gr.Slider(0, 1, value=0.7, label="temperature")
        top_p = gr.Slider(0, 1, value=0.9, label="top_p")
        lock_seed = gr.Checkbox(True, label="lock seed")
    run = gr.Button("Run", variant="primary")
    out = gr.Markdown()
    run.click(summarize, [prompt, temperature, top_p, lock_seed], out)

demo.launch()
* Running on local URL:  http://127.0.0.1:7860
* To create a public link, set `share=True` in `launch()`.

What renders: a Textbox labelled 'prompt' prefilled with 'Summarize the incident report'; below it a single 41-pixel row 'Advanced settings ▶' (the header button is `button.label-wrap`, the icon span carries an inline `transform: rotate(90deg)` while closed); then a primary 'Run' button and an empty Markdown. In /config the tree is id 1 textbox, id 2 accordion (`skip_api: true`), ids 3/4 sliders, id 5 checkbox, id 8 markdown; one dependency, api_name='summarize', targets=[[7,'click']], inputs=[1,3,4,5], outputs=[8].

Clicking Run with the panel shut printed '4 words in — temperature=0.7, top_p=0.9, seed locked=True': the three hidden controls submitted their constructor defaults. I then opened the panel, arrowed temperature to 0.83, shut it again and clicked Run: 'temperature=0.83'. Hiding the slider did not reset it.

The whole point: a folded panel is a normal member of the layout, not a disabled one.

$ import gradio as gr

reads = []

def note(kind):
    reads.append(kind)
    return " → ".join(reads)

with gr.Blocks(title="Docs demo") as demo:
    answer = gr.Markdown("### Answer\nThe refund window is 30 days.")
    with gr.Accordion("How this was scored", open=True) as how:
        gr.Markdown("Token overlap weighted by inverse document frequency, then normalised to 0-1.")
    trail = gr.Textbox(label="listener trail", lines=2)
    how.expand(lambda: note("expand"), None, trail)
    how.collapse(lambda: note("collapse"), None, trail)

demo.launch()
* Running on local URL:  http://127.0.0.1:7861
* To create a public link, set `share=True` in `launch()`.

What renders: an 'Answer' Markdown, an open 'How this was scored ▼' panel with one line of prose and a 'listener trail' Textbox. /config lists two dependencies — api_name='lambda' with targets=[[2,'expand']] and api_name='lambda_1' with targets=[[2,'collapse']] — both with inputs=[] and outputs=[4].

First click on the header folded the panel and wrote 'collapse' into the trail; the second click appended 'expand', leaving 'collapse → expand'. Reloading the page left the trail empty: an accordion that starts `open=True` fires no expand event on mount, so the listener only reports real user intent.

The listeners take no arguments — wire one pair per accordion and keep the state in Python, not in the callback signature.

$ import gradio as gr

LONG = "\n\n".join(f"Clause {i}: the licence covers internal evaluation only." for i in range(1, 21))

with gr.Blocks(title="Licence reader") as demo:
    with gr.Accordion("Licence text", open=True, max_height=160):
        gr.Markdown(LONG)
    with gr.Accordion("Dataset notes", open=False) as notes:
        gr.Markdown("1,204 rows, 14 columns, scraped 2026-03.")

    open_btn = gr.Button("Read the notes")
    close_btn = gr.Button("I'm done")
    open_btn.click(lambda: gr.Accordion(open=True), None, notes)
    close_btn.click(lambda: gr.Accordion(open=False), None, notes)

demo.launch()
* Running on local URL:  http://127.0.0.1:7862
* To create a public link, set `share=True` in `launch()`.

What renders: 'Licence text ▼' open, its body measured at exactly 160px with `overflow-y: auto` over a 759px scrollHeight — about four and a half screens of clauses inside a fixed band; then a closed 'Dataset notes ▶' whose body measured 0px high with no components mounted inside; then the two buttons. /config stores `max_height: 160` on the accordion node and two dependencies, api_name='lambda' (outputs=[3], via 'Read the notes') and 'lambda_1'.

Clicking 'Read the notes' returned `gr.Accordion(open=True)` into the outputs: the header gained the `open` class, the body flipped to `display: block` and the notes appeared — label intact, children intact. There is no `.update()` on the class, so returning the component is the supported way to drive it from Python.

`max_height` caps, `height` forces. With short text the same panel shrank to 23px instead of holding 400px of empty box (measured with max_height=400).

Common flags

label="Advanced settings"
The header text, and the only argument worth passing positionally. Keep it a noun phrase — it's a signpost, not a sentence.
open=False
Initial state at mount, nothing more. There is no reactive `open` you can flip from inside the page without a round trip.
max_height=160
New in 6.29.0. Caps the body and scrolls inside it, but shrinks to fit short content — measured 23px for two lines under max_height=400.
height=200
New in 6.29.0. Forces the body to that exact height even when the content is one sentence, which is usually not what you want for prose.
visible=False
Drops the accordion and every child from the page — the label never appears in the DOM (checked). `visible="hidden"` keeps the node but renders it `display: none` at 0×0, which is only useful if you plan to switch it back from Python.
elem_id / elem_classes
Land on the wrapper `div.block.gr-accordion`, not on the header button — target `.label-wrap` inside if you want to restyle the signpost.
acc.expand(fn) / acc.collapse(fn)
Since 5.5.0, no arguments passed. Cheapest engagement metric in a demo: count how many readers open the explanation.

History

3.3 — a folding door with no handle

`gr.Accordion` appeared in Gradio 3.3 (2022-09-08) as part of the first Blocks layout set, with `label`, `open=True` and `elem_id` — and a static `update(open, visible)` helper, because that was how you changed anything in 3.x. 3.4.0 added the `visible` flag. The 4.0.0 rewrite (2023-10-31) dropped `update()` in favour of returning a component instance, which is still how you toggle it today.

5.5.0 and 6.29.0 — the two things it was missing

From 4.0 through 5.4 the class declared `EVENTS = []`: a layout container you could click but not observe. Gradio 5.5.0 (2024-11-04) finally added `expand` and `collapse`, 25 months after the component shipped. Sizing came later still — `height` and `max_height` only landed in 6.29.0 (2026-09-29), one release before the 6.30.0 I tested.

Under the hood

What the fold actually is, in the DOM and in the config

The container is `div.block.gr-accordion.padded.auto-margin` (41px tall when closed). Inside sit the header `button.label-wrap` — with the class `open` added when expanded, and `span.icon` carrying an inline `transform: rotate(90deg)` when collapsed — and the body `div[data-testid="accordion-content"]`, whose inline style is toggled between `display: none` and `display: block`. Because the body is switched by `display` rather than by an animated height, the panel snaps open — computed `transition` is `all`, but `display` isn't animatable. The body's children are mounted lazily on first expand, which is why a closed panel is free. On the Python side the accordion serialises as `{type: "accordion", props: {label, open, visible, height?, max_height?}, skip_api: true}` — `skip_api` means a layout can never be an API endpoint, and `open` is only a mount-time prop, which is exactly why returning `gr.Accordion(open=True)` works (the frontend applies it as a props update) and why a bare `.update()` doesn't exist any more.

Fun facts

Pros & cons

pros

  • + The default view stays honest — two controls up front, the temperature/top_p/seed trio one click away, without a second page or a settings dialog.
  • + Closed means unbuilt: a collapsed panel contributes no components to the DOM, so it costs nothing at first paint.
  • + Nothing is lost while folded — a slider moved to 0.83 still reached the handler after the panel was closed again.

cons

  • − The header button has no `aria-expanded` or `aria-controls` in the 6.30.0 DOM, so screen readers get a button with no state.
  • − `.expand()` and `.collapse()` receive no arguments and nothing fires on page load — you need one closure per panel to know which one opened.
  • − No `.update(open=...)` since 4.0: toggling it from Python means returning a whole `gr.Accordion(open=...)` into a listener's outputs.

Takeaways

  1. 1Move the knobs you don't want to explain — temperature, top_p, seed, retry count — into `open=False`; the handler keeps receiving their values.
  2. 2Never let JavaScript depend on a node inside a closed panel at page load: it isn't there until the first expand.
  3. 3Use `max_height` for prose panels (shrinks when short, scrolls when long) and `height` only when you want a fixed viewport.
  4. 4Wire `.expand()` first if you want one number that tells you whether anyone reads the explanation.
  5. 5To open or close it from Python, return `gr.Accordion(open=True)` into the outputs — there is no `.update()`.

Related commands

archive entry — written and published by a scheduled job, never editorially reviewed. Kept for reference: out of the site’s search index, and no ads run here. the reviewed library →

← all learning