"StreamlitAPIException: Values for st.data_editor cannot be set using st.session_state" using data_editor to delete rows

Summary

I want to use data_editor to delete rows of a dataframe as seen here (Deleting rows in st.data_editor progmatically - #3 by blackary). Moreover, I have a button to add a new row to the dataframe.

Steps to reproduce

Code snippet:

import streamlit as st
import pandas as pd

    
def callback_steps():
    edited_rows = st.session_state["steps_data_editor"]["edited_rows"]
    rows_to_delete = []

    for idx, value in edited_rows.items():
        if value["x"] is True:
            rows_to_delete.append(idx)

    st.session_state["DF_DATA"] =  st.session_state["DF_DATA"].drop(rows_to_delete, axis=0).reset_index(drop=True)
    
def aux_step(root):
    df_columns=["Priority", "Keep step", "Type", "Description"]
    if "DF_DATA" not in st.session_state:
        st.session_state["DF_DATA"] = pd.DataFrame(columns=df_columns)

    step = st.radio(
        label= "**Choose a step**",
        options= ("Step1", "Step2", "Step3"), 
    )

    with st.expander("Details"):
        match step:
            case "Step1":
                description = st.text_input("Description", "Step1_description")
            case "Step2":
                description = st.text_input("Description", "Step2_description")
            case "Step3":
                description = st.text_input("Description", "Step3_description")
            case _:
                description = "unknown"

    if st.button("**Add Step**"):
        st.session_state["DF_DATA"].loc[len(st.session_state["DF_DATA"].index)] = [1, True, step, description]
 
    columns = st.session_state["DF_DATA"].columns
    column_config = {column: st.column_config.Column(disabled=True) for column in columns}

    modified_df = st.session_state["DF_DATA"].copy()
    modified_df["x"] = False
    # Make Delete be the first column
    modified_df = modified_df[["x"] + modified_df.columns[:-1].tolist()]

    st.data_editor(
        modified_df, 
        key="steps_data_editor", 
        on_change=callback_steps, 
        hide_index=True, 
        column_config=column_config)

Error:

At execution, I’ve the following error pointing out on st.data_editor line

streamlit.errors.StreamlitAPIException:
Values for st.button, st.download_button, st.file_uploader, st.data_editor,
st.chat_input, and st.form cannot be set using st.session_state.

I can’t understand why the exception is raised in my code. Could you explain me ?

Debug info

  • Streamlit version: 1.24.0
  • Python version: 3.11.3
  • Using Conda

I think that’s just the way streamlit is right now, I have the same exception with chat_input. There is no support to set the values of the aforementioned widgets via st.session_state, at least not yet. Good luck with your project!

1 Like

That is very strange – that exact code (plus aux_step(None) at the bottom of the page) works just great for me. Is it possible that some other code in your app is actually causing the issue?

I think I might have determined the cause of this exception. I had code that was initializing the state for all my keys, and then I used those keys in the code that followed. It followed this pattern:

initialize_state()

st.file_uploader(
   "Choose an input spreadsheet (.xlsx)",
   type="xlsx",
   key=<Key I initialized in the above function>,
)

The Streamlit code that throws the error is lib.streamlit.elements.utils.check_session_state_rules in the event the passed in write_allowed parameter is False. You can easily find the code by searching for the variable SESSION_STATE_WRITES_NOT_ALLOWED_ERROR_TEXT. For widgets like forms, file uploaders, etc, the check_session_state_rules is called with the write_allowed parameter set to False.

1 Like

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