ui.update_accordion_panel

ui.update_accordion_panel(
    id,
    target,
    *body,
    title=MISSING,
    value=MISSING,
    icon=MISSING,
    show=None,
    session=None,
)

Dynamically update accordion panel contents.

Dynamically (i.e., programmatically) update/modify accordion panels in a Shiny app. These functions require an id to be provided to the accordion and must also be called within an active Shiny session.

Parameters

id : str

A string that matches an existing accordion’s id.

target : str

The value of an existing panel to update.

*body : TagChild = ()

If provided, the new body contents of the panel.

title : TagChild | None | MISSING_TYPE = MISSING

If not missing, the new title of the panel.

value : str | None | MISSING_TYPE = MISSING

If not missing, the new value of the panel.

icon : TagChild | None | MISSING_TYPE = MISSING

If not missing, the new icon of the panel.

session : Optional[Session] = None

A Shiny session object (the default should almost always be used).

References

Bootstrap Accordion

See Also

Examples

#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400

## file: app.py
from shiny import App, Inputs, Outputs, Session, reactive, ui


def make_panel(letter: str) -> ui.AccordionPanel:
    return ui.accordion_panel(
        f"Section {letter}",
        f"Some narrative for section {letter}",
        value=f"sec_{letter}",
    )


items = [make_panel(letter) for letter in "ABCDE"]

app_ui = ui.page_fluid(
    ui.input_switch("update_panel", "Update (and open) Sections"),
    ui.accordion(*items, id="acc", multiple=True),
)


def server(input: Inputs, output: Outputs, session: Session):
    @reactive.effect
    @reactive.event(input.update_panel)
    def _():
        txt = " (updated)" if input.update_panel() else ""
        show = bool(input.update_panel() % 2 == 1)
        for letter in "ABCDE":
            ui.update_accordion_panel(
                "acc",
                f"sec_{letter}",
                f"Some{txt} narrative for section {letter}",
                title=f"Section {letter}{txt}",
                # Open Accordion Panel to see updated contents
                show=show,
            )
        next_show_txt = "close" if show else "open"

        ui.update_switch("update_panel", label=f"Update (and {next_show_txt}) Sections")


app = App(app_ui, server)