Skip to content

Dropdown

Bases: InputElement

_value_type: type property

Get the Dropdown value type: either a list of string list[str] when the Dropdown is multiple choice, otherwise a single string str.

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

A single or multipe choice dropdown menu.

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[Union[str, int, float], List[Union[str, int, float]], List[List[Union[str, int, float]]]]]

Pre-selected value(s) among the options.

required
label Optional[str]

Label to display left of the dropdown menu.

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

List all possible options available in the dropdown menu. 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.

[]
multiple bool

Set to True if multiple choice is allowed, otherwise only a single element can be selected.

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 dropdown, Mode, Project

Project().mode = Mode.EXECUTE
widget = dropdown(
    key="Dropdown",
    value=["A", "C"],
    options=["A", "B", "C"],
    multiple=True
)
print(widget)

Output
["A", "C"]

Dynamic options:

from onecode import csv_reader, dropdown, Mode, Project

Project().mode = Mode.EXECUTE

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

widget = dropdown(
    key="Dynamic Dropdown",
    value=None,
    options='$csv$.columns',
    optional=True
)
print(widget)

Output
None

_validate(value)

Validate the selected value (see _validate_option_value() ). In case of multipe choice, each choice is validated individually.

_validate_option_value(value)

Raises:

Type Description
ValueError

if the value is not part of the possible options.

Note

This validation is not performed when the option list is dynamic.

streamlit(id)

Returns:

Type Description
str

The Streamlit code for a dropdown menu (st.multiselect | selectbox).