Undo changes in st.experimental_data_editor

Hello,
I have created a pandas dataframe (‘df’) and I show it in my app with:

edited_df = st.experimental_data_editor(df, num_rows=“dynamic”, key=)
The user can add/remve and edit the rows, lets say I want to add an “undo” button to return to the previous state, how should I implement it?
I am aware that each change is traced in st.session, but if I change the variable "edited_df’ in the code, it still remains the same:( can someone enlight me here?

Regards,

Kobi

1 Like

You can’t manually assign state to a data editor at this point. If you wanted to include step-wise undo functionality, you would have to keep track of the new resultant dataframe with each edit so you could reinitialize the data editor to match the previous result.

If on the other hand you mean that you want to undo all changes at the click of a button, you can simply change the key of the data editor to force it to create a new one with the same starting dataframe.

I’m not following your solution :frowning: if my editable dataframe is created by reading a ‘df’ that had not been changed through the code, next re-run, I would expact that ‘edited_df’ be re-created and remains the same, or is it because I passes a key that it saved the user’s changes?

The data editor is stateful. Rerunning a page does not reset widgets in general. For any widget, if you want to force it back to its beginning state you have to either:

  1. Manually set its state back to the default value (not possible with the data editor).
  2. Destroy and recreate the widget. This can be done by either removing it from the page and putting it back (probably not appropriate in your case), or by changing one of its creation parameters such as changing the key.
1 Like

Can you provide a simple example of how to change the key?

Additionally, how would I then reference the key in a callback function if it changes on reruns.

My goal is to essentially have a reset button for the experimental data editor to reset all the checkboxes in the displayed experimental data editor to False.

You can toggle between two keys and just check which one exists, or you can create a separate value in session state to record what the current key is. For example:

import streamlit as st
import pandas as pd

if 'df' not in st.session_state:
    df = pd.DataFrame({
        'a': [1, 2, 3],
        'b': [4, 5, 6],
        'C': [False, False, False]
    })
    st.session_state.df = df
    st.session_state.key = 0

df = st.session_state.df

def reset():
    st.session_state.key += 1

st.experimental_data_editor(df, key=f'editor_{st.session_state.key}')

st.button('Reset', on_click=reset)

st.write('If you need to get the data editor\'s edits from session state...')
st.write(st.session_state[f'editor_{st.session_state.key}'])
1 Like

Is storing the df in st.session_state a requirement for using this technique?

The example here works for me but as soon as I use df without storing to session_state it stops working.

You need to have the key in session state (st.session_state.key), but this would work the same if I statically defined the same df at the beginning of the script. Can you show how you’ve modified it that it stops working for you?

Thanks. I’ve got it working now. I’d swear that my now working version was one of the attempts that didn’t before. (So - silly user error.) Problem solved.

Working Version (using data_editor):

import pandas as pd
import streamlit as st

if 'key' not in st.session_state:
    st.session_state.key = 0

df = pd.DataFrame({
    'a': [1, 2, 3],
    'b': [4, 5, 6],
    'C': [False, False, False]
})

def reset():
    st.session_state.key += 1

st.data_editor(df, key=f'editor_{st.session_state.key}')

st.button('Reset', on_click=reset)

st.write('If you need to get the data editor\'s edits from session state...')
st.write(st.session_state[f'editor_{st.session_state.key}'])
1 Like

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