Multitabs app with real time data: frontend does not refresh properly

Hola, just passing by


I think it meant managing your 5 second interval readings outside the Streamlit thread. Like in Best (fastest) practice to display live 2D data - #4 by andfanilo you would download the readings every 5 seconds in a separate asyncio thread (which shouldn’t block Streamlit), put them in a queue and have the Streamlit thread pull the new readings from there. That way you can still change page in between those 5 seconds.

It does not solve your ghosting problem though (I mean it might work though, since the asyncio sleep shouldn’t block the Streamlit thread, but I haven’t tried this far). I personally try to put placeholders as deeply in the tree as possible and only on single elements so the key/label of the widget manages its visibility, not empty full containers/columns (emptying entire containers of widgets, as you can see doesn’t really work out and ghosts of children of the container still persist :frowning:) and reuse widgets as much as possible, like

import streamlit as st
from streamlit_option_menu import option_menu
import random
import time

st.header("Multipage with data refresh")

page = option_menu("", ["page1", "page2"], orientation="horizontal")
placeholder = st.container()
with placeholder:
    m_1 = st.empty()
    m_2 = st.empty()
    m_3 = st.empty()

if page == "page1":
    while True:
        m_1.metric(label="Page 1 Metric 1", value=random.randrange(1000, 1109))
        time.sleep(1)
elif page == "page2":
    while True:
        m_1.metric(label="Page 2 Metric 1", value=random.randrange(2000, 2109))
        m_2.metric(label="Page 2 Metric 2", value=random.randrange(2000, 2109))
        m_3.metric(label="Page 2 Metric 3", value=random.randrange(2000, 2109))
        time.sleep(1)

but I totally understand this can’t easily work for you if you’re trying to build containers with different structures and have to prebuild like 50 metrics/charts :confused: plus I’m not totally convinced quick changes from empty to chart to metric would work.

Haven’t read the full issues on Github to give you a better solution. If I get you a better solution (aside from H2O Wave) I’ll write it down here.

Also, st.experimental_rerun after a time.sleep forces a rerun and sometimes gets rid of ghosts. Sometimes (that’s a trick I used a while back)

Have a nice day,
Fanilo

2 Likes