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.")
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.