Refreshing the screen without stacking images(tables) in a while loop application

Hi everyone,

I’ve stated using Streamlit recently and I’m still trying to find my way around to produce the result I need from my application.
My program has the following structure:

  • in the main function I am retrieving data via api, then do a lot of processing with pandas and I get four final tables which I am displaying on the Streamlit app.
  • this process is in an infinite loop, so everytime I retrive new data from api, I need to re-do the calculations and call the Streamlit display function to print me the updated tables.
    My main problem is that the tables are stacking-up, instead of clearing the window and print tables on a fresh empty screen for each cycle of the loop.

This is what I have inside the StreamlitDisplay function:

st.empty()
col1, col2 = st.columns((3, 1))
fig1 = go.Figure(data=go.Table(header=dict(values=['<b>Symbol</b>', '<b>Price</b>'], fill_color='paleturquoise'), cells=dict(values=[table1['Symbol'], table1['Price']))
with col1:
    st.header('My Table1')
    st.write(fig1)

I simplified the version of my function with just one table (and only two columns), so you have an idea about the structure of it. After I do this table printing job, the function returns to the main where the loop restarts with new api calls.

My expectation was that calling st.empty() when I call this display function will clear the screen allowing printing as if it was the first iteration of the loop. In reality I get images adding up to the bottom without end.
Using plotly.graph_objects I managed to do really nice customization to the layout of my tables, with colors and so on, so this is basically the last thing remaining to have the job done.

Is there any way to fix this or how can I approach this type of application involving an infinite loop where thing are re-calculated on each iteration and new results have to be updated on the screen?

Thanks you!

Hi @mg1795, welcome to the Streamlit community!

You need to define st.empty() outside of your function call, so that you can continue to write over that object.

Best,
Randy

Thank you, it worked! Just to make it clear if anyone else reads this topic in the future, the structure I used is the following:
with st.empty():
while loop

displayOnScreen()

George

1 Like