Data_editor - Not returning edited data on button click

Hello, I’m new to Streamlit and I appreciate if you could suggest solutions for the issue I’m having.

Idea
The idea is to get edited values in “edited_col” column on submit (so that I can update database record).

Issue
The issue I’m having is that I have to click Submit button twice to get it to work. Also the table value unexpectedly reset value as well.

Potential Cause
I think it is caused by the data_editor not returning the edited result at the button onclick event.

Potential Solution?
Would you be able to tell me if there is a way to force data_editor to return the edited result? or may be better ways to make this work somehow please?

Thank you.

I’m testing script below on https://edit.share.stlite.net/
(Streamlit v1.24.0)

import streamlit as st
import pandas as pd

df = pd.DataFrame(
    [
       {"Type": 3, "ROW_ID": 6, "edited_col": "etc", "rating": 4, "is_widget": True},
       {"Type": 1, "ROW_ID": 7, "edited_col": "st.selectbox", "rating": 4, "is_widget": True},
       {"Type": 1, "ROW_ID": 8, "edited_col": "st.balloons", "rating": 5, "is_widget": False},
       {"Type": 2, "ROW_ID": 9, "edited_col": "A", "rating": 3, "is_widget": True},
       {"Type": 1, "ROW_ID": 10, "edited_col": "B", "rating": 3, "is_widget": True},
       {"Type": 2, "ROW_ID": 11, "edited_col": "C", "rating": 3, "is_widget": True},
       {"Type": 2, "ROW_ID": 12, "edited_col": "D", "rating": 3, "is_widget": True},
       {"Type": 2, "ROW_ID": 13, "edited_col": "F", "rating": 3, "is_widget": True}
   ]
)

if "df" not in st.session_state:
    st.session_state.df = df[df["Type"].isin([2])]

with st.sidebar:
    if st.button("obj 1"):
        st.session_state.df = df[df["Type"].isin([1])]

    if st.button("obj 2"):
        st.session_state.df = df[df["Type"].isin([2])]

edited_df = st.data_editor(st.session_state["df"])

if st.button("Submit"):
    df = st.session_state["df"]
    only_diff_df = df.compare(edited_df)
    changde_df_row = df[df.index.isin(only_diff_df.index)]
    for i in only_diff_df.index:
        changde_df_row = df[df.index.isin([i])]
        st.write("ROW_ID")
        st.write(changde_df_row["ROW_ID"].item())
        st.write("Chanded to ")
        st.write(only_diff_df["edited_col"]["other"][only_diff_df["edited_col"]["other"].index.isin([i])].item())
    st.session_state["df"] = edited_df
1 Like

st.experimental_data_editor was deprecated in version 1.23.0. Use st.data_editor instead.

Thanks for your comment. And Fair point. I’ve modified the topic using data_editor. Cheers.

I think i’m facing same issue. I have a data_editor with 7 column, which the last one is enabled to user inputs. And then i have a download button to export all the data_editor in xlsx format. But, when i download it, the data from the last column is always empty. I don’t know if i have to use some kind of session_state or not.

PS: there’s a micro download button icon on the top-right of the table, and when i hit it, it download, as csv, with the data from the last column successfully. I wish i had this answer in the forum, but i just can’t find the solution anywhere.

I don’t have a good solution yet for this, but I’ve added an “Review Changes” button to get the last user changes before making “Submit” button enable.

I’ve also implemented “Add” form rather than letting user add items via data_editor.