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, kwargs)
¶
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 |
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 |
None
|
optional |
Union[bool, str]
|
Specify whether the value may be None. |
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 |
[]
|
multiple |
bool
|
Set to True if multiple choice is allowed, otherwise only a single element can be selected. |
False
|
**kwargs |
Any
|
Extra user meta-data to attach to the element. Argument names cannot overwrite
existing attributes or methods name such as |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
if the |
AttributeError
|
if one the |
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)
["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)
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 ( |