Handling Unsaved Changes in Streamlit Form Before Triggering Job

I am building a Streamlit app where I have:

A form with 6-8 fields and a "Submit" button to update a database.
An additional button to trigger a job in a Kubernetes cluster using the form's field values.

Problem:
When users change values in the form but don’t press the “Submit” button and then try to trigger the job, they should be notified about unsaved changes. However, I’m struggling to detect these changes consistently.

What I’ve Tried:

  • Capturing user inputs outside the form
    Didn’t work well with the Streamlit form structure.
  • Using st.session_state to track changes
    * This partially works, but the issue is:
    * When the user changes a value in the form for the first time, the change isn’t captured immediately.
    * On the first click of the job-trigger button, it doesn’t detect changes.
    * On subsequent clicks, it correctly detects changes.

What I Need:
A reliable way to track unsaved changes in the form and alert the user before triggering the job.

Any advice or examples on how to handle this scenario effectively in Streamlit would be greatly appreciated!

I don’t think you can do that with forms. The applications cannot see the changes in the form until the submit button is pressed. That is pretty much the definition of a form and it is at odds with your intent.

Thanks for your time.

Do we have any alternatives or ideas to achieve this could be appreciable :frowning:

Just don’t use a form. I don’t think I understand enough of your use case or the challenges you are facing to be much more specific. I suppose you can still have a button that saves the data and another button that triggers the job, and you can detect unsaved changes by comparing the widget values to the saved data.

I think thats the only way to work on my usecase. Right now I solved the usecase by using CSS in my code.

I don’t know if it helps in your instance, but forms can also have more than one submit button. So you could have one button to only save the values, and another to both save the values and trigger a job. Thus instead of warning about unsaved changes, your job trigger button could just be another form submit button.

1 Like