Pre-populating editable values from a database

I’m experimenting with Streamlit and I’ve got as far as putting together a form with editable fields save out values to a database so they persist between sessions, and reading those back from the database via a button. My problem, though, is this…

I’d like to be able to read those values automatically, and make them available across the whole application when it first runs. That’s easy to do too, simply running the same method that’s linked to the load from database button at the beginning of the script. The problem comes with editing the values that are automatically loaded.

I understand that Streamlit works by re-running the script on user interactions. What happens, though, is that this happens before the code associated with the form submit button is run. This means that the old values are loaded into the form before anything is written back to the database. Which means that they never reflect the new imputted values.

Is there a way to prevent this re-run behaviour in specific circumstances, or change the execution sequence at all? To run a block of code before whole-script re-run? If not, is there a common workaround for this kind of sequencing issue?

Have you try running the specific part you are interested using st.form