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

Also would like to know how to do this

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

Having the same issue!!

Also have the same question here

I want to learn this as well…

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.