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).