Skip to content

Checkbox

Bases: InputElement

_value_type: type property

Get the Checkbox value type: boolean bool.

__init__(key, value, label=None, count=None, optional=False, hide_when_disabled=False)

A simple checkbox with a label. Value is either True, False or None.

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[bool, List[bool]]]

Initial check status: True, False or None.

required
label Optional[str]

Label to display next to the checkbox.

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

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

from onecode import checkbox, Mode, Project

Project().mode = Mode.EXECUTE
widget = checkbox(
    key="Checkbox",
    value=True,
    label="My Checkbox"
)
print(widget)
Output
True

_validate(value)

No validation is performed.

streamlit(id)

Returns:

Type Description
str

The Streamlit code for a checkbox (st.checkbox).