I’m having the main page perform some predictions. But every time I go to a different page while it is not complete and return back, I get an error and have to refresh the entire page. Is there a way to just disable page navigation until the loading process is completed?
Here is a sample demo on how this can be done. There can be other ways. In this demo there is a selectbox in the sidebar that we want to disable if we run a prediction.
A selectbox has a disabled parameter, we can use it to enable/disable this box. Create a callback function in the prediction button to disable the selectbox in the sidebar. We also need a variable to handle selectbox disability.
import time
import streamlit as st
if 'selboxdisabled' not in st.session_state:
st.session_state.selboxdisabled = False
def cb():
"""Disables sidebar selectbox.
A callback function to be executed when prediction
button is pressed or triggered.
"""
st.session_state.selboxdisabled = True
def run_prediction(sec):
time.sleep(sec)
return 'done'
st.sidebar.selectbox('select name',
['', 'roel', 'alex', 'allan'],
disabled=st.session_state.selboxdisabled)
run = st.button('do prediction', on_click=cb)
if run:
status = run_prediction(5)
# Restore selboxdisabled state.
if status == 'done':
st.success('prediction is done')
st.session_state.selboxdisabled = False
# Call rerun so that streamlit will rerun the code
# from top to bottom thereby hitting the statement:
# st.sidebar.selectbox(...)
# but now with st.session_state.selboxdisabled set to False.
# As a result the sidebar selectbox will be enabled again.
st.experimental_rerun()
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.