Resetting upload or select if the other is being used?

The app I’m building allows users to either select a document from out database or to upload their own (I’m using st.sidebar.selector and st.upload widgets). If the user changes from widget to the other, I’d like the previous widget to reset. Currently, the old widget stays the same when the other is executed. Can anyone provide guidance on how to reset the old widget when the other is used?

Here’s the code I’m using for the sidebar functionality:

with upload_placeholder.info(" 👈 Select document or upload your own to start chat"):  
    st.sidebar.header("Select a File or Upload New Document")
    with st.sidebar:

        sidebar_completed = False
        st.session_state.vectore_store = None
        
        # Adding empty line for spacing
        st.markdown("") 

         # Radio button for user confirmation with agreement link
        agreement_link = "[User Agreement](https://google.com)"
        user_confirmation = st.checkbox(label=f"I confirm that I have read and understood the {agreement_link}.")
        
        if user_confirmation:  # User confirmed, allow document selection/upload

            # Create a dropdown menu in the sidebar for file selection
            file_selected = st.sidebar.selectbox(label="Select a File", options=saved_docs, placeholder='Choose an option', index=None )
            select_sidebar_completed, selected_vectore_store = select_document_sidebar(file_selected)
            
            # Widget to upload new document
            file_input = st.file_uploader("Upload your PDF file", type="pdf")
            upload_sidebar_completed, uploaded_vectore_store = upload_document_sidebar(file_input)
            
            # Determine if a document is selected or uploaded and remove the old document
            if select_sidebar_completed:
                if "message_container" in st.session_state:
                    st.session_state.message_container.empty()
                message_container = st.empty()
                message_container.success('Document loaded! Ready to Chat!', icon="✅")
                st.session_state.message_container = message_container

                sidebar_completed = select_sidebar_completed
                upload_placeholder.empty()
            
            elif upload_sidebar_completed:           
               if "message_container" in st.session_state:
                    st.session_state.message_container.empty()
                message_container = st.empty()
                message_container.success('Document processed successfully! Ready to Chat!', icon="✅")
                st.session_state.message_container = message_container
                sidebar_completed = upload_sidebar_completed
                upload_placeholder.empty()

Hi @Vincent_Goldberg,

Welcome to our community! It’s great to have you here! :hugs:

It’s difficult to be certain without access to the full code, but you might want to implement a mechanism to reset the state of one widget (either the file selector or the uploader) when the other is used.

Perhaps something along these lines, leveraging session state, may solve your issue:

with upload_placeholder.info(" 👈 Select a document or upload your own to start chat"):
    st.sidebar.header("Select a File or Upload New Document")
    with st.sidebar:

        sidebar_completed = False
        st.session_state.vectore_store = None
        
        # Adding an empty line for spacing
        st.markdown("") 

        # Radio button for user confirmation with an agreement link
        agreement_link = "[User Agreement](https://google.com)"
        user_confirmation = st.checkbox(label=f"I confirm that I have read and understood the {agreement_link}.")

        if user_confirmation:  # User confirmed, allow document selection/upload

            # Check if the last interaction was with the other widget and reset if needed
            if 'last_interaction' in st.session_state:
                if st.session_state.last_interaction == 'upload' and 'file_selected' in st.session_state:
                    del st.session_state.file_selected
                elif st.session_state.last_interaction == 'select' and 'file_input' in st.session_state:
                    del st.session_state.file_input

            # Create a dropdown menu in the sidebar for file selection
            file_selected = st.sidebar.selectbox(label="Select a File", options=saved_docs, placeholder='Choose an option', index=None, key="file_selected")
            select_sidebar_completed, selected_vectore_store = select_document_sidebar(file_selected)
            
            # Widget to upload a new document
            file_input = st.file_uploader("Upload your PDF file", type="pdf", key="file_input")
            upload_sidebar_completed, uploaded_vectore_store = upload_document_sidebar(file_input)
            
            # Determine if a document is selected or uploaded and remove the old document
            if select_sidebar_completed:
                st.session_state.last_interaction = 'select'
                # Rest of your code...

            elif upload_sidebar_completed:
                st.session_state.last_interaction = 'upload'
                # Rest of your code...

Let me know how this goes.

Best,
Charly