Data Editor not resetting as I would expect after Form Submission

I have tried running this both locally and deployed and I am having problems after clicking the submit button that the changes are persisting in the data editor. What i would like is for it to revert to the “empty” dataframe.

df = pd.DataFrame(
    [
        {
            "left_surface": None,
            "reached_bottom": None,
            "left_bottom": None,
            "reached_surface": None,
            "depth": 0,
            "supervisor": "",
            "Approved": False,
        }
    ]
)
  
# Column formatters
column_configs: dict[str, Any] = {}
# TODO: Get rid of magic number
for col in df.columns[:4]:
    proper_col: str = " ".join(col.split("_")).title()
    column_configs[col] = st.column_config.DatetimeColumn(
        label=proper_col,
    )
column_configs["depth"] = st.column_config.NumberColumn(
    label="Depth (ft)",
    min_value=0,
    default=0,
)
column_configs["supervisor"] = st.column_config.SelectboxColumn(
    label="Supervisor", options=st.session_state["supervisors"]
)
# Log editor
with st.form("editor"):
    edited_df = st.data_editor(
        df.copy(),
        column_config=column_configs,
        num_rows="dynamic",
        key="test",
        disabled=["approved"],
    )
    submitted = st.form_submit_button("Submit")

if submitted:
    submit_logs(edited_df)
    st.rerun()

[Gumby] It sounds like you’re encountering a common issue with Streamlit’s data persistence.

Define a *get_empty_df()* function to create our initial “empty” DataFrame.

use Streamlit’s *session_state* to manage the DataFrame’s state across reruns.

After submission, we reset *st.session_state.df* to a new empty DataFrame.

We call st.rerun() to refresh the app with the reset DataFrame.

This approach ensures that after each submission, the data editor reverts to its initial state. The st.rerun() call is crucial as it triggers a complete refresh of the app, ensuring the UI reflects the reset state

.

This still doesn’t seem to be working as expected.

If i st.write() the one in session state after submission, it is one row as expected, but the one displayed in the form is still the edited one.

st.session_state.df = create_empty_df()
    
# Column formatters
column_configs: dict[str, Any] = {}
# TODO: Get rid of magic number
for col in st.session_state.df.columns[:4]:
    proper_col: str = " ".join(col.split("_")).title()
    column_configs[col] = st.column_config.DatetimeColumn(
        label=proper_col,
    )
column_configs["depth"] = st.column_config.NumberColumn(
    label="Depth (ft)",
    min_value=0,
    default=0,
)
column_configs["supervisor"] = st.column_config.SelectboxColumn(
    label="Supervisor", options=st.session_state["supervisors"]
)
# Log editor
with st.form("editor"):
    edited_df = st.data_editor(
        st.session_state.df,
        column_config=column_configs,
        num_rows="dynamic",
        key="test",
        disabled=["approved"],
    )
    submitted = st.form_submit_button("Submit")

if submitted:
    submit_logs(edited_df)
    st.session_state.df = create_empty_df()
    st.rerun()