Streamlit st.dataeditor is throwing error when we try to add new rows

Aim was to create an UI that talks to Postgres DB. I wanted to do Create or Update rows using the dataframe the data editor creates. The code was working fine earlier. now it throws error ::warning:" Error during cell creation.

This should never happen. Please report this bug.
Error: Error: Row index is out of range: 8"
Update to the existing rows are working fine. only creation of new records is causing trouble.
I wanted to add rows to the dataframe by setting num_rows = “dynamic” using the dataeditor widget. But currently it’s just throwing error whenever I try to add new record. Please help me fix this. I use the session.state to update the dataeditor to have the latest data.

The main function :
def main() → None:

read_dotenv()
st.set_page_config(layout="wide")
st.title("Create or Update Records")

# Check if the session state has already been initialized
if "edited_df" not in st.session_state:
    rows = run_query("SELECT * from table_dev.excel_test")
    data = to_df(rows)
    data.columns = [
        "id",
        "subscription_id",
        "subscription_name",
        "resource_id",
        "status",
    ]
    st.session_state.edited_df = data

edited_df_placeholder = st.empty()

# Display filtered data in data editor
edited_df = edited_df_placeholder.data_editor(
    st.session_state.edited_df,
    num_rows="dynamic",
    use_container_width=True,
    hide_index=False,
)

save_button_clicked = False
if st.button("Save Changes"):
    save_button_clicked = True
    updated_df = edited_df.copy()
    if updated_df.equals(st.session_state.edited_df):
        st.warning("No changes were made on UI.")
    else:
        for index, row in updated_df.iterrows():
            if row["status"] not in {
                "Not Started",
                "In Progress",
                "Pending",
                "Rejected",
                "Done",
            }:
                st.error(
                    f"Invalid status value '{row['status']}' entered for the id:{row['id']}. Please enter one of these: 'Not Started', 'In Progress', 'Pending', 'Rejected', 'Done'."
                    f" Save to the database failed."
                )
                return
        with st.spinner("Saving changes to Database"):
            update_resources_in_database(
                updated_df, st.session_state.edited_df
            )
            st.session_state.edited_df = updated_df
            st.success("Database updated successfully.")

if st.button("Reload Latest Data"):
    with st.spinner("Reloading Latest Data..."):
        changes_detected, updated_data = check_for_changes(
            st.session_state.edited_df
        )
        if changes_detected:
            st.session_state.edited_df = updated_data
            # Update filtered_df with the latest data

            filtered_df = st.session_state.edited_df
        # Update data editor with the latest filtered data
        edited_df_placeholder.data_editor(
            st.session_state.edited_df,
            num_rows="dynamic",
            use_container_width=True,
            hide_index=False,
        )
        st.success("Data reloaded successfully.")

Dependencies I have downloaded :
streamlit = “^1.32.2”
mitosheet= {version= “^0.1.566”}
pandas = “^2.1.0”

1 Like

Hi @DIVYA_DHARSHINI,

Thanks for sharing your question with the community! :balloon: Please update your debugging post to include a code snippet and a link to your app’s public GitHub repo – this will allow the community to help you find an answer as quickly as possible. In the meantime, this post will be tagged as needs-more-info.

1 Like

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