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?