New Component: Dynamic multi-select filters

Hi there, absolute great tool. Congratulations, i was looking for this since a long time.

That’s my situation:

  • I run a streamlit multipage app with two pages/apps in /pages

  • both use dynamic filters, but have different data sources and different associated filter sets

  • Both apps on their own run flawlessly

  • When I switch to the other app I get an error:
    KeyError: ‘Produkt’ (the first filter from the app I clicked on first)

    File “/home/src/pages/1_Chemstation.py”, line 41, in
    dynamic_filters.display_filters()

    File "/opt/conda/lib/python3.10/site-packages/streamlit_dynamic_filters/dynamic_filters.py", line 160, in display_filters options = filtered_df[filter_name].unique().tolist()

    File "/opt/conda/lib/python3.10/site-packages/pandas/core/frame.py", line 4107, in __getitem__ indexer = self.columns.get_loc(key)

    File "/opt/conda/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 3819, in get_loc raise KeyError(key) from err

So my guess is that:

  • When the first app is loaded it stores the filters in the cache.
  • When the second app is loaded it retrieves the cached filters, but they are not present in the second app hence the key error.

Is there a way to isolate these two apps from each other so that both can run individually within the multipage setup?

Thank you all in advance.

This problem can solved via the following code placed before all the dynamic filters code blocks:

listOfYourFilters = ['filter1', 'filter2']

# Check if there is an empty session_state
if len(st.session_state) == 0:
    pass
else:
    # Check if any site specific filters are in the session_state, if so go on and do nothing
    if bool(set(listOfYourFilters).intersection(st.session_state)) == True:
        pass
    else:
        # If there is no site specific key (filters) in the session state it's the wrong session state
        # and it has to be cleared.
        for key in st.session_state.keys():
            del st.session_state[key]

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