Hide button during inference time


I am developing an application, which infer an AI model when RUN button is pressed. How can i disable/hide this RUN button while model is running.

Here’s a toy example:

import streamlit as st
import time

def run():
    st.session_state.run = True

def clear():
    st.session_state.result = None

def expensive_process():
    with st.spinner('Inference running...'):
    st.session_state.run = False
    st.session_state.result = 'Done!'

if 'run' not in st.session_state:
    st.session_state.run = False
    st.session_state.result = None

st.button('Run inference', on_click=run, disabled=st.session_state.run)

if st.session_state.run:
    # Include rerun if you want the Run Inference button to be clickable as soon
    # as the inference is done.
    # st.experimental_rerun()

if st.session_state.result is not None:
    st.button('Clear', on_click=clear)
1 Like

I created a feature request for a method to simplify this common scenario. Feel free to vote on it here or check out the linked, related feature requests.

My demo show a generated image from model. If run st.experimental_rerun() to get button clickable as you said, this image will disappear immediately. How to solve that?

Save the image into session state and then retrieve it from session state after the rerun.

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