I want to print some text, then clear the screen, and then print some more text. Is there a command that I can use to accomplish this?
You can use st.empty to keep a placeholder that can be cleaned whenever you want to add new elements.
Here is a unnecessarily convoluted fun example:
import streamlit as st
if "page" not in st.session_state:
st.session_state.page = 0
def nextpage(): st.session_state.page += 1
def restart(): st.session_state.page = 0
placeholder = st.empty()
st.button("Next",on_click=nextpage,disabled=(st.session_state.page > 3))
if st.session_state.page == 0:
# Replace the placeholder with some text:
placeholder.text(f"Hello, this is page {st.session_state.page}")
elif st.session_state.page == 1:
# Replace the text with a chart:
placeholder.line_chart({"data": [1, 5, 2, 6]})
elif st.session_state.page == 2:
# Replace the chart with several elements:
with placeholder.container():
st.write("This is one element")
st.write("This is another")
st.metric("Page:", value=st.session_state.page)
elif st.session_state.page == 3:
placeholder.markdown(r"$f(x) = \exp{\left(x^🐈\right)}$")
else:
with placeholder:
st.write("This is the end")
st.button("Restart",on_click=restart)
This doesn’t seem to work when not triggered by a UI event. How does one clear the screen in response to some other kind of event - e.g. expiration of a timer, or a notification completion of a task coming from a remote service?
You could use with st.empty():.
Example:
import streamlit as st
import time
def next_page():
placeholder.empty()
if(st.session_state['button1_pressed'] < 4):
st.session_state['button1_pressed'] += 1
def previous_page():
placeholder.empty()
if(st.session_state['button1_pressed'] > 1):
st.session_state['button1_pressed'] -= 1
if 'button1_pressed' not in st.session_state:
st.session_state['button1_pressed'] = 1
placeholder = st.empty()
with st.empty():
if(st.session_state['button1_pressed'] == 1):
with st.container():
st.write("This is page one")
cols1, cols2, cols3, cols4, cols5 = st.columns([1,1,1,1,1])
with cols1:
st.button('<',key='<1',on_click=previous_page)
with cols5:
st.button('>',key='1>',on_click=next_page)
time.sleep(5)
next_page()
if(st.session_state['button1_pressed'] == 2):
with st.container():
st.write("This is page two")
cols1, cols2, cols3, cols4, cols5 = st.columns([1,1,1,1,1])
with cols1:
st.button('<',key='<2',on_click=previous_page)
with cols5:
st.button('>',key='2>',on_click=next_page)
time.sleep(5)
next_page()
if(st.session_state['button1_pressed'] == 3):
with st.container():
st.write("This is page three")
cols1, cols2, cols3, cols4, cols5 = st.columns([1,1,1,1,1])
with cols1:
st.button('<',key='<3',on_click=previous_page)
with cols5:
st.button('>',key='3>',on_click=next_page)
time.sleep(5)
next_page()
if(st.session_state['button1_pressed'] == 4):
with st.container():
st.write("This is page four")
cols1, cols2, cols3, cols4, cols5 = st.columns([1,1,1,1,1])
with cols1:
st.button('<',key='<4',on_click=previous_page)
with cols5:
st.button('>',key='4>',on_click=next_page)
time.sleep(5)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.