Session status behaves unexpectly with `button`

Problem summary

Background information

I was trying to deploy a machine learning model where people can upload the input file with st.file_uploader() and I can use a st.button() to confirm the calculations. I choose st.button()
because we will have multiple models to choose from and I want to perform the calculations only when all of them are set by the user.

Problem I have

I was using the session state to check if the uploaded file had changed or not because I will need it to check I will need to rerun some shared calculations. But I noticed that st.session_state.uploaded_file_changed changed after check if the submit_job_button is True or not.

I was hoping to be able to check if the input files are changed or not. Thank you very much for friends in the Streamlit community.

Steps to reproduce

Code snippet:

import streamlit as st

if "uploaded_file" not in st.session_state:
    st.session_state.uploaded_file = None
if "uploaded_file_changed" not in st.session_state:
    st.session_state.uploaded_file_changed = False

uploaded_file = st.file_uploader(
    label="Upload a txt file",
    type=["csv", "txt"],
    help="Input file.",
    accept_multiple_files=False,
    # key="uploaded_file",
    # on_change=update_uploader_session_info,
)
if uploaded_file:
    # when new file is uploaded is different from the previous one
    if st.session_state.uploaded_file != uploaded_file:
        st.session_state.uploaded_file_changed = True
    else:
        st.session_state.uploaded_file_changed = False
    st.session_state.uploaded_file = uploaded_file
    # print(f"status of uploaded file {st.session_state.uploaded_file}")

submit_job_button = st.button(label="Submit Job", type="secondary")

st.write(
    f"the state of uploaded file changed before checking: {st.session_state.uploaded_file_changed}"
)
# Generate predictions when the user uploads a file
if submit_job_button:
    # for k, v in st.session_state.items():
    #     st.session_state[k] = v

    st.write(
        f"the state of uploaded file changed after checking: {st.session_state.uploaded_file_changed}"
    )

    # this where my actual ML model will start working here

Expected behavior:

I want to check the status change and run the calculations.

  • Once upload a new file for the first time, st.session_state.uploaded_file_changed should become True.
  • Once replaced with a new file, the st.session_state.uploaded_file_changed should be True.
  • If no new file is uploaded, but only click the submit job button, it should skip the calculations.

Actual behavior:

I uploaded a random text file, before clicking the button, the state of uploaded file changed before checking: True. Then I click the submit job button, the page refreshed and I got
the state of uploaded file changed before checking: False.
the state of uploaded file changed after checking: False
.

Debug info

  • Streamlit version: 1.27.2
  • Python version: 3.8.18
  • Using Conda
  • OS version: Red Hat Enterprise Linux 9.2
  • Browser version: Google Chrome Version 117.0.5938.149 (Official Build) (64-bit)

config.toml file contents

[server]
# Max size, in megabytes, for files uploaded with the file_uploader.
# Default: 200
maxUploadSize = 2

Some resources that I have checked

This is normal behavior of streamlit – any widget interaction leads to the whole script being run from top to bottom.

If you actually want this

  • If no new file is uploaded, but only click the submit job button, it should skip the calculations.

Then you should put the logic that checks and updates st.session_state.uploaded_file inside the if submit_job_button block.

1 Like

Thanks, Blackary. I will test it following your suggestion and get it back to you soon.

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