Dynamic multiselect filters for Streamlit
Project description
Dynamic Multi Select Filters for Streamlit
Custom component to create dynamic multiselect filters in Streamlit. The filters apply to a dataframe and adjust their values based on the user selection (similar to Google Sheets slicers or Only Relevant Values in Tableau).
Basic documentation is available at https://arsentievalex.github.io/streamlit-dynamic-filters/
How to install and use the package:
-
Install the package using pip:
pip install streamlit-dynamic-filters
-
Import the
DynamicFilters
class:from streamlit_dynamic_filters import DynamicFilters
-
Create an instance of the
DynamicFilters
class and pass the dataframe and the list of fields that will serve as filters:dynamic_filters = DynamicFilters(df, filters=['col1', 'col2', 'col3', 'col4'])
-
Display the filters in your app:
dynamic_filters.display_filters()
-
Display the filtered dataframe:
dynamic_filters.display_df()
Sample usage with sidebar filters:
import streamlit as st
import pandas as pd
from streamlit_dynamic_filters import DynamicFilters
data = {
'region': ['North America', 'North America', 'Europe', 'Oceania',
'North America', 'North America', 'Europe', 'Oceania',
'North America', 'North America', 'Europe', 'Oceania'],
'country': ['USA', 'Canada', 'UK', 'Australia',
'USA', 'Canada', 'UK', 'Australia',
'USA', 'Canada', 'UK', 'Australia'],
'city': ['New York', 'Toronto', 'London', 'Sydney',
'New York', 'Toronto', 'London', 'Sydney',
'New York', 'Toronto', 'London', 'Sydney'],
'district': ['Manhattan', 'Downtown', 'Westminster', 'CBD',
'Brooklyn', 'Midtown', 'Kensington', 'Circular Quay',
'Queens', 'Uptown', 'Camden', 'Bondi']
}
df = pd.DataFrame(data)
dynamic_filters = DynamicFilters(df, filters=['region', 'country', 'city', 'district'])
with st.sidebar:
st.write("Apply filters in any order 👇")
dynamic_filters.display_filters(location='sidebar')
dynamic_filters.display_df()
Demo GIF:
Sample usage with columns:
import streamlit as st
import pandas as pd
from streamlit_dynamic_filters import DynamicFilters
data = {
'region': ['North America', 'North America', 'Europe', 'Oceania',
'North America', 'North America', 'Europe', 'Oceania',
'North America', 'North America', 'Europe', 'Oceania'],
'country': ['USA', 'Canada', 'UK', 'Australia',
'USA', 'Canada', 'UK', 'Australia',
'USA', 'Canada', 'UK', 'Australia'],
'city': ['New York', 'Toronto', 'London', 'Sydney',
'New York', 'Toronto', 'London', 'Sydney',
'New York', 'Toronto', 'London', 'Sydney'],
'district': ['Manhattan', 'Downtown', 'Westminster', 'CBD',
'Brooklyn', 'Midtown', 'Kensington', 'Circular Quay',
'Queens', 'Uptown', 'Camden', 'Bondi']
}
df = pd.DataFrame(data)
dynamic_filters = DynamicFilters(df, filters=['region', 'country', 'city', 'district'])
st.write("Apply filters in any order 👇")
dynamic_filters.display_filters(location='columns', num_columns=2, gap='large')
dynamic_filters.display_df()
Demo GIF:
0.1.7 - 1st August 2024
Added
- Key for each elements in filters, so that multiple DynamicFilters can be used in one app (contribution by @vikashgraja)
- reset_filters function (contribution by @ragchuck)
0.1.6 - 27th March 2024
Added
- Sorted alphabetically filter labels
0.1.5 - 28th December 2023
Added by kzielins kzislinsk@gmail.com
- Hierarchical filter selectors
- Independent filters with diffrent sessions name
0.1.3 - 28th September 2023
Added
- Ability to specify filter location in display_filters(). The filters can be either displayed in sidebar, main area or columns.
- Error handling of invalid arguments in display_filters().
Changed
- Renamed filter_except() to filter_df(). The function returns a filtered df.
Fixed
- The StreamlitApiException that occured when selected values did not exist in the dataset.
- Possibility to have more than one filter
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for streamlit_dynamic_filters-0.1.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f67a6f79bd4c5216b1a0fdf42ca6c963ee5829bcd97ad579c4ca71f58f4c5196 |
|
MD5 | 11c3f829186a02e2141101046c53da34 |
|
BLAKE2b-256 | c2e89a188ae67a9a23c6ebaf1dc84329a61e4fa8ade36b5b3d699f583a8cb75d |
Hashes for streamlit_dynamic_filters-0.1.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ea0b27fe28f05fdfe7dde188a0d75bcf8a48a6da06d84b2eda82c04a94c8cff |
|
MD5 | ff4a4477224f600cf5cc144a6eb9a8ad |
|
BLAKE2b-256 | cab237e4d93ebea07566a3a00103f9b28fa2d7cea78d393b3a0ff95ae3097a38 |