Multiselect Field in Sidebar Ignores Selection and Refreshes

Summary

I am writing an app that retrieves data from a sql server and stores it in a dataframe. The user is then allowed to use two filters to filter through this dataframe and look at KPIs based on the filtered data.
The problem I am facing now is with one of the mltiselect filters. When I try to add a selection, the app does take it, it simply refreshes and revers back to the default selection/option.

Code snippet:

# ----- SIDEBAR FILTER & DATA CONTROL ------
st.sidebar.header('Data Control')

# Selecting Distribution Center:
distro_select = st.sidebar.multiselect(
    "Select Distro ID#:",
    options=query_df['WAREHOUSE_ID'].unique(),
    default=899)

# Selecting Staging ID:
store_select = st.sidebar.multiselect(
    "Select Store #:",
    options=query_df['SHIPPING_TO'].unique(),
    default=[110])

# Query the raw db based on user filters from the sidebar:
@st.cache
def filtered_data(query_df, distro_select, store_select):
    filtered = query_df.query(
        "SHIPPING_TO in @store_select & WAREHOUSE_ID in @distro_select")
    return filtered
filtered = filtered_data(query_df, distro_select, store_select)

Expected behavior:

Generate the dataframe based on the selection.

Actual behavior:

overrides my selection and refreshes the app.

Debug info

  • Streamlit version: 1.17.0
  • Python version: 3.11.1
  • Using venv
  • OS version: windows

I’m not 100% sure I’m interpreting this as you intend, so could you show a bit more code so I can see how you are handling query_df between page loads?

If you are updating df_query between selections, then you will be passing a different list of options to the widgets and you could be seeing them reinitialize and thus “going back to default.”

If you expand the script to show how you load/save df_query and may give a simple data frame for me to use as input, I’d be happy to test it out.

@mathcatsand

I solved this issue by caching the query’s result before using it in the app using the python module ‘pickle’. You can see more detailed code and the solution in this thread:

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.