button1 = st.button("Let's get started!")
if button1:
user_csv = st.file_uploader("Upload your file here!", type="csv")
if user_csv is not None:
st.write("Amazing! Let me have a look at your file.")
user_csv.seek(0)
df = pd.read_csv(user_csv, low_memory=False)
But the second if statement won run and the file uploader disappears after clicking the button. Why is this happening and how to fix it? Thanks!
Buttons do not save their state, so after you interact with the any other component, the button goes back to False. Here is a better explanation of that behavior, in section 1. Buttons aren’t stateful:
You could also try something with callbacks, if suitable:
if "mydf" not in st.session_state:
st.session_state.mydf = pd.DataFrame()
def UploaderCallBack(somekey):
if st.session_state[somekey] is not None:
st.write("Amazing! Let me have a look at your file.")
try:
st.session_state.mydf = pd.read_csv(st.session_state[somekey], low_memory=False)
st.dataframe(st.session_state.mydf)
except:
pass
if st.session_state.mydf.shape[0] == 0:
button1 = st.button("Let's get started!")
if button1:
user_csv = st.file_uploader("Upload your file here!", type="csv", key="somekey", on_change=UploaderCallBack, args=("somekey",))
if st.session_state.mydf.shape[0] > 0:
st.write("And let's do some other things too.")