Experimental data editor

Summary

Hello All,

Wondering if someone can assist me with the experimental_data_editor widget. I’m experiencing a strange behaviour where when making changes to the data, they revert back to their original value on the first change, but then the change is persisted on a second change.

I’ve tried all sorts of attempts using forms, session_state but cannot get the functionality working as expected.

Steps to reproduce

Code snippet:

import streamlit as st
import pandas as pd
from pandas.api.types import CategoricalDtype

my_category_choices = CategoricalDtype(categories=['A', 'B', 'C', 'D'], ordered=True)

#df  = pd.DataFrame({
#    'my_category':['A','B','C','B'],
#    'my_score':[0.6,0.5,0.4,0.3],
#})

df = pd.read_csv('control.csv')

df['my_category'] = df['my_category'].astype(my_category_choices)

output_df = st.experimental_data_editor(df)

output_df.to_csv('control.csv', index=False)

If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Change the values multiple times in the first column using the drop down menus. Note how values revert back to their prior value on every other change.

Expected behavior:

The point is to read a csv, make changes to it, then commit the changes back to the csv. It could be committed back automatically, or on a button press.

Actual behavior:

On every second change, the edited value reverts back to its pre-change value.

Debug info

  • Streamlit version: (get it with $ streamlit version) 1.18.1
  • Python version: (get it with $ python --version) 3.10.9
  • Using Conda? PipEnv? PyEnv? Pex? PIP
  • OS version: MacOS, Windows 10
  • Browser version: Safari, Chrome

So far I don’t think the experimental data editor can update the CSV i also tried it a lot, try using cache

Managed to solve this using session state, replacing the last two lines of the code I posted with this:


if 'exp_data_frame' not in st.session_state:
    st.session_state.exp_data_frame = st.experimental_data_editor(df)
    output_df = st.session_state.exp_data_frame

else:
    output_df = st.experimental_data_editor(
        st.session_state.exp_data_frame
        ) 

output_df.to_csv("control.csv", index = False)

3 Likes

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