New Component: Dynamic multi-select filters

Yes it seems so.
Also perhaps st.session_state.filters can potentially conflict in an app which might accidentally is also trying to use its own filters variable in sessions state. Should be renamed to something more explicit/unique like st.session_state.streamlit_dynamic_filters which will have a much lesser chance of an accidental clash.

2 Likes

thanks! I just merged your commit. Also, I replaced st.experimental_rerun with st.rerun in the newest version streamlit-dynamic-filters 0.1.4

4 Likes

Really nice implementation of the filter.
Is there any functionality to change the colours ?

2 Likes

thank you, what colors exactly do you mean? You can use Streamlit theme in config.toml file to change colors of the components.

3 Likes

Nice !

I have a question though. How do i display two differents dataframe with the component on a same page ? I get a " There are multiple identical st.multiselect widgets with the same generated key." an ddidn’t finfd how to pass a key argument to the multiselect ? Maybe this is a stupid question… (did not find any way tho add it. if location == ‘sidebar’:
with st.sidebar:
selected = st.multiselect(f"Select {filter_name}“, options, default=st.session_state.filters[filter_name])
elif location == ‘columns’ and num_columns > 0:
with col_list[counter - 1]:
selected = st.multiselect(f"Select {filter_name}”, options, default=st.session_state.filters[filter_name])

2 Likes

This is how you pass a key argument:

selected = st.multiselect(f"Select {filter_name}", options, default=st.session_state.filters[filter_name], key="some key")
1 Like

Hi @Fabienne you’re right, up until now there was no option to have more than one set of filters in the same app. One of the contributors added this functionality in the new version 0.1.5. After you pip install it, just pass in a filters_name argument.

dynamic_filters = DynamicFilters(df, filters=[‘col1’, ‘col2’, ‘col3’], filters_name=‘filters1’)

3 Likes

Hi Oleksandr

Have been trying out this component and its really cool.

I wonder if its possible to access the filtered DataFrame as input for drawing a graph or a map?

Take care

Audun

2 Likes

Hi! thanks, glad to hear!
Yes, you should be able to get the filtered DataFrame using filter_df()

See example code below:

dynamic_filters = DynamicFilters(df, filters=['col1', 'col2', 'col3'], filters_name='my_filters')
dynamic_filters.display_filters(location='sidebar')
dynamic_filters.display_df()

# save filtered df as new variable
new_df = dynamic_filters.filter_df()
3 Likes

It’s great component!
I wonder how could I customize the filters’ names?

2 Likes

I think right now there is no option to customize the filters’ name, by default it will be “Select {your column name}”

1 Like

hi there! thank you for this wonderful component!

quick question! is there any way to use a filter selection as a parameter and add it to the title for example?

1 Like

Hi! I’m not sure I understand what you mean, can you provide an example of what you’re trying to achieve?

1 Like

image

so let’s say that the user selects the timeframe, “05” for example.

I want to use this “05” and add it to my title (and have it change when the user changes it)

I actually managed this by accessing the filtered dataframe like this:

{dynamic_filters.filter_df()[‘timeframe’].iloc[0]}

1 Like

Hey! Good job on the component :smiley: Could you please add sorting the values of the chosen filters? I mean this part:

        for filter_name in st.session_state[self.filters_name].keys():
            filtered_df = self.filter_df(filter_name)
            options = filtered_df[filter_name].unique().tolist()

I use a date column as filter, and it would make searching for a specific one much easier. Thanks!

1 Like

Thanks Oleksandr, worked fine.

Is there a simple way to sort the values in the filters to make it easier to find specific values to filter on?

Take care

Audun

1 Like

Hi there! I added alphabetical sorting of filter labels by default in the new version. Please use pip install streamlit-dynamic-filters==0.1.6

CC: @domciak

2 Likes

Thanks. This works really well. Is there a way to pass the filtered df to something else. I’m using xlwings to give the user an option to open the data in excel, but I’d like to pass the filtered df to xl wings to open.

2 Likes

Hi Oleksandr, I really like your component, it’s very useful and it works really well, but I was wondering if there was a way to make it work on st.data_editor and not only on st.dataframe, in order to return an editable dataframe after filters have been applied.
Thanks in advance

2 Likes

Hi @Daniel_Turner did you try this approach?

dynamic_filters = DynamicFilters(df, filters=['col1', 'col2', 'col3'], filters_name='my_filters')
dynamic_filters.display_filters(location='sidebar')
dynamic_filters.display_df()

# use filtered df in xlwings or other libraries
new_df = dynamic_filters.filter_df()
2 Likes