"reset" experimental_data_editor to original values

Would like to reset an experimental data editor to the original passed in dataframe. Once edits are made, I want the ability for the user to “abandon” those edits

I’ve tried the steps from here:

if 'user_data' not in st.session_state:
            st.session_state['user_data'] = user_data

modified_df = st.experimental_data_editor(st.session_state['user_data'], num_rows='dynamic', key=data_editor_key)

if st.button('Reset Changes'):
    st.session_state['user_data'] = user_data
    st.experimental_rerun()

with no success.

Any thoughts?

1 Like

You can assign a key to your data editor, then change the key when you want to force Streamlit to create a new, “fresh” one. By using a different key, Streamlit will just “start over” with the data editor.

2 Likes

Could you not save the original dataset in a different key such as ‘original_data’ within the session state and if the user abandons the changes then update the editable df with the original_data from the session state?

@bilalmussa89 I tried that as well - no luck. But thanks.

Works like a champ, thanks!

Are there any memory/performance effects to this approach? i.e., if the user does it alot, or with a large DF, will those old ones hang around in memory?

I can’t say for 100%, but in general widgets have a cleanup process when they are no longer rendered. Hence, Streamlit should discard that widget data associated to the old key when you change it to a new one. You could profile the memory in a test case to be certain…

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