Summary
Hi all, I am trying to create a streamlit app where based on 1 filter selection criteria, I want to populate other filter selections. Then, once the submit button is hit, then I want to proceed ahead with processing the data.
Steps to reproduce
Code snippet:
import streamline as st
import pandas as pd
my_df = pd.DataFrame({
'Name': ['A', 'A', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'D', 'D'],
'Color':['red', 'blue', 'blue', 'black', 'black', 'green', 'blue',
'yellow', 'white', 'green', 'purple']
})
col1, col2 = st.columns(2)
name_selection = col1.multiselect('select names ', my_df.name.unique().tolist(), key='names')
color_selection = col2.multiselect('select color ', my_df.color.unique().tolist(), key='color')
Scenario 1
If I select name
as A then the color selection should be only a list of red
and blue
and not others.
Scenario 2
Similarly, when I choose color as Black first, then I should get only a list of B
and C
in name list. The filter order is dependent on the user.
In general, I have around 5 to 6 filters and once a user selects a filter condition on any one of the multi select columns, then the other filter conditions should automatically update and populate the list.
- How can I achieve this using session_state or on_change() functions?
- Do I need a st.form() for this?
Here is scenario 1 -
And here is scenario 2 -