Follow these steps to add a left-side sidebar to your app:
Add ui.layout_sidebar() inside any Shiny UI page method (e.g., ui.page_fluid()). ui.layout_sidebar() creates a sidebar layout.
Pass ui.sidebar()as the first argument of ui.layout_sidebar() to create the sidebar. By default, the position parameter of ui.sidebar() is “left” and the sidebar will appear on the left.
Supply additional components (output components, cards, text, etc.) to ui.layout_sidebar() to define the contents of the main content area.
Sidebar on the right
#| '!! shinylive warning !!': |#| shinylive does not work in self-contained HTML documents.#| Please set `embed-resources: false` in your metadata.#| standalone: true#| components: [viewer]#| layout: horizontal#| viewerHeight: 125## file: app.pyfrom shiny import App, uiapp_ui = ui.page_sidebar( ui.sidebar("Sidebar", position="right", bg="#f8f8f8"), "Main content",) def server(input, output, session): passapp = App(app_ui, server)
Follow these steps to add a right-side sidebar to your app:
Add ui.layout_sidebar() inside any Shiny UI page method (e.g., ui.page_fluid()). ui.layout_sidebar() creates a sidebar layout.
Pass ui.sidebar()as the first argument of ui.layout_sidebar() to create the sidebar.
Pass position="right" to ui.sidebar(). The position argument controls where the sidebar appears relative to the main content.
Supply components (e.g., inputs) to ui.sidebar() to define the sidebar’s contents. Supply additional components (e.g., output components, cards, etc.) to ui.layout_sidebar() to define the contents of the main content area.
Sidebar within a card
#| '!! shinylive warning !!': |#| shinylive does not work in self-contained HTML documents.#| Please set `embed-resources: false` in your metadata.#| standalone: true#| components: [viewer]#| layout: horizontal#| viewerHeight: 175## file: app.pyfrom shiny import App, uiapp_ui = ui.page_fillable( ui.card( ui.card_header("Card with sidebar"), ui.layout_sidebar( ui.sidebar("Sidebar", bg="#f8f8f8"), "Card content", ), ) )def server(input, output, session): passapp = App(app_ui, server)
from shiny.express import uiui.page_opts(fillable=True)with ui.card(): ui.card_header("Card with sidebar")with ui.layout_sidebar(): with ui.sidebar(bg="#f8f8f8"): "Sidebar""Card content"
ui.sidebar() has an open parameter that defines whether the sidebar appears open or closed when the app launches. To create a sidebar that is initially closed, set the open parameter to "closed".
The other options for open are:
"desktop": The default. The sidebar starts open on a desktop screen and closed on mobile.
"open": The sidebar starts open and can be closed.
"always": The sidebar is always open and cannot be closed.