Access edited data from st.data_editor

I have a streamlit app where I am showing a Pandas dataframe as a editable dataframe using the st.data_editor component.

I am able to edit the values in the table shown using st.data_editor. However I want to access that edited data in my code for further reference.

I am able to get the index of which rows were edited using the edited _rows parmater but not able to access the actual updated data.

Can anyone please help me in how to achieve this functionality?

Not sure what you are looking for exactly. edited_rows is a dictionary and you acces it like any other dictionary. All the data is there as you can see in the example application.

@Gayo

I want to achieve the functionality that is shown here in this demo app.

The issue that I am facing now is that when I change the values in the table that I am displaying using st.data_editor, the entire page reloads for every entry that I change.

Firstly, I want to reload it only after hitting a “Save” button. Second, I don’t want all my other components in my app to reload. Just the table should reload.

What functionality shown there are you unable to achieve?

I am not sure I understand what you mean by “reload”. Your script runs again each time the user interacts with a widget, as explained in the docs:

any time something must be updated on the screen, Streamlit reruns your entire Python script from top to bottom.

This can happen in two situations:

  • Whenever you modify your app’s source code.

  • Whenever a user interacts with widgets in the app. For example, when dragging a slider, entering text in an input box, or clicking a button.

You can avoid the rerun by putting some widgets in a form. But changes made to those widgets won’t be available to the application until the submit button is clicked. There is an example of that in the demo you linked.

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)

        






    


The return value of the widgets in a form is only available in the next rerun, after calling the callback. In the callback, session_state.edited_data still has the old data, you cannot access the edited data from there.