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 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:
Manually set its state back to the default value (not possible with the data editor).
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.
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}'])
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}'])
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.