New Component: Dynamic multi-select filters

Hi @saasyp at the moment, the component supports only st.dataframe, but you could probably do something like this:

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

new_df = dynamic_filters.filter_df()
st.data_editor(new_df)
2 Likes

Thanks. That works great!

2 Likes

I’m sorry for the silly question! Your suggestion is what I was looking for, thanks for your help!

2 Likes

Hi Oleksandr,

I am using DynamicFilters. I am getting “Row index out of range” error in Devtools. I copied the DynamicFilters code into a separate file and ran it. I still get the errors.

The error messages appear when I start filtering using the multiselect boxes.

As I select additional filters, I get more errors. These errors are only seen in Devtools. The app works fine. There are 7089 rows in the dataframe.

Row index out of range: 7089 (listed 9 times)
Row index out of range: 2 (listed 9 times)
Row index out of range: 3 (listed 9 times)
Row index out of range: 4 (listed 9 times)

Row index out of range: 4136 (listed 9 times)

Isolated code:
import streamlit as st
import pandas as pd
import altair as alt
from streamlit_dynamic_filters import DynamicFilters
import warnings
warnings.filterwarnings(‘ignore’)

@st.cache_data
def load_data():
data = pd.read_csv(“US_Breweries.csv”)
data[‘postal_code’] = data[‘postal_code’].str.slice(0,5)
data = data[[‘name’,‘brewery_type’,‘address_1’,‘city’,‘state’,‘postal_code’,‘phone’,‘website_url’]].copy()
return data

df = load_data()
selected_breweries = df.copy()

st.write(“Apply filters:”)

dynamic_filters = DynamicFilters(selected_breweries, filters=[‘state’, ‘city’, ‘brewery_type’, ‘postal_code’, ‘name’], filters_name = ‘filters1’)

dynamic_filters.display_filters(location=‘columns’, num_columns=2, gap=‘large’)

selected_breweries = dynamic_filters.filter_df()

st.write(“”)
st.write(“”)

with st.expander(“Data Preview”):
st.write(“Hover over column name to display icons on the right for download, search, fullscreen”)
st.write(“Click columns names to sort”)
dynamic_filters.display_df()

st.write(“”)

Thanks

1 Like

HI Oleksandr_Aresentiev

I’ve been effectively utilizing the Dynamic Filter package. However, I encountered limitations in adjusting positions and applying names to the filters(not dataframe column name). Consequently, I’ve developed a solution to these issues. I’m curious about the best practices for distributing this enhancement. Any guidance or suggestions would be greatly appreciated.

2 Likes

Hi dongbang_ban,

Have you noticed any Row out of index errors when you look at devtools after using the DynamicFilters?

1 Like

Hii, @Oleksandr_Arsentiev, can this dynamic filters applied to multiple dfs?

1 Like

Hi @Oleksandr_Arsentiev I’ve been using the dynamic filters lib for a while and it’s amazing! I just have one question: is it possible to use the same filters in multiple pages, where I can change them in one page and it applies to all pages with the filter?

2 Likes

It’s not working properly inside an experimental_fragment, i noticed there is a st.rerun inside this package to trigger the filter change but what if we call the required filters inside an experimental_fragment where the filters dynamically change value without affecting other charts and tables once the user is clicked apply filter button, it will appears so i have commented the re-run and call the display filter function inside a experimental_fragment funciton but now the filters are properly changing the values dynamically but since re-running is happening the charts and tables are disappearing

1 Like

Hello - is anyone else having issues with null values in fields? I get a TypeError: ‘<’ not supported between instances of ‘float’ and ‘str’ when trying add fields in from a dataframe that contain nulls. Is there any way to handle these? Thanks!

Hi @AmazingAvocado I was able to replicate the errors, but honestly don’t know what causes them/how to fix it. Seems to be coming from JS.

Hi @shreesaraandevarajan currently only one df is supported, but feel free to contribute by opening a pull request

Hi @ferreirapd thanks, glad to hear! Did you try using the same key in filters_name argument when you initiate a DynamicFilters class? This is the name that’s saved to the st.session_state so I believe it should apply to multiple pages.

dynamic_filters = DynamicFilters(df, filters=['region', 'country', 'city', 'district'], filters_name='geo_filters')

I think that’s because the code tries to sort the values for each filter. The quickest fix would be to just replace None values with some string representation

1 Like

Hi, is there a way to dynamically create a Date Range filter? I would like to have a slider created dynamically for a date range if the df column is a date type.

Hi, right now the component supports only multiselect filters

1 Like

Does this component also handle “Numeric” filtering? Column C has values between A and B?

Similarly for dates (date between X and Y)?

I think you would want different type of filter for date and numeric values rather than multiselect? Currently it supports only multiselect and it’s best for categorical/hierarchical values.

1 Like

I saw the “filter_dataframe” work done by some streamlit folks. It’s very simple code and easy to implement and supports categorical, date range, numeric range and regex based filters. It also allows to select all columns as we need dynamically so your view does not get cluttered.

Check this: st-filter-dataframe/streamlit_app.py at main · tylerjrichards/st-filter-dataframe · GitHub
(Check the filter_dataframe function)

Blog with visuals: Auto-generate a dataframe filtering UI in Streamlit with filter_dataframe!

I chose this simple implementation over the streamlit-dynamic-filters module because of the date and numeric support. The code itself is extremely simple. May be 50 lines… Thats it.

@Oleksandr_Arsentiev thank you very much for this great feature! :star_struck:
I’m exploring the dynamic filters and I would like to ask you how is it possible to add the “Reset All Filters” button?


I know that you added it but I couldn’t find the code. I tried to use the st.session.state but I wasn’t sucessful.

Thank you!