Access edited data from st.data_editor

Thanks @Goyo for the response. I have put my data editor component inside a form and now the page reloads only after clicking the form button.The “Recalculate and save” button in the form recalculates everything based on the edited data and saves it in the session_state variable.

However, the recalculations are visble after clicking the button twice.

I am attaching my code for reference.

Please let me know where I am going wrong or if there is any other way to achieve the same.
Also let me know which all blocks to cache and how. I have not familiar with caching in Streamlit.

import streamlit as st

session_state = st.session_state


dropdown = # code for st.selectbox()



if dropdown:
    df= pd.read_csv("filename.csv")

    #some extra preprocessing and function calls

    df = some_preprocess(df)

    grouped_df = df.groupby("Group").agg({"column_1":"sum"})

    grouped_df["col2"]=1



    if "original_data" not in session_state:
        session_state.original_data= grouped_df

    if "edited_data" not in st.session_state:
        session_state.edited_data= grouped_df.copy()
    

    def save_edits():

        edited_df = session_state.edited_data

        #recalculation of col2 based on edits made in the table
        edited_df["col2"] = edited_df["col1"]+3

        session_state.edited_data=edited_df
        session_state.original_data=session_state.edited_data
    

    with st.form("Table"):
        session_state.edited_data = st.data_editor(session_state.original_data,num_rows="dynamic")

        st.form_submit_button("Recalculate and save",on_click=save_edits)

        






    


1 Like