Is there a way to get form data without pressing submit button?

Hello!

I have a page that contains many forms for data annotation purposes. In fact, for each data point, there are two forms: one imports a few attributes from another data point, and the other saves the data point to a sqlite3 database.

Here’s the problem:
If the annotator completes a few forms in a row, they can’t save all of them at once. They have to click save for each form.

Since I have the components’ keys in the session_state, I thought I could just get the data for each component and do the saving for all of them. Unfortunately, it seems Streamlit only passes the data to the session_state once the submit button is clicked. Is that a correct assumption?

Would there be a way to implement this functionality?

Thanks for the help and the awesome project/work.

Hi @ogabrielluiz , maybe you can approach the solution without using forms: Just use the standard input widgets to gather data into your session variables and have 1 common submit button to send your data to the database. After database commit, you can reinitialise your widget variables.

Cheers

1 Like

That’s a good suggestion. Since each widget has its own state held under its key, there’s no need to create separate session state keys. Can you use a global callback which persists the current global state across all widgets? Does the annotator user need to restore saved states from one session to the next (annotation takes time and it would be frustrating to lose a session)?

The problem is that there are some selectboxes. When the interaction happens the data is lost.

Also, an import functionality gets data from another object and copies it to the current object. When this happens we also get a page reload which erases the data.

Another thing is the page reload: on every interaction, there will be a reload. So if the person has to choose 10 objects on a multiselect, there will be 10+ reloads by the end of the process.

The problem with having this is that on each interaction there’s a reload. So, basically the person won’t be able to finish a form without the page reloading many times.

You can use the state value of one control to initialize the value of another (provided the other is rendered after the first). The toggle state of a checkbox is preserved on a rerun (not so for buttons). Sometimes I find it’s more “reliable” to set the dependent values that need to be used in components further down in your code in the on_change / on_click callback of the earlier component in your code which supplies the value. (That is rather than direct assignment to a variable.)