I’m using streamlit 0.84.1.
My project loads a dataframe with a unique title on each row, and the user is allowed to modify the “subscribed” status of each row (all set to FALSE in the example below):
Currently when the “subscribed” status gets changed, rerunning the app from top to bottom clears those edits and resets everything. So I’ll need to use session state to preserve those changes across re-runs and not lose that information.
However, I can’t figure out how to dynamically add to session state. The documentation example of using a counter worked because the counter variable always had the same name. Here, I think I want my [key] to be the title, and [value] to be the new “subscribed” state. (Users can change the “subscribed” status of the same title multiple times, but I haven’t figured out how to track that yet.)
if 'Developmental Biology' not in st.session_state: st.session_state['Developmental Biology'] = 'TRUE'
So, in my MWE below, I need some advice on how to proceed within the
if st.button block. How do I get
title to show as different names each time through the loop, such as
import streamlit as st import pandas as pd @st.cache(allow_output_mutation=True, suppress_st_warning=True) def load_data(file): st.write("New file loaded") return pd.read_csv(file)#, sep=',', encoding='utf-8') #Process the data in cached way to speed up processing #load data file = "test_10lines.csv" df = load_data(file) #change the Boolean TRUE/FALSE to simply uppercase strings df['subscribed'] = df['subscribed'].astype(str) df['subscribed'] = df['subscribed'].str.upper() df #offer user the journal titles, and they select one or multiple to edit at a time selected_titles = st.multiselect('Journal Name:', pd.Series(df['title']), help='Displayed in order provided by the underlying datafile') st.write(selected_titles) #what to change 'subscribed' to radiovalue = st.radio("Change 'Subscribed' status to:", ['TRUE', 'MAYBE', 'FALSE']) if st.button('Commit change!'): for title in selected_titles: if title not in st.session_state: #initialize counter the first time st.session_state.df['title'] = 'test' title_filter = (df['title'] == title) df.at[title_filter, 'subscribed'] = radiovalue df