Hi guys I am new too streamlit and I extremely delighted to part of such a vibrant community.
Currently I am working on an streamlit application and facing a problem. Well the thing is that i have multiple selectbox options and I am filtering my dataframe on the basis of these values. Currently i am using a top down approach to do the filtering meaning that my later selectbox options values are dependent on the basis of the previous selection ox selection. But now i want to modify it in such a way so that whenever any selectbox option is selected all the rest of selectbox options get updated accordingly.
I am sharing the code with my current approach. Any suggestion will be much appreciated.
Thanks.
Hi @Hassan_waseem, and welcome to the Streamlit forum!
Please if you wouldn’t mind sharing the code, it would be easier for me review/debug.
Best,
Charly
import pandas as pd
import streamlit as st
data = {'feature_1': ['A','B','A','C','D','M','A','B','A','C'],
'feature_2': ['Zack','Hassan','Ali','Hassan','Hassan','Ali','Zack','Hassan','Hassan','Hassan'],
'feature_3': [0, 1, 0, 1, 1, 1, 0, 0, 1, 1],
'feature_4': [10,10,10,10,10,20,20,20,20,20],
'Result_count': [1000,2000,3330,100,250,20,990,160,10,200]}
df = pd.DataFrame(data)
#feature_1 filters
feature_1_val = df["feature_1"].unique()
feature_1ed = st.sidebar.selectbox('feature_1', feature_1_val)
#filter out data
df = df[(df["feature_1"] == feature_1ed)]
#feature_2 filters
feature_2_val = df["feature_2"].unique()
feature_2 = st.sidebar.selectbox('feature_2', feature_2_val)
#filter out data
df = df[(df["feature_2"] == feature_2)]
#feature_3 filters
feature_3_val = df["feature_3"].unique()
feature_3 = st.sidebar.selectbox('feature_3', feature_3_val)
#filter out data
df = df[(df["feature_3"] == feature_3)]
#feature_4 filter
feature_4_val = df["feature_4"].unique()
feature_4 = st.sidebar.selectbox('feature_4',feature_4_val)
#filter out data
df = df[(df["feature_4"] == feature_4)]
@Charly_Wargnier any suggestions?
If you wanted your dataframe to reflect all your widgets’ values, you can go with:
df = df[(df["feature_1"] == feature_1ed) | (df["feature_2"] == feature_2)] # etc
st.write(df)
Would that work for you?
Best,
Charly
No it wont work. The problem is that i want to update my selectbox options according to the selection done on other selection box options. For example if some option in selectionbox for feature_3 is selected the options for selectionbox for feature_1 , feature_2 and feature_4 will be updated accordingly.
I see. Have you tried with nested conditionnals?
if feature_1 == "value":
feature_2 = st.sidebar.selectbox( #specific values
feature_3 = st.sidebar.selectbox( #specific values
#etc...
Charly
I have avoided using it. As the plan is to connect the application with a database. Hence I need to make the application and the selectbox options dynamic
Thanks alot
Hi Hassan,
I’ve had a chat internally and while it seems like something that is possible w.o any nested conditionals, I’ve yet to get my hand on a code snippet to share.
I’m sure it’s possible to achieve similar results w/ the new state functionality, it you wanted to give it a try
Best,
Charly
I will try the new state functionality. And it would be much appreciated if you could share a code snippet if possible. Thanks
Try using loops
I have drop this idea for now and have opted for hierarchical filtering for option. Any ways thanks for all the assistance
@Hassan_waseem I just created a component which does the dynamic filtering. See more here - https://discuss.streamlit.io/t/new-component-dynamic-multi-select-filters/49595