Switch

#| 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)
from shiny import render
from shiny.express import input, ui

ui.input_switch("switch", "Switch", False)  

@render.ui
def value():
    return input.switch()
from shiny import ui, render, App

app_ui = ui.page_fluid(
    ui.input_switch("switch", "Switch", False),  
    ui.output_ui("value"),
)

def server(input, output, session):
    @render.ui
    def value():
        return input.switch()

app = App(app_ui, server)
No matching items

Relevant Functions

No matching items

Details

A switch allows you to select between logical values.

To add a switch to your app:

  1. 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.

  2. Specify the id and label parameters of ui.input_switch() to define the identifier and label of the switch.

  3. By default, the value parameter, which defines the switch’s initial value, is False. If you’d like the initial value to be True, set value equal to True.

The value of an input component is accessible as a reactive value within the server() function. To access the value of a switch:

  1. Use input.<switch_id>() (e.g., input.switch()) to access the value of the switch. The server value of a switch is True if checked and False otherwise.