How to delete several widgets programmatically by clicking button?

I create cv inference app and want to add clear screen button. Images are uploaded through input. So, as I understood, I need to clean input’s uploaded files and screen from inference widgets. I seemingly implemented cleaning input using:

        if not 'uploaded_files' in session:
            session.uploaded_files = str(uuid.uuid4())

        if st.sidebar.button('Clean screen'):
            session.uploaded_files = str(uuid.uuid4())

          uploaded_files = st.file_uploader('Select files', key=session.uploaded_files,
                                            accept_multiple_files=True)

But I can’t find out how to clean screen from widgets.

Could you be a bit more specific about the behavior you’re looking for, and what you’ve tried so far?

One general idea is to use st.session_state to keep track of which widgets should be shown on the screen, and when you click a button you can update the session state and use st.experimental_rerun to rerender the app with the new widgets shown/hidden.

Here’s an example:

Thanks for providing the example. So as I understood from it, I can delete several widgets which I will put in the container using click handler:

del st.session_state["container_id"]
st.experimental_rerun()
1 Like

I wrote code like this, but it doesn’t work. Could you tell why?

        if "widgets" not in session:
            container = st.container()
            session.widgets = {'container': container}
            
        for k, v in session.widgets.items():
            new_v = type(v)()
            if new_v != v:
                session.widgets[k] = new_v
            
        if st.sidebar.button('Clear'):
            session.uploaded_files = str(uuid.uuid4())
            container = session.widgets["container"]
            del session.widgets["container"]
            session.widgets["container"] = type(container)()

         uploaded_files = st.file_uploader('Select files', key=session.uploaded_files,
                                            accept_multiple_files=True)

        # Add widgets to container depending on inference (like this)
        session.widgets["container"].json('{"name": "result"}')

I’m sorry, it’s not entirely clear to me what exactly you are expecting that script to do. Could you give a complete simplified script that you expect to work, including how you plan to add widgets?