Manas Jadhav
Manas Jadhav

Reputation: 301

How can we create this type of filter checkbox in Streamlit Python?

This is the output it should look like-

I have tried doing this:

import pandas as pd
import streamlit as st
import plotly.express as px
from PIL import Image

st.set_page_config(page_title=' Sales Report ')
st.header('Sales Report')
st.subheader('Results')

df=pd.read_csv('data')
st.dataframe(df)

I got the table showing up on Website, but how can I add that filter checkbox?

Upvotes: 0

Views: 1482

Answers (1)

LazyClown
LazyClown

Reputation: 822

Explanation in the code

import streamlit as st
import pandas as pd
import numpy as np
np.random.seed(0)

# toy data
def get_data():
    df = pd.DataFrame(np.random.randn(10, 3), columns=["Category", "A","B"])
    df["Category"] = np.random.choice(['Apple', 'Banana', 'Grapes'], 10)
    return df

df = get_data()
st.subheader("Filtered Dataframe")
st.sidebar.write('Select Filter')

cat_list = df.Category.unique()
val = [None]* len(cat_list) # this list will store info about which category is selected
for i, cat in enumerate(cat_list):
    # create a checkbox for each category
    val[i] = st.sidebar.checkbox(cat, value=True) # value is the preselect value for first render

# filter data based on selection
df_flt = df[df.Category.isin(cat_list[val])].reset_index(drop=True)
if df_flt.shape[0]>0:
    st.dataframe(df_flt)
else:
    st.write("Empty Dataframe")

enter image description here

Upvotes: 2

Related Questions