Need help: st.data_editor doesn't save edits, it just refreshes the page and looses saved inputs on submit

I am encountering an issue with my app which I have simplified into a nice example, here:

import streamlit as st
import pandas as pd
def function():
    # Parsing the string into a list using json.loads
    person_title_strings3 = ['accountant','chef','friend']

    # Converting the list into a DataFrame
    df_job_titles = pd.DataFrame(person_title_strings3, columns=['Job Title'])

    # To display the DataFrame (optional)

    with st.form("my_form"):
        edited_df =  st.data_editor(df_job_titles, num_rows="dynamic")
        submitted = st.form_submit_button("Submit")

        if submitted:
            st.write("Edited dataframe:", edited_df)

            
if st.button('Press to see df'):
    function()

When I run this code:

  1. first the user presses this button to see a df
    image

then, they can edit the dataframe using st.data_editor:

and upon submit, I want the dataframe to be saved and displayed (so it can be used later in the function I do some computation on their updated dataframe).
But there is an issue! as you can see if you run the example provided, when you press submit, everything disappears! the user’s edits go away and we have refreshed the page :(. The dataframe edits are not saved for me to continue with my functions.

Do you have any advice? Is this possible?

The reason I NEED to display the dataframe only after the first button click (“press to see df”, in this example) is because the dataframe is first created with user inputting a sentence in the sidebar like “I am a student looking to talk to experts to get their advice”, which when you press “press to see df” calls the openAI API, which makes the first iteration of the dataframe with relevant job titles for the user to talk to, which is displayed. I then want the user to be able to edit this dataframe & then my code will do more functions after the user input, like call the Apollo API to get email addresses for those job titles.
Is this possible? I could use some help on the dataframe not dissappearing after the user edits it & tries to save it

Thank you!

I am running locally with streamlit version 1.28.2 and python version 3.10.10

1 Like

The button returns True only once. If you want to keep track of whether it has been pressed or not, you need to store that info in session state.

if st.button('Press to see df'):
    st.session_state["button_pressed"] = True

if st.session_state.get("button_pressed", False):
    function()

This was insanely helpful, thank you so much! I was able to make my app work perfectly and I’m so thrilled. I also noticed you made a little typo for anyone reading this thread later, the second False should be true.

But other than that this was so helpful and I am able to accomplish everything. Thank you again!

if st.button('Press to see df'):
    st.session_state["button_pressed"] = True

if st.session_state.get("button_pressed", True):
    function()

If you put True there, the function will be called before the button is clicked.

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