Can we able to stop Streamlit from executing the entire code?

Hello,
I have a Bayesian optimization workflow what I want to put in Streamlit app.
However, the function evaluation part will be interactive where looking at a image, the user will rate the image as (0, 1, or 2).
My target workflow in Streamlit should be my BO code will stop at that function eval point and wait for the user to provide the input and then run for next iteration with the new image and stops for user input again, so on…

As I am working in Streamlit, I see after passing the input, it tends to run the entire code (with default values of the widgets), and with changes in the input in widget, it starts running from fresh, which I dont want

I want to run the app which will stop at certain point of my integral BO code and wait for user interaction to continue running again. Is this workflow possible in Streamlit?

I have very minimal experience in Streamlit. Any suggestions?

Welcome, @arpanbiswas52!

Two things to check out:

Take a look at those and the examples, and I suspect you’ll find some tools to solve your use-case. If you get stuck on something specific, let me know!

1 Like

Great thank you.
I will check those and let you know if I have any questions

Thank you for your help.
Another question I have-
It seems the app execute the whole section of the code, however, is it possible to hold the execution after certain line of code and then with user (may be clicking botton or anything) to execute the rest of the code.
Please note, I see (after using session) that the app runs the full code and shows all the images where I can able to change value of the widget of all those images individually, but I would want the code not to show all the images but one image at a time (the other images to not execute before my rating to the earlier image)
Basically all the image data lies in a matrix, where I would want the app to pause after running the 1st entry of the matrix (1st image) and after user give rating of that image, the next data in the loop will run to show the next image.
Can this workflow be possible?

Here’s some example code that shows something similar to what you’re looking for, using a list of images

import streamlit as st

# Make list of random cat placeholder images
random_images = [
    "https://placekitten.com/200/400",
    "https://placekitten.com/300/400",
    "https://placekitten.com/400/400",
    "https://placekitten.com/500/400",
    "https://placekitten.com/600/400",
]

if "image_idx" not in st.session_state:
    st.session_state.image_idx = 0

if "image_ratings" not in st.session_state:
    st.session_state.image_ratings = {}

# Display the image
st.image(random_images[st.session_state.image_idx])

if st.session_state["image_idx"] in st.session_state.image_ratings:
    rating = st.session_state.image_ratings[st.session_state.image_idx]
else:
    rating = ""

with st.form("image_rating"):
    rating = st.text_input("Rate image", value=rating)

    if st.form_submit_button("Submit"):
        st.session_state.image_ratings[st.session_state.image_idx] = rating
        st.info("Submitted!")

if st.button("Next image", key="next"):
    idx = st.session_state.image_idx
    idx = (idx + 1) % len(random_images)
    st.session_state.image_idx = idx
    st.experimental_rerun()

elif st.button("Previous image", key="previous"):
    idx = st.session_state.image_idx
    idx = (idx - 1) % len(random_images)
    st.session_state.image_idx = idx
    st.experimental_rerun()

st.session_state

You can see it running here https://playground.streamlitapp.com/?q=image-rater

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