Skip to main content

Dynamic multiselect filters for Streamlit

Project description

Dynamic Multi Select Filters for Streamlit

Open Demo App

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).

How to install and use the package:

  1. Install the package using pip: pip install streamlit-dynamic-filters

  2. Import the DynamicFilters class: from streamlit_dynamic_filters import DynamicFilters

  3. 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'])

  4. Display the filters in your app: dynamic_filters.display_filters()

  5. 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.5 - 28th December 2023

Added by kzielins kzislinsk@gmail.com

  • Hierarchical filter selectors
  • Independent filters with diffrent sessions name

0.1.3 - 28th September

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.

Deprecated

Removed

Fixed

  • The StreamlitApiException that occured when selected values did not exist in the dataset.
  • Possibility to have more than one filter

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_dynamic_filters-0.1.5.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_dynamic_filters-0.1.5-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_dynamic_filters-0.1.5.tar.gz.

File metadata

File hashes

Hashes for streamlit_dynamic_filters-0.1.5.tar.gz
Algorithm Hash digest
SHA256 27b00c88c4bcf06be4f249c2c2ae425fd6eb17d07e3c96881d6c94be10e43e5e
MD5 19a8dbe1c9237ee578db728410a82e61
BLAKE2b-256 a2fc6adf6c7e02a9a139590b58e111823ae3740deb9db35e17436bb9cb45f846

See more details on using hashes here.

File details

Details for the file streamlit_dynamic_filters-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_dynamic_filters-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ca4bbef1fb22811478abf50f23f81e6ce555fb36dd30ce868f73a05ee0d528e5
MD5 bbb11784ffe08dbbe38383764d06e436
BLAKE2b-256 02864b49b85e16c088915a7c175685c39b33183f564597c6ac36490bbc790ac1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page