How to colapse text once it has been displayed

I have following problem:

I have a function that displays text with st.write every 2 seconds. Now I want to only show the 5 most recent messages. My current code will fill the page to the bottom until the program is finished.

Let’s suppose my program counts integers:

Current situation: …7…8…9…10…11…12…13 …14…Finished

Desired output: (only shows the 5 most recent integers and erases old ones) …
7…8…9…10…11
(erase output)
8…9…10…11…12
(erase output)
9…10…11…12…13
(erase output)
10…11…12…13…14
Finished

How can I solve this programmatically? How can I erase the output?

Hey @MaxMnemo ,
Is this what you intend to do ?

streamlit-test2-2022-02-22-14-02-45

Code Snippet
import streamlit as st
import time

_emp = st.empty()
numbers = [1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15]

if st.button("Run"):
    for x in numbers:
        pos = numbers.index(x)
        d = numbers[pos:pos+5]
        if pos < 10:
            time.sleep(0.5)
            _emp.code(d)

Hope this helps.

Best
Avra

1 Like

Yes exactly that, thank you very much

I forgot that streamlit has the empty keyword.

1 Like