How to keep values in a list constant in-between reruns?

I am building an app in which I show a dataframe containing outfit items chosen for the user. I also want to give the user the option to change certain items from said dataframe. Here is how i build the list of items:

items = list()
if (outfit.coat != None):
    items.append(outfit.coat)
if (outfit.top2 != None):
    items.append(outfit.top2)
items.append(outfit.top1)
items.append(outfit.bottom)
items.append(outfit.footwear)
st.dataframe(database.iloc[items])

and here is the part where I want to give the user the option of changing the items out:

with st.expander("Change outfit elements"):
    with st.form("Outfit items"):
        options = st.multiselect('Which items do you wish to change?',database.iloc[items])
        st.write('You selected:', options)
        submitted = st.form_submit_button("Change")
        if submitted:
            st.write("Outfit was changed to:")
            st.dataframe(database.iloc[items])

My problem is that when the submit button is clicked, a rerun occurs and another outfit is chosen, making the selection process useless as the options in the multiselect widget disappear (as they are not there anymore when a new outfit is chosen)

How can I use session states to circumvent this? I have tried:

    if 'items' not in st.session_state:
        st.session_state.items = list()
    
    if (outfit.coat != None):
        st.session_state.items.append(outfit.coat)
    if (outfit.top2 != None):
        st.session_state.items.append(outfit.top2)
    st.session_state.items.append(outfit.top1)
    st.session_state.items.append(outfit.bottom)
    st.session_state.items.append(outfit.footwear)
    st.dataframe(database.iloc[st.session_state.items])

but i get the error "AttributeError: ‘function’ object has no attribute ‘append’ "

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.