Summary
I have a multi-select drop down with default value on the side bar. I am trying to persist the selected value and show the selected value in the drop down after I switch to another page and switch back.
Folder structure:
Current issue
however, the drop down disappeared after I select another value “SF” (which makes sense because now session_state has a value so it doesn’t run the function to show the multi-select drop down). What I am confused about is that, if I want the multi-select drop down to show at all time, I would need to run selected_values
in my load_functions.py
at all time… but then upon changes, I would need to replace the current hard coded default value NYC
with the session_state value that stored the user selected input. I think I may need to use some action like on_change and I am struggling with that. Also, does it make sense to have the code for the first and second page side bar to be exactly the same? I simply just want a side bar with selected drop down value to be persistent during the session and shown on the side bar
1-apple.py
import streamlit as st
from load_functions import selected_values, load_df
if 'df' not in st.session_state:
df = load_df()
st.session_state['df'] = df
else:
df = st.session_state['df']
with st.sidebar:
if 'cities' not in st.session_state:
selected_cities = selected_values(df)
st.session_state['cities'] = selected_cities
else:
selected_cities = st.session_state['cities']
st.write(f"Session state: {st.session_state['cities']}")
2-banana.py
import streamlit as st
from load_functions import selected_values, load_df
if 'df' not in st.session_state:
df = load_df()
st.session_state['df'] = df
else:
df = st.session_state['df']
with st.sidebar:
if 'cities' not in st.session_state:
selected_cities = selected_values(df)
st.session_state['cities'] = selected_cities
else:
selected_cities = st.session_state['cities']
st.write(f"Session state: {st.session_state['cities']}")
load_functions.py
import streamlit as st
import numpy as np
import pandas as pd
def selected_values(df):
cities = np.sort(df['city'].unique())
city_container = st.container()
all_cities = st.checkbox("Select all", key=1)
if all_cities:
selected_cities = city_container.multiselect("Select one or more city:",
cities,
cities)
else:
selected_cities = city_container.multiselect("Select one or more city",
cities,
default=['NYC'])
return selected_cities
def load_df():
# initialize data of lists.
data = {'city': ['NYC', 'NYC', 'SF', 'Plano', 'SLC', 'SF'],
'age': [20, 21, 19, 18, 21, 22]}
# Create DataFrame
df = pd.DataFrame(data)
return df
FROM:
TO: (When I select SF)
Expected result:
When I select SF, I want the drop down selection to show only SF and when I switch to page banana, I want SF to show, when I switch back to page apple, I want SF to show.
More:
I also tried to modified some code in load_functions.py
but it still doesn’t work as expected. Also tried
st.session_state.my_widget_key = st.session_state.my_widget_key