Summary
I want to run 4 models in the same page by clicking 4 different buttons for each one.
When I click the first button, the first model is applied and I have an output.
When I click the second button, the output of the first model disappears.
I tried by saving in the memory the output and re-print it. I tried using session_state. I tried using Parallelize.
But nothing worked
Steps to reproduce
Code snippet:
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.sidebar.image(image, caption='Input Image', use_column_width= 'auto')
if st.button('Predict 1'):
st.write('## Model 2')
model2(image) #this is gonna show a plot
if st.button('Predict 2'):
st.write('## Model 2')
st.write(model3(image))#this is gonna show a text
if st.button('Predict 3'):
st.write('## Model 3 ')
st.write(model3(image)) #this is gonna show a text
if st.button('Predict 4'):
st.write('## Model 4')
st.write(model4(image)) #this is gonna show a text
Expected behavior:
I expected the buttons to “leave untouched” the different outputs
Actual behavior:
If I click any button, I clear every other button’s output
Hey @fr3nz99, can you share the example where you tried implementing this with session state? As is, I would expect the output to clear since the Streamlit reruns your script when you interact with a widget.
There might be a cleaner way to do this, but I would create 4 session_state
variables that track whether a model has been run:
if "model1_computed" not in st.session_state:
st.session_state.model1_computed = False
if "model2_computed" not in st.session_state:
st.session_state.model2_computed = False
if "model3_computed" not in st.session_state:
st.session_state.model3_computed = False
if "model4_computed" not in st.session_state:
st.session_state.model4_computed = False
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.sidebar.image(image, caption='Input Image', use_column_width= 'auto')
if st.button("Predict 1") or st.session_state.model1_computed:
st.write("## Model 1")
model2(image) #this is gonna show a plot
st.session_state.model1_computed = True
if st.button("Predict 2") or st.session_state.model2_computed:
st.write("## Model 2")
st.write(model3(image))#this is gonna show a text
st.session_state.model2_computed = True
if st.button("Predict 3") or st.session_state.model3_computed:
st.write("## Model 3 ")
st.write(model3(image)) #this is gonna show a text
st.session_state.model3_computed = True
if st.button("Predict 4") or st.session_state.model4_computed:
st.write("## Model 4")
st.write(model4(image)) #this is gonna show a text
st.session_state.model4_computed = True
However, it looks as though your code will re-run each model each time you want to run a new one, especially if you apply the above, wasting computational power. I would consider changing what your model1
- model4
functions return or do, so that you could save the outputs of your model to the session state, and you don’t need to re-compute the functions/models each time. Something like:
if "model1_computed" not in st.session_state:
st.session_state.model1_computed = False
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.sidebar.image(image, caption='Input Image', use_column_width= 'auto')
if st.button("Predict 1"):
#Compute the model, store in session state, set 'computed' indicator to True
st.session_state.model1_output = model1(image) #This saves the model outputs into the session_state
st.session_state.model1_computed = True
#etc etc for other models
# Check to see if models were run, and display output if it was:
if st.session_state.model1_computed:
st.write("## Model 1")
st.write(st.session_state.model1_output)
1 Like