Streamlit reset page after adjustingg input change

Hello, everyone!

I am having trouble in allowing a user to simulate results based on new input that they can adjust, which is then passed through a ML model. It happens that after grabbing a row from the data frame, “populating” it in a slider and whatnot, the page resets as soon as I change the input value into a new value, . Here is a snippet of my code, could you please guide me onto how to solve this:

st.subheader("Simulate")
    file_name = "train_input_test.csv"
    input_id = st.text_input("Enter ID", "")

    if st.button("Search by Id"):
        IDS = [input_id]
        with open(file_name, newline='') as csvfile:
            reader = csv.DictReader(csvfile)
            rows = [row for row in reader if row['encounter_ID'] in IDS]
        if rows:
            #st.write(rows)
            new_time_in_hospital = st.slider("Time in hospital",1,14,int(rows[0]['time_in_hospital']),None,None)
            new_num_medications = st.slider("Number of Medication",1,81,int(rows[0]['num_medications']))
            new_num_procedures = st.text_input("time_in_hospital", rows[0]['num_procedures'])

            if st.button("Generate New CSV"):
                new_file_name = "new" + file_name
                out_put_file = writer(open(new_file_name, "w"), newline='')

                with open(file_name, "r") as f:
                    read = csv.reader(f)
                    for new_row in read:
                        st.write(new_row)
                        if new_row['encounter_ID'] in IDS:
                            new_row['time_in_hospital'] = new_time_in_hospital

                        out_put_file.writerow(new_row)

Hey @acidic_ph, welcome to the forums

I’ve answered a similar question here using another version of the State hack that the one you found on the earlier posts. There are multiple versions that work for different Streamlit versions given how fast it evolves, so it’s sometimes hard to track the one for you :wink:

3 Likes