Switch
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [viewer]
#| viewerHeight: 200
## file: app.py
from shiny import ui, render, App
app_ui = ui.page_fluid(
ui.input_switch("switch", "Switch", True),
ui.output_ui("value"),
).add_class("p-5")
def server(input, output, session):
@output
@render.ui
def value():
return input.switch()
app = App(app_ui, server)Relevant Functions
-
ui.input_switch
ui.input_switch(id, label, value=False, *, width=None)
Details
A switch allows you to select between logical values.
To add a switch to your app:
Add
ui.input_switch()to the UI of your app to create a switch. Where you call this function will determine where the switch will appear within the app’s layout.Specify the
idandlabelparameters ofui.input_switch()to define the identifier and label of the switch.By default, the
valueparameter, which defines the switch’s initial value, isFalse. If you’d like the initial value to beTrue, setvalueequal toTrue.
The value of an input component is accessible as a reactive value within the server() function. To access the value of a switch:
- Use
input.<switch_id>()(e.g.,input.switch()) to access the value of the switch. The server value of a switch isTrueif checked andFalseotherwise.
Variation Showcase
A live kitchen sink of all possible parameters for the Switch in Shiny Core and Shiny Express.
![]()