I am trying to create an editable dataframe by loading data from a csv file into a dataframe, and then passing it to the data_editor, which would return df_edited.
What I would like to have as a result, is this df_edited to be saved again and reused in the data_editor. Basically, for every change to be saved in the csv and reflected back in the data_editor.
I am noticing that I have to edit the dataframe twice for the changes to be reflected. I believe this is due to the code running top down every time an edit is detected, and I am not quite sure how to ensure that the edited dataframe would load up after saving.
I have tried using states and callbacks, but so far no luck.
I’m experiencing the same issue. When reading CSV or JSON files, the changes are only saved after editing the st.data_editor twice. I attempted to use @tonykip’s solution, but it didn’t work. @Paul_Bezko, were you able to resolve this issue?
Here’s an update version using session state:
if 'df' not in st.session_state:
st.session_state.df = pd.DataFrame(data=pd.read_csv("data/data.csv"))
edited_df = st.data_editor(st.session_state.df, use_container_width=True, hide_index=True)
edited_df.to_csv("data/data.csv", index=False)
It kinda works, but as soon as I leave the page and come back to it, the data_editor gets the value from the session_state and updates the csv with the old value.
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.