Radio Buttons

#| standalone: true
#| components: [viewer]
#| viewerHeight: 200

## file: app.py
from shiny import App, render, ui

app_ui = ui.page_fluid(
    ui.input_radio_buttons(
        "radio",
        "Radio buttons",
        {"1": "Option 1", "2": "Option 2", "3": "Option 3"},
    ),
    ui.output_ui("value"),
    {"class": "vh-100 d-flex align-items-center px-4"},
)

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

app = App(app_ui, server)
from shiny import render
from shiny.express import input, ui

ui.input_radio_buttons(  
    "radio",  
    "Radio buttons",  
    {"1": "Option 1", "2": "Option 2", "3": "Option 3"},  
)  

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

app_ui = ui.page_fluid(
    ui.input_radio_buttons(  
        "radio",  
        "Radio buttons",  
        {"1": "Option 1", "2": "Option 2", "3": "Option 3"},  
    ),  
    ui.output_ui("value"),
)

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

app = App(app_ui, server)
No matching items

Relevant Functions

  • ui.input_radio_buttons
    ui.input_radio_buttons(id, label, choices, *, selected=None, inline=False, width=None)

No matching items

Details

Use a set of radio buttons to select an item from a list.

To add radio buttons to your app:

  1. Add ui.input_radio_buttons() to the UI of your app to create a set of radio buttons. Where you call this function will determine where the radio buttons will appear within the app’s layout.

  2. Specify the id and label parameters of ui.input_radio_buttons() to define the identifier and label of the set of radio buttons.

  3. Specify the value and label that accompanies each radio button using the choices parameter. choices can be either a list or a dictionary. If choices is a list, the list elements become the radio button values and labels. If choices is a dictionary, ui.input_radio_buttons() uses the dictionary keys as the button values and the dictionary values as the button labels.

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

  1. Use input.<radio_buttons_id>() to access the value of a radio button set (e.g., input.radio()). The server value of a set of radio buttons is a string containing the selected value.