Continuously updating image content

(how) is it possible to update the same image on every loop of a while structure?

the used base code is that one: Examples — Python MSS latest documentation

found only this thread so far: Continuously updating dashboard

thanks in advance

One option would be to use st.fragment’s run_every parameter along with session_state to rerun the code for generating/updating the image, like this:

import streamlit as st
from PIL import Image

if "image_step" not in st.session_state:
    st.session_state["image_step"] = 0


@st.fragment(run_every=1)
def draw_image():
    image_step = st.session_state["image_step"] * 10
    image = Image.new(
        "RGB",
        (100, 100),
        (
            image_step,
            image_step,
            image_step,
        ),
    )

    st.code(f"{image_step=}")

    st.image(image)

    st.session_state["image_step"] += 1

    if image_step > 255:
        st.session_state["image_step"] = 0


draw_image()

2024-12-03_09-44-03 (1)