Button to uncheck all checkboxes

I tried so many methods and none of it work. Somehow the checkbox never unchecks completely.

def reset_matrix():
      state.matrix = {'c1': [0, 0, ], 'c2': [0, 0]}
      st.rerun()

matrix  = state.matrix
steps = 2

for row, column in matrix.items(): # {'Row1': [False, False, ..]}
            cols = st.columns([0.25] + [0.75] * steps)
            cols[0].success(row)

            for i in range(steps):
                hex_label = f'{i:01X}'  # Ensure the label is always a 1-character hexadecimal value
                key = f'{row}_{i}'
                column[i] = cols[i + 1].checkbox(label=hex_label, value=column[i], key=key, label_visibility='hidden')

        # Add a button that resets the beat matrix
        if st.button('Cleanup sequencer'):
            reset_matrix()

I tried to manually set all checkbox to False by state key and I get streamlit exception cannot modify widget after instantiated.

def reset_matrix():
    state.matrix = {'c1': [0, 0, ], 'c2': [0, 0]}
    state.update({f'{row}_{i}': False for row in state.matrix for i in range(2)})

This thing is so annoying. It such a simple problem that somehow persisted for over 5 years.

Hello,
I still got some bad behaviour with the session state (we need to press sometimes twice the reset button)

        # Define checkbox keys
        checkbox_keys = ['button', 'button2', 'button3', 'button4']

        # Initialize session state for checkboxes if not already done
        for key in checkbox_keys:
            if key not in st.session_state:
                st.session_state[key] = False

        # Function to reset checkboxes
        def reset_checkboxes():
            for key in checkbox_keys:
                st.session_state[key] = False

        # Create a container for checkboxes

        with st.container():
            # Calculate the number of columns needed
            num_cols = 2  # You can change this to adjust the layout
            num_rows = (len(checkbox_keys) + num_cols - 1) // num_cols

            # Create a grid layout of checkboxes
            for row in range(num_rows):
                cols = st.columns(num_cols)
                for col in range(num_cols):
                    idx = row * num_cols + col
                    if idx < len(checkbox_keys):
                        key = checkbox_keys[idx]
                        # Display the checkbox and update session state
                        st.session_state[key] = cols[col].checkbox(key, key=f"checkbox_{key}", value=st.session_state[key])
            # Reset button
            if st.button('Reset', on_click=reset_checkboxes):
                st.rerun()

Hope it’s what you are looking for !

I got this error because the key and the value are the same. Fix by changing the key name.

My current fix basically does a hard reset on every action. Weird behavior is that every first click will not be registered. At least the button works

    def reset_checkbox():
        """ 
        Instantiate the key BEFORE YOU CREATE WIDGET. Then assign widget to key so you can reset it anytime
        """
        for row, lst in state.matrix.items():
            for i in range(len(lst)):
                key = f'{row}_{i}'
                if key not in state:
                    state[key] = False
                state[key] = False
    

    def reset_matrix():
        state.matrix = init_dict()
        st.rerun()


    @st.experimental_fragment
    def seq_part():
        """ 
        Experimental fragment bc we doing a lot of request inside the checkbox so we isolating it as fragment
        """
        # Add a button that resets the matrix

        if st.button('Cleanup sequencer'):
            reset_matrix()

        matrix  = state.matrix
        steps = 16

        for row, lst in matrix.items(): # {'row1': [False, False, ..]}
            cols = st.columns([1] * steps)

            for i in range(steps):
                hex_label = f'{i:01X}'  # Ensure the label is always a 1-character hexadecimal value
                key = f'{row}_{i}'
                sequence[i] = cols[i + 1].checkbox(label=hex_label, value=lst[i], key=key, label_visibility='hidden')

        state.matrix = matrix    


   # app part
    reset_checkbox()
    seq_part()