I have a text and a button where the button is to load some data. How can i get rid of them after the data has been loaded.
if df is None:
st.write("No data has been loaded")
fetch = st.button("Load Data")
else:
fetch = False
This is what i am doing right now, i tried making fetch False and doing st.rerun() but that doesn’t work. I think i can use “with” but cant get it to work either.
Welcome to the community and thanks for your question! You’re on the right track—Streamlit reruns the script on every interaction, so to hide the text and button after loading data, you should store the loaded data (e.g., a DataFrame) in st.session_state. Then, conditionally display the text and button only if the data isn’t loaded yet. Setting fetch = False or using st.rerun() alone won’t persist the data or hide the widgets, since variables reset on rerun unless stored in session state.
Here’s a minimal example:
import streamlit as st
import pandas as pd
if "df" not in st.session_state:
st.session_state.df = None
if st.session_state.df is None:
st.write("No data has been loaded")
if st.button("Load Data"):
# Replace with your data loading logic
st.session_state.df = pd.DataFrame({"A": [1, 2, 3]})
else:
st.write("Data loaded!")
st.dataframe(st.session_state.df)