Sidebars
A sidebar layout creates a sidebar in your Shiny app, typically used for inputs, and a large main area, typically used for outputs.
A sidebar layout creates a sidebar, typically used for inputs, and a large main area, typically used for outputs.
Relevant Functions
-
ui.layout_sidebar
ui.layout_sidebar(sidebar, *args, fillable=True, fill=True, bg=None, fg=None, border=None, border_radius=None, border_color=None, gap=None, padding=None, height=None, **kwargs)
-
ui.sidebar
ui.sidebar(*args, width=250, position='left', open='desktop', id=None, title=None, bg=None, fg=None, class_=None, max_height_mobile=None, gap=None, padding=None)
Sidebar on the left
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 ofui.layout_sidebar()
to create the sidebar. By default, theposition
parameter ofui.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
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 ofui.layout_sidebar()
to create the sidebar.Pass
position="right"
toui.sidebar()
. Theposition
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.) toui.layout_sidebar()
to define the contents of the main content area.
Sidebar within a card
#| standalone: true
#| components: [viewer]
#| layout: horizontal
#| viewerHeight: 175
## file: app.py
from shiny import App, ui
app_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):
pass
app = App(app_ui, server)
Follow these steps to add a sidebar within a card to your app:
- Add
ui.card()
to the UI of your app to create a card. - Pass
ui.layout_sidebar()
toui.card()
to define a sidebar layout within the card. - Add
ui.sidebar()
and additional elements toui.layout_sidebar()
to define the sidebar and main content as usual. - Add inputs or other components as desired to
ui.sidebar()
to define the sidebar’s contents.
Collapsed sidebar
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.