Skip to content

RadioButton

Bases: InputElement

_value_type: type property

Get the RadioButton value type: string str.

__init__(key, value, label=None, count=None, optional=False, hide_when_disabled=False, options=[], horizontal=False)

A single choice represented as a group of exclusive radio buttons.

Parameters:

Name Type Description Default
key str

ID of the element. It must be unique as it is the key used to story data in Project(), otherwise it will lead to conflicts at runtime in both execution and Streamlit modes. The key will be transformed into snake case and slugified to avoid any special character or whitespace. Note that an ID cannot start with _. Try to choose a key that is meaningful for your context (see examples projects).

required
value Optional[Union[str, List[str]]]

Radio button initially selected.

required
label Optional[str]

Label to display on top of the field.

None
count Optional[Union[int, str]]

Specify the number of occurence of the widget. OneCode typically uses it for the streamlit case. Note that if count is defined, the expected value should always be a list, even if the count is 1. count can either be a fixed number (e.g. 3) or an expression dependent of other elements (see Using Expressions for more information).

None
optional Union[bool, str]

Specify whether the value may be None. optional can either be a fixed boolean (False or True) or a conditional expression dependent of other elements (see Using Expressions for more information).

False
hide_when_disabled bool

If element is optional, set it to True to hide it from the interface, otherwise it will be shown disabled.

False
options List[str]

List all possible options available. This list may either be fixed or dynamic (to a certain extent): in the latter case, use expressions in a similar way as optional and count. See example below.

[]
horizontal bool

Set to True to have radio buttons displayed horizontally, otherwise radio buttons will be displayed vertically.

False

Raises:

Type Description
ValueError

if the key is empty or starts with _.

AttributeError

if one the kwargs conflicts with an existing attribute or method.

Example

Fixed options:

from onecode import radio_button, Mode, Project

Project().mode = Mode.EXECUTE
widget = radio_button(
    key="RadioButton",
    value="A",
    options=["A", "B", "C"]
)
print(widget)

Output
"A"

Dynamic options:

from onecode import csv_reader, radio_button, Mode, Project

Project().mode = Mode.EXECUTE

df = csv_reader("csv", "/path/to/file.csv")

widget = radio_button(
    key="Dynamic RadioButton",
    value=None,
    options='$csv$.columns',
    optional=True
)

assert widget is None

_validate(value)

Raises:

Type Description
ValueError

if the choice is not part of possible options.

streamlit(id)

Returns:

Type Description
str

The Streamlit code for a group of radio buttons (st.radio).