Data Editor in form not updating the edited dataframe when submit button is clicked

Hi,

Python and Streamlit newbie here so I suspect I am doing something wrong. I have Panda dataframe in a form like so:

def form_02():
    # Create empty DataFrame for data entry
    form_data_df = pd.DataFrame(
        {"Color": pd.Series(dtype="str"), "Quantity": pd.Series(dtype="int")},
        index=range(5),
    )
    my_form_02 = st.form("my_request_02", clear_on_submit=True, enter_to_submit=False)
    with my_form_02:
        edited_df = st.data_editor(
            form_data_df,
            column_config={
                "Color": st.column_config.SelectboxColumn(
                    "Color",
                    width="medium",
                    options=df_colors["Color"].values,
                    required=False,
                ),
                "Quantity": st.column_config.Column(
                    width="small",
                    required=False,
                ),
            },
            hide_index=True,
            use_container_width=True,
        )
        st.form_submit_button("Send Request")

When I first hit the submit button after entering values in the table, I get what I expect and form clears as expected. If I then hit the submit button with nothing entered, the edited_df still has the previous values. I would expect it to be empty in this case. What am I missing in my understanding of the behaviour here and how can I detect if someone is attempting to submit the form without entering anything? Thanks in advance!

Playing around with this a bit more and using session state, if I do this:

form_02()
# form writes edited_df to st.session_state.form_data when submit button is clicked

if 'form_data' in st.session_state:
    st.session_state.form_data
    del st.session_state.form_data

# if 'form_data' in st.session_state:
    st.session_state.form_data

I receive the error: AttributeError: st.session_state has no attribute “form_data”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs

Makes sense as I have just deleted it above. But then if uncomment the if statement I am returned st.session_state.form_data, even though it just told me the session state doesnt have this attribute. Calling st.rerun() doesn’t clear it but navigating to another page and back does, is there a difference between the former and latter?

I have managed to suss this by doing this:

if 'dek' not in st.session_state:
    st.session_state.dek = str(uuid.uuid4())

....

# add key=st.session_state.dek to st.editor statement
....

form_02()

if 'form_data' in st.session_state:
    if len(st.session_state.form_data):
        json_data = json.dumps(st.session_state.form_data.to_json(orient="records"))
        json_data
        st.session_state.dek = str(uuid.uuid4())

Now if I press the submit button and dataframe is empty in the form it all works as expected. This post helped:

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