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)
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.
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:
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.