I’m looking to clear the file_uploader after the file have been read.
The application that I develop could lead to multiple download and selection of data. The problem is that after the download of the document, the differents selection start the reading for each manipulation. The only thing I can think of is to tell the users to delete the file from the file_uploader but it’s not an option for what I want to develop.
Hi @Toto, welcome to our community, we’re thrilled to have you with us!
Users can manually clear the uploaded file by using the uploader’s built-in “x” button if they wish to upload a different file - but if you need it programmatically, changing the key to a new value and using st.rerun() should do the trick, although I’ve not tried it.
Here’s a way to do it when the download button is clicked
import streamlit as st
if "uploader_key" not in st.session_state:
st.session_state.uploader_key = 0
uploaded_file = st.file_uploader(
"Choose a CSV file", type="csv", key=f"uploader_{st.session_state.uploader_key}"
)
def update_key():
st.session_state.uploader_key += 1
if uploaded_file is not None:
st.download_button(
"Download CSV",
data=uploaded_file,
file_name="uploaded_file.csv",
mime="text/csv",
on_click=update_key,
)
In this picture, you can see the file_updater on top. I use it to download several zip. Each zip is read and stored in a list and each element of the list appear as a line.
For each package, the user need to select the building with a selectbox.
The problem is when I want to change the selectbox, the file is read again. I need to clear the file_uploader just after I read the zip file. The code from @blackary doesn’t seem to work when I integrate it into my code…
My code (I put aside the rest of the code but I follow the code from @blackary ):
if file: #Object with a function that read the zip file and store it as an object.
st.session_state[‘Data_projet’].Add_data(file)
st.download_button(“Download ThBat zip”,
data=file,
on_click=Update_key,
)
(The building name is the one thing I cannot find in the zip file so it is very important that the user can update this data).
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.