Aggrid table inside expander does not keep changes made after closed

Hello All,

I have an editable aggrid table within an expander inside a form. If I edit the table, and then close the expander and open it again, the data goes back to the original input. If I edit the table, click submit on the form, then close the expander, the data goes back to the original input but if I click submit a second time, then close and open the expander, the data is updated. Does anybody know a fix?

streamlit-stream_testing_js-2023-03-22-16-03-08

Here is the sample code:

import streamlit as st
import pandas as pd
from st_aggrid import AgGrid, GridOptionsBuilder,DataReturnMode,GridUpdateMode

if "stored_df" not in st.session_state:
    st.session_state.stored_df = pd.DataFrame

def main():

    if st.session_state.stored_df.empty:
        st.session_state.stored_df = pd.read_csv("test_params.csv")

    
    form = st.form('Form')
    expander1 = form.expander('Expander')
      
    with expander1:
        gb = GridOptionsBuilder.from_dataframe(st.session_state.stored_df)
        gb.configure_default_column(editable=True)
        data = AgGrid(
            st.session_state.stored_df,
            gridOptions=gb.build(),
            key=f'_{0}_ag',
            reload_data=False,
            data_return_mode=DataReturnMode.AS_INPUT,
            update_mode=GridUpdateMode.MODEL_CHANGED,
        )
        st.session_state.stored_df = data.data

    submit = form.form_submit_button('Submit')


if __name__ == '__main__': 
    main() 

Hey @shivpalit-das42,

Thanks for sharing this question. Can you update your code snippet to be runnable? (We don’t have access to test_params.csv.)

I think the behavior you’re seeing is a result of the fact that session state isn’t being updated after each time the user changes the aggrid cell value + the app will rerun every time the user clicks the “Submit” button. You could resolve this by adding something to update session state when the user changes the value of the aggrid cell(s).

One other factor to consider is that there’s currently a bug with expanders and session state that could be playing a role – you can rule this out as a root cause by temporarily removing the expander and seeing if that changes the behavior.