I’ve been working on a local Streamlit app, and I’ve been encountering a strange issue with the multiselect widget.
I have a multiselect widget in a sidebar, and every time I select something from the list, it collapses, which is annoying.
I then have another multiselect widget in an expander in the main window, and I can select multiple items, and the list only collapses once I click elsewhere, which is how I want all my multiselect widgets to work.
Here is the code snippet for the sidebar widget.
if sample_inclusion_exclusion == 'Include':
sample_options = ['Show all'] + sorted_column_names[sst.selected_name].to_list()
default_sample = 'Show all'
elif sample_inclusion_exclusion == 'Exclude':
sample_options = sorted_column_names[sst.selected_name].to_list()
default_sample = None
if 'sample_selection_list' not in sst:
sst.sample_selection_list = None
# Choose which samples to include/exclude
selected_samples = st.multiselect("Select which samples to " + sample_inclusion_exclusion.lower(),
sample_options,
default=sst.sample_selection_list if sst.sample_selection_list else default_sample,
label_visibility='visible',
key="list_selected_samples",
on_change=lambda: setattr(sst, 'sample_selection_list', sst.list_selected_samples))
sst.selected_samples = selected_samples
Here is the snippet for the widget which is not collapsing.
if 'multiselect_samples_for_bar' not in sst:
sst.multiselect_samples_for_bar = None
multiselect_samples_for_bar = st.multiselect(
"Select up to 10 samples to compare their compound profile",
sst.numeric_columns,
max_selections=10,
default=(sst.multiselect_samples_for_bar
if
(sst.multiselect_samples_for_bar and sst.multiselect_samples_for_bar in sst.numeric_columns)
else
None),
key="multi_selected_samples",
on_change=lambda: setattr(sst, 'multiselect_samples_for_bar', sst.multi_selected_samples)
)