How to remove the uploaded file in Streamlit when clicking on the self-made "Reset all" streamlit button?

Summary

I was wondering if anyone could kindly clarify whether the only way to remove/clear uploaded file(s) is still by hitting the โ€˜xโ€™ button or if there is an official programmatic way to clear them. The reason I ask is because I have created a streamlit button called "Reset all" where this button will make the app return to the state of when the program just successfully executed via streamlit run app.py command.

Thatโ€™s mean the program is returning to the beginning stage without any user input.

Steps to reproduce

Code snippet:

def initialize_session_state():
    if "no_file_conversation" not in st.session_state:
        st.session_state.no_file_conversation = [
            SystemMessage(content="I'm a PDF Chatbot. Ask me a question about your documents!")
        ]
    if "conversation" not in st.session_state:
        st.session_state.conversation = None
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = None
    if 'user_input_prompt' not in st.session_state: # This one is specifically use for clearing the user text input after they hit enter
        st.session_state.user_input_prompt = 'None'
    if "pdf_docs" not in st.session_state:
        st.session_state.pdf_docs = None
    if "is_processed" not in st.session_state:
        st.session_state.is_processed = None
    if "is_vectorstore" not in st.session_state:
        st.session_state.is_vectorstore = False
    if "extra_class" not in st.session_state: # This is a control variable, use to check the type of user's last conversation, either 'Warning' or 'None'
        st.session_state.extra_class = None

# Reset button part
reset = st.sidebar.button('Reset all')
if reset:
   st.session_state.clear()
   initialize_session_state()

 
Expected behavior:
The web app should look like this after clicking the Reset all button.

 
Actual behavior:
When I have done with the chatting, I clicked on the Reset all button and it didnโ€™t remove the file I uploaded.

Debug info

  • Streamlit version: 1.23.1
  • Python version: 3.10.8
  • Using: PipEnv
  • OS version: Windows 11 Home
  • Browser version: Goole Chrome 114.0.5735.199 (Official Build) (64-bit)

Links

Hey @mingjun1120 , I found this solution that might work for you Are there any ways to clear file uploader values without using streamlit form

1 Like

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