Combining st.button with st.experimental_data_editor

Combining st.button output to st.experimental_data_editor

  • User should be able to add or delete rows using experimental_data_editor;
  • When user click button (via st.button), we should add row to st.experimental_data_editor;
  • The “recent” added row from click button can be removed by user desires;
  • If user click button twice, two rows should be added to st.experimental_data_editor.

What is not working?

  • When I click “Add row to data editor”, the row is added correctly. However, if I click again, my previously added row will go away;
  • If I delete a row using st.experimental_data_editor, any “added rows” via button get removed.

Steps to reproduce

Code snippet:

import streamlit as st
import pandas as pd
from datetime import datetime
now = datetime.now()


df = pd.DataFrame(
    {
        "col1": [1, 2, 3, 4, 5],
        "col2": [10, 20, 30, 40, 50],
        "col3": [100, 200, 300, 400, 500],
    }
)
if "df_value" not in st.session_state:
    st.session_state.df_value = df

# def update(edited_df):
#    for row_1, row_2, row_3 in zip(
#        edited_df["col1"], edited_df["col2"], edited_df["col3"]
#    ):
#        st.write(
#            "INSERT INTO mytable VALUES %s, %s, %s",
#            (
#                row_1,
#                row_2,
#                row_3,
#            ),
#        )
#    # some more code

edited_df = st.experimental_data_editor(
    df,
    key="editor", # An optional string or integer to use as the unique key for the widget. 
    num_rows="dynamic",
)

if edited_df is not None and not edited_df.equals(st.session_state["df_value"]):
    # This will only run if
    # 1. Some widget has been changed (including the dataframe editor), triggering a
    # script rerun, and
    # 2. The new dataframe value is different from the old value
    #update(edited_df)
    st.session_state["df_value"] = edited_df


if st.button('Add row to data editor'): #only add when user clicks Add;
    bla = pd.DataFrame({
                                    "col1": [now.strftime("%H:%M:%S")],
                                    "col2": [112321],
                                    "col3": [1001231],
                                })
    edited_df = pd.concat([st.session_state["df_value"],bla])

if edited_df is not None and not edited_df.equals(st.session_state["df_value"]):
    # This will only run if
    # 1. Some widget has been changed (including the dataframe editor), triggering a
    # script rerun, and
    # 2. The new dataframe value is different from the old value
    #update(edited_df)
    st.session_state["df_value"] = edited_df

edited_df = st.experimental_data_editor(
    edited_df,
    key="editor2", # An optional string or integer to use as the unique key for the widget. 
    num_rows="dynamic",
)

Debug info

  • Streamlit version: 1.22.0
  • Python version: 3.11.1

Links

  • Part of this idea was taken from here.

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