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