Page rerunning even after using session state and st.form

Hi, this is my code and the image is what the page looks like, but when submitting the form in the third expander section the whole page reruns.

# Select and Download revisions to the data
                def apply_changes(data, convert_dtypes, apply_deduplication, apply_numeric_extraction, apply_outlier_replacement, labs_outlier_thresholds):
                    if convert_dtypes:
                        data = validate_and_convert_dtypes(TABLE, data)[0]
                    
                    if apply_deduplication:
                        data = data.drop_duplicates()

                    if apply_numeric_extraction:
                        col = data['lab_value'].astype(str)
                        data['lab_value_numeric'] = pd.to_numeric(col.str.extract('(\d+\.?\d*)', expand=False), errors='coerce')    

                    if apply_outlier_replacement:
                        data, _, _, _ = replace_outliers_with_na_long(data, labs_outlier_thresholds, 'lab_category', ‘lab_value_numeric’)

                    return data


                def convert_df_to_file_format(data, file_format):
                    if file_format == ‘csv’:
                        return data.to_csv(index=False).encode('utf-8’)
                    elif file_format == ‘parquet’:
                        return data.to_parquet(index=False)
                    else:
                        return None
                
                st.write("# Select and Download Recommended Revisions to the Data”)
                with st.expander("Expand to View”):
                    st.write("#### Select changes to apply”)

                    if 'form_submitted' not in st.session_state:
                        st.session_state['form_submitted'] = False

                    with st.form(key='apply_labs_changes_form’):
                        # Create checkboxes for changes
                        if any(data.duplicated()):
                            apply_deduplication = st.checkbox("Remove duplicates”)
                        else:
                            apply_deduplication = False

                        if create_lab_value_numeric:
                            apply_numeric_extraction = st.checkbox("Create 'lab_value_numeric’”)
                        else:
                            apply_numeric_extraction = False

                        if replaced_count > 0:
                            apply_outlier_replacement = st.checkbox("Replace outliers”)
                        else:
                            apply_outlier_replacement = False

                        if mismatch_columns:
                            convert_dtypes = st.checkbox("Convert to expected data types”)
                        else:
                            convert_dtypes = False
                
                        st.write("#### Select file format for download”)
                        file_type = st.selectbox("Select file type for download", ["csv", "parquet”])
                        submit_button = st.form_submit_button(label=‘Submit’)

                    if submit_button:
                        st.session_state['form_submitted'] = True

                    if st.session_state['form_submitted’]:
                        with st.spinner("Applying changes
”):
                            df = data.copy()
                            revised_data = apply_changes(df, convert_dtypes, apply_deduplication, apply_numeric_extraction, apply_outlier_replacement, labs_outlier_thresholds)
                            revised_data_file = convert_df_to_file_format(revised_data, file_type)
                            st.write("Successfully applied changes. Click to download revised data.”)
                        if revised_data_file:
                            st.download_button(
                                label="Download revised data”,
                                data=revised_data_file,
                                file_name=f"revised_{TABLE}_data.{file_type}”,
                            )
                        if st.button("Reset”):
                            st.session_state['form_submitted'] = False

That is how it is supposed to work.

1 Like

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