Data_editor not changing cell the 1st time but only after the second time

The problem seems to be common to those trying to use the data_editor widget/tool – not getting the cell to change the 1st time but the second time it works. I have seen the responses to this but have not seen a solution. I have a session state defined with the and I have a on_change function/def setup although I am not sure what the on_change function should do other than change the session state to the data_editor object.

Could someone please walk thru this procedure for us in a complete working streamlit file?

1 Like

There are several examples in the docs.

1 Like

Yes there are some examples of how to use the data_editor, and that is helpful but can we show how to use a session state, with a data editor and a on_change within a data_editor. How these three items work together to retain edited values within a data_editor tool.
I have done this (setup a session state, setup a data_editor and a on_change function call) but still have problems. The 1st time time I change the data_editor the values do not change bit then the 2nd time I change the data_editor the value change and are retained.

1 Like

The modified data is the return value of data_editor(). I don’t know what you are doing in the on_change callback or why you think you need one.

1 Like

because I thought the on_change would execute before anything else - thinking that that was why the data_editor was not returning the changed value until the 2nd time. Because this too (the on_change) did not work I assume that an on_change is not needed to retain the changed values and that using a session state should work. I think you are saying that a on-change function call is not needed to retain the changes in data_editor, that setting the results from the data_editor to a session state is all that is needed. However this is still not working on the 1st change in the data_editor.

1 Like

The examples don’t have that problem, do they?

1 Like

I think I found the issue. I used a radio button to select different groups of columns. Then I used the column_order=list to show the group of columns. This did not work with the session state. I think the column_order=list in data_editor has a problem - it may reset the session state?

1 Like

Not anythong that I would call reset the session state, but I guess there are some ways in which it would cause trouble.

1 Like

here a simple example of the problem

import streamlit as st

@st.cache_data
def load_data():
initial_data = {‘item’: {‘Fd1’: ‘item 1’, ‘Fd2’: ‘item 1’, ‘Fd3’: ‘item 1’, ‘Fd4’: ‘item 1’, ‘Fd5’: ‘item 1’, ‘Fd6’: ‘item 1’, ‘Fd7’: ‘item 1’},
‘Name’: {‘Fd1’: ‘info 1’, ‘Fd2’: ‘info 1’, ‘Fd3’: ‘info 1’, ‘Fd4’: ‘info 1’, ‘Fd5’: ‘info 1’, ‘Fd6’: ‘info 1’, ‘Fd7’: ‘info 1’},
‘Data 1’: {‘Fd1’: 10, ‘Fd2’: 25, ‘Fd3’: 22, ‘Fd4’: 15, ‘Fd5’: 30, ‘Fd6’: 24, ‘Fd7’: 8},
‘Data 2’: {‘Fd1’: 2.5, ‘Fd2’: 3.0, ‘Fd3’: 4.0, ‘Fd4’: 4.2, ‘Fd5’: 3.0, ‘Fd6’: 1.8, ‘Fd7’: 0.8},
‘Data 3’: {‘Fd1’: .35, ‘Fd2’: 0.4, ‘Fd3’: 0.42, ‘Fd4’: 0.5, ‘Fd5’: 0.5, ‘Fd6’: 0.3, ‘Fd7’: 0.2}}
return initial_data

st.set_page_config(layout=“wide”,page_title=“Test Case”)

if ‘Fd_data’ not in st.session_state:
initial_data = load_data()
st.session_state[‘Fd_data’] = initial_data

tab1, tab2, tab3 = st.tabs([“Feed Data”, “Operating Data”, “Calibration Ylds”])

with tab1:
col1, col2, col3 = st.columns([4, 1, 4])
c1 = col1.container(border=True)
c2 = col2.container(border=True)
c3 = col3.container(border=True)

fd_sel = c1.radio('Select cases',['Select Item A', 'Select Item B', 'Select Item C'],
                  horizontal=True, label_visibility="collapsed")

if fd_sel == 'Select Item A':
    edit_list = ('Name','Data 1','Data 2')
if fd_sel == 'Select Item B':
    edit_list = ('Name','Data 2','Data 3')
if fd_sel == 'Select Item C':
    edit_list = ('Data 1','Data 2','Data 3')

# using the "column_order=edit_list" as show below does not work correctly
data_dic = c1.data_editor(st.session_state['Fd_data'],column_order=edit_list, hide_index=True )
# with out the column_order=edit_list this works well
data_dic = c1.data_editor(st.session_state['Fd_data'], hide_index=True )
1 Like

Other than making separate data_editor tables, is there a way to use the column_order= “some list” parameter in the data_editor to display different groups of columns in a single data_editor without the data going back to the initial or original data (i.e. a load data function). It seem like this might be a bug or I am missing something.

I cannot reproduce the problem with that code either. I don’t really understand the intent of the code, though, so I might be missing something.

I’m encountering the same issue: cell content updates are not always recorded when data is entered. Running the following simple code locally:

    row = st.session_state['row']
    new_row = st.data_editor(row,
                         hide_index=True,
                         disabled=["AI"],
                         use_container_width=True)
    print(new_row)
    st.session_state['row'] = new_row
    print(st.session_state['row'])

Exhibits the same behavior. After updating a cell and pressing Enter, the first update isn’t reflected, but the second update is. The print() statements used for debugging confirm that it is not related to st.session_state.

By the way, I’m also using AgGrid(df, ..), and it behaves the same way.
Adding observation:
it looks like when some type of object display refresh is running, the cell is not updated.