Clear the file_uploader after using the file data

Hello,

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.

I try the following topic : Are there any ways to clear file uploader values without using streamlit form
But I don’t want to add a button to make that move and it doesn’t seems to work if I try to add the code into the file_uploader condition.

Someone can help me ?

Tks

Hi @Toto, welcome to our community, we’re thrilled to have you with us! :hugs:

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.

Have you looked into this?

Best,
Charly

Yes, I already try it and it seems to not working as expected when you do it.
I try the same way as the following topic:

And I had a loop until I close manually (I did the incrementing in the key value).

I welcome other methods to download the files but I didn’t seen any other way…

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,
    )
1 Like

I’m sorry, it doesn’t seems to work… With an image it could be clearer :

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 :slight_smile:

Hello,

I didn’t succes to clear the file_uploader…
But I put a condition on the file_id

I store all the file_id into a list and put the following condition.

if file.file_id not in st.session_state[‘Data_projet’].files_id:

That do the job well.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.