How to clear session state when click the 'x' in file_uploader

Hey - I am having trouble accessing the setting behind the close button (‘x’) in the file uploader.

I need the session state to be clear if user closes the current file. Currently, when i click ‘x’ and upload a new one, the file wont be rewritten.

Thanks!

image

3 Likes

Also would like to know how to do this

1 Like

I’m running into the same problem. Is there any solution to it?

1 Like

Having the same issue!!

1 Like

Also have the same question here

1 Like

I want to learn this as well…

1 Like

Hi @Zoe2, welcome to the forum!

You can make sure the session state gets updated by adding a key to the file uploader, like this:

st.file_uploader("Upload a file", key="file_uploader")
# See the current value like this:
st.write(st.session_state.file_uploader)

Thanks, what if the widget is defined as a variable to be used further like

variable_df = st.file_uploader()

The same solution works just fine

You can also access the currently-uploaded file via st.session_state.file_uploader if you set file_uploader as the key, even if you don’t assign the output to a variable directly.

1 Like