ui.update_accordion

ui.update_accordion(id, *, show, session=None)

Dynamically set accordions' states.

Dynamically (i.e., programmatically) update/modify accordions 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.

show : bool | str | list[str]

Either a string or list of strings (used to identify particular accordion_panel(s) by their value) or a bool to set the state of all panels.

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

items = [
    ui.accordion_panel(f"Section {letter}", f"Some narrative for section {letter}")
    for letter in "ABCDE"
]

app_ui = ui.page_fluid(
    ui.input_action_button("set_acc", "Only open sections A,C,E", class_="mt-3 mb-3"),
    # Provide an id to create a shiny input binding
    ui.accordion(*items, id="acc", open=["Section B", "Section D"], multiple=True),
)


def server(input: Inputs, output: Outputs, session: Session):
    @reactive.effect
    @reactive.event(input.set_acc)
    def _():
        ui.update_accordion("acc", show=["Section A", "Section C", "Section E"])


app = App(app_ui, server)