Callbacks with asyncio

Hi guys, I’m trying to use streamlit with asyncio to create a real time dashboard. I want to make a sidebar which contains a select box, when the user select an option I want the callback function immediately called, the problem is that I also have an async function keep_updating_placeholders, which keeps updating the placeholders with new values and then wait for 5 seconds. And now with following code, it seems the callback function is only called after the wait for 5 seconds is over, instead of immediately. Any help is appreciated! Thanks!

import streamlit as st
import time
import asyncio

st.set_page_config(page_title="stream", layout="wide")

placeholders = [st.empty() for i in range(5)]
sidebar = st.sidebar


def show_reselected_option():
    sidebar.write('Re-selected: ', st.session_state.select_option)


with sidebar:
    selected_option = st.selectbox('Select an option', ['A', 'B', 'C'], key='select_option', on_change=show_reselected_option)
    st.write('Selected: ', selected_option)


async def keep_updating_placeholders():
    while True:
        try:
            for i, placeholder in enumerate(placeholders):
                with placeholder.container():
                    st.subheader(f'placeholder {i}')
                    st.text(f'text {i}, timestamp: {time.time()}')
            await asyncio.sleep(5)
        except Exception as e:
            print(f'Draw Exception: {e}')


asyncio.run(keep_updating_placeholders())

1 Like

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