DynamicFilters
is a class in the streamlit-dynamic-filters
library designed to create dynamic multi-select filters in Streamlit applications. Below is a detailed explanation of its methods, arguments, and usage examples.
__init__(self, df, filters, filters_name='filters')
Initializes the DynamicFilters object with a dataframe and a list of filters.
df
(DataFrame
): The pandas DataFrame on which filters are applied.filters
(list
of str
): List of column names in df
for which filters are to be created.filters_name
(str
, optional): The key name for storing filters in the Streamlit session state. Default is 'filters'
.import pandas as pd
from streamlit_dynamic_filters import DynamicFilters
data = {'Category': ['Fruit', 'Vegetable'], 'Item': ['Apple', 'Carrot']}
df = pd.DataFrame(data)
dynamic_filters = DynamicFilters(df, ['Category', 'Item'])
check_state(self)
Initializes the session state with filters if not already set.
reset_filters(self)
Resets the current filter.
st.button("Reset Filters", on_click=dynamic_filters.reset_filters)
filter_df(self, except_filter=None)
Filters the dataframe based on session state values except for the specified filter.
except_filter
(str
, optional): The filter name to be excluded from the current filtering operation.DataFrame
: Filtered dataframe.filtered_df = dynamic_filters.filter_df(except_filter='Item')
display_filters(self, location=None, num_columns=0, gap="small")
Renders dynamic multiselect filters for user selection.
location
(str
, optional): Location to display filters. Can be 'sidebar'
, 'columns'
, or None
(defaulting to main area).num_columns
(int
, optional): Number of columns to display filters in when location
is 'columns'
. Must be 0 (default) or a positive integer.gap
(str
, optional): Gap between columns when location
is 'columns'
. Can be 'small'
, 'medium'
, or 'large'
.dynamic_filters.display_filters(location='sidebar')
display_df(self, **kwargs)
Renders the filtered dataframe in the main area.
kwargs
: Additional keyword arguments to pass to streamlit.dataframe
.dynamic_filters.display_df()