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