Disable buttons, checkboxes and other widgets during code execution


I have quite an extensive application build where users can use a large machine learning model. Inference can take time, during which the app is prone to crashing if the user interacts with any widgets.

The issue seems to be that during the long prediction (around 1 minute) if the user clicks anything on the application, the app code is executed from the beginning of the script which causes the ongoing process to crash the app.

Here’s how it should work in my opinion:

  1. The user makes some settings using radio buttons, checkboxes sliders etc.
  2. The user clicks a button “Predict”.
  3. All widgets disable.
  4. A process is started (~1 minute). During this time the user can not change any widgets since that will terminate the process.
  5. Process finished, widgets get reenabled
  6. The user can continue to examine the prediction results by interacting with new widgets that were previously not visible.

I have looked at these solutions:

However, this does not really solve the issue. I get that streamlit is implemented in a way that makes it hard to disable widgets since they are not class based. But it is unintuitive, and does not work to use the work around of st.session_state since this requires an st.experimental_rerun anyway to take effect. And the solution to have a placeholder into which you set new buttons each time feels like a shady work around.

I feel that other application builders, like PyQt solve this very easily, where each widget is the instance of class that can be e.g. disabled or enabled at any moment. It can also be used to get or set the value of a widget etc.

I know there’s no quick fix to this but I really think that streamlit would benefit a lot from changing widgets to class based instead of variable based. This way users get more flexibility with controlling their interface and removes the need for working with st.session_state. If anyone has other solutions than the one cited above, please let me know! Btw, streamlit is great, it’s neat, looks nice and extremely easy to use, as long as you want to do the shorter application builds.


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