I had the same issue with resetting the selected value in Streamlit pills. After some experimentation, I figured out a way to do it and created a function to handle this easily. Here’s how you can achieve it:
from streamlit_pills import pills
import streamlit as st
from typing import Iterable, Union, Callable
def custom_pills(label: str, options: Iterable[str], icons: Iterable[str] = None, index: Union[int, None] = 0,
format_func: Callable = None, label_visibility: str = "visible", clearable: bool = None,
key: str = None, reset_key: str = None):
"""
Displays clickable pills with an option to reset the selection.
Args:
label (str): The label shown above the pills.
options (iterable of str): The texts shown inside the pills.
icons (iterable of str, optional): The emoji icons shown on the left side of the pills. Each item must be a single emoji. Default is None.
index (int or None, optional): The index of the pill that is selected by default. If None, no pill is selected. Defaults to 0.
format_func (callable, optional): A function applied to the pill text before rendering. Defaults to None.
label_visibility ("visible" or "hidden" or "collapsed", optional): The visibility of the label. Use this instead of `label=""` for accessibility. Defaults to "visible".
clearable (bool, optional): Whether the user can unselect the selected pill by clicking on it. Default is None.
key (str, optional): The key of the component. Defaults to None.
reset_key (str, optional): The key used to reset the selection. Defaults to None.
Returns:
(any): The text of the pill selected by the user (same value as in `options`).
"""
# Create a unique key for the component to force update when necessary
unique_key = f"{key}-{reset_key}" if key and reset_key else key
# Pass the arguments to the pills function
selected = pills(label=label, options=options, icons=icons, index=index, format_func=format_func,
label_visibility=label_visibility, clearable=clearable, key=unique_key)
return selected
# Example usage
if 'selected_option' not in st.session_state:
st.session_state.selected_option = None
if 'reset_key' not in st.session_state:
st.session_state.reset_key = 0
# Function to reset the selection
def reset_selection():
st.session_state.selected_option = None
st.session_state.reset_key += 1 # Increment the key to force update
# Add a button to reset the selection
if st.button('Reset Selection'):
reset_selection()
# Use the custom_pills function
selected = custom_pills("Label", ["Option 1", "Option 2", "Option 3"], ["🍀", "🎈", "🌈"], index=None, clearable=True, key="pill", reset_key=str(st.session_state.reset_key))
# Update the selected option in session state
if selected is not None:
st.session_state.selected_option = selected
st.write("Selected option:", st.session_state.selected_option)
Explanation:
custom_pills
Function:
- Encapsulates the logic for the pills and accepts the same arguments as the original
pills
function.
- Adds a
reset_key
argument to allow forced updates of the component.
- Example Usage:
- Uses
st.session_state
to store the selected option and a reset key.
- The
reset_selection
function resets the selection and increments the reset key.
- The “Reset Selection” button calls
reset_selection
to force an update.
- Calls
custom_pills
with the appropriate arguments and reset key.
- Updates the selected option in the session state.
This approach ensures the reset works correctly, forcing the pills component to refresh and clear the selection.