π’Β Β Button Selector
Submitted by Zhijia Liu
Summary
A button selector that can be used to select an item from a list of options.
Functions
button_selector
Create a button selector for choosing an item from a list of options.
This function creates a grid of buttons representing the items in the provided list. The selected button is highlighted, and the index of the selected item is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
options |
Sequence[str]
|
A list of strings representing the selectable options. |
required |
index |
int
|
The index of the default selected item. Defaults to 0. |
0
|
spec |
int
|
The number of columns in the button grid. Defaults to 4. |
4
|
key |
str
|
A unique key for the button selector. Used for maintaining state. |
'button_selector'
|
label |
str
|
A label for the button selector. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The index of the currently selected item in the options. |
Note
This function uses Streamlit's session state to maintain the selected item across reruns of the app.
Source code in src/streamlit_extras/button_selector/__init__.py
Import:
- You should add this to the top of your .py file
Examples
example
def example():
month_list = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
]
selected_index = button_selector(
month_list,
index=0,
spec=4,
key="button_selector_example_month_selector",
label="Month Selector",
)
st.write(f"Selected month: {month_list[selected_index]}")