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.