How to reset checkbox

My target:

  1. Enter a key in text box, the App will run a query and return a list of options.
  2. For each option, display a checkbox and check it by default.
  3. The options from step 1 are likely to be the same among different runs but with small difference.
  4. I don’t want to use multiselect drop down here.

The issue:

  1. If I uncheck a checkbox, the state will be memorized even if the checkbox is regenerated.
  2. Yes I tried to give the a unique id for each checkbox but it will keep resetting every checkbox.

A example code:

import streamlit as st
import numpy as np

def main():
    case_id = st.text_input("Please enter an ID")
    if case_id:
        options = sorted(np.random.choice([1,2,3,4,5], 4, replace=False))
        pathways_bool = [st.checkbox(str(_x),True) for _x in options]


if __name__ == "__main__":
    main()

Thanks for your help!

2 Likes

Hey @Bo9562,

Welcome to the Streamlit Community! I’m afraid I don’t quite understand the problem you have here (sorry about that!). Streamlit is designed so that when you check or uncheck a box, that would usually feed back into your script somewhere and dynamically change something on the web page. For example, if you unchecked the option ‘2’, the script would re run and then display/not-display something that was linked to that ‘2’ checkbox.

When the script reruns, technically the checkboxes are regenerated then, and if it never remembers the users selection, then nothing will happen on the page.

Why are you looking for this functionality? A more specific example in this case may help us identify a different widget that might be more suited to your needs!

Hope this helped!
Happy Streamlit-ing!
Marisa

1 Like

Dear all,

I’d like to come back to this issue on reseting the mark on a checkbox.

In my app I have a checkbox that is used to initiate a connection to a measuring device.
Once checked, that app tries to get the connection established in the checkbox’s callback function.

However, sometimes this attempt may fail, e.g. the device is switched off. In this case I want the checkbox to become unchecked again. I tried deleting the key (if it exists) associated with the checkbox before but the box stubbornly remembers it’s previous state.

Is there some way to accomplish, what I want?

Thanks a lot!
Markus

Hi,
We’ve faced this same issue as w-markus, and it seems that the checkbox issue has not been resolved yet.

Could anyone please guide us to the solution ?

To recapitulate, we need the checkbox to become unchecked again after a re-run.

Hey @w-markus,

First, welcome to the Streamlit Community! :partying_face: :nerd_face: :raised_hands: :partying_face: :tada: :tada:

and @h3045,

With st.session_state you can have the checkbox un-check on a re-run using the on_change parameter. But as no one has posted a code example of how to reproduce this, or of the functionality you are trying to achieve it’s difficult to point you in the right direction.

Session State blog post and examples

Does anyone have an MVP they can post that I can try locally?

Happy Streamlit-ing!
Marisa

import sys
from turtle import onclick
import streamlit as st
import pandas as pd
st.markdown(""" <style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style> """, unsafe_allow_html=True)

##Working with the sidebar

original_title = '<p style="font-family:Times New Roman; color:White; font-size: 25px;">Select your Database </p>'
st.sidebar.markdown(original_title, unsafe_allow_html=True)

# adding a selectbox for number of databases
add=st.sidebar.selectbox('',options=('None','DB1','DB2'))

#working with first database
if add=='DB1':
    st.sidebar.write('')
    #putting query and reset buttons inside DB1 section
    col1, col2 = st.sidebar.columns([2,6])
    with col1:
        query=st.button('Query')
    with col2:
        reset=st.button('Reset')  
    st.sidebar.write('')

# putting checkboxes for selection
    checkbox_title='<p style="font-family:Times New Roman; color:White; font-size: 21px;">Select the columns you want to fetch in query </p>'
    st.sidebar.markdown(checkbox_title,unsafe_allow_html=True)
    option_s=st.sidebar.checkbox('p')
    option_u=st.sidebar.checkbox('pa')
    option_v=st.sidebar.checkbox('CA')
    option_a=st.sidebar.checkbox('En')
    option_t=st.sidebar.checkbox('ti')

    known_variables = option_s + option_u + option_v + option_a + option_t

    st.write(option_a)
    if known_variables <2:
        st.sidebar.write('You have to select minimum 2 variables.')
    elif known_variables >= 2:
        st.sidebar.write('You can click on query to search the results')

I am trying to reset all the checkboxes using the reset button ut I am not able to do it

Hi,
To all wondering, we managed a hacky way to make the check boxes get unchecked.

import streamlit as st
import time

if 'boxbool' not in st.session_state:
    st.session_state.boxbool = False
someholder = st.empty()
if not st.session_state.boxbool:
    if 'first edit' in st.session_state:
        del st.session_state['first edit']
    someedit = someholder.checkbox('Edit',key='first edit')
else:
    if 'second edit' in st.session_state:
        del st.session_state['second edit']
    someedit = someholder.checkbox('Edit',key='second edit')        
delete = st.button('Submit')
if delete:
    if st.session_state.boxbool == False:
        st.session_state.boxbool = True
    else:
        st.session_state.boxbool = False
    st.experimental_rerun()    


# ''' The below implementation SHOULD technically work but doesn't
# someholder = st.empty()
# if 'first edit' in st.session_state:
#     del st.session_state['first edit']
#     # Can we delete something else that makes the app forget
# someedit = someholder.checkbox('Edit',key='first edit')
# delete = st.button('Submit')
# if delete:
#     someholder.empty()
#     st.experimental_rerun()
# '''

@Marisa_Smith Could you try this locally to point us in the right direction ?

Hi, anybody here?

Hi @Amit_Lohani,

Apologies for the delay. If you are looking to have all the checkboxes reset when the button reset is pushed then all you need to do is pass the on_click parameter a function that sets all the checkboxes back to false! (pass the key parameter to each checkbox with a unique string to add them to the session sate object)

it might look something like this: NOTE: untested code- but it tells you the flow and generally how to solve this

# call back function -> runs BEFORE the rest of the app
def reset_button():
    st.session_state["p"] = False
return

#button to control reset
reset=st.button('Reset', on_click=reset_button)

#checkbox you want the button to un-check
check_box = st.checkbox("p", key='p')

#write out the current state to see that our flow works
st.write(st.session_state)

you can see in the st.write statement that you can click the checkbox “p”, have it return the value true, and then hit the reset button and it will become unchecked.

Happy Streamlit-ing!
Marisa

3 Likes

hey @h3045,

I am not sure what your trying to achieve here with your code (since its fairly complex, as much detail in comments would be appreciated!)

but in my example above I code a mock-up of how to reset a checkbox using state!

Happy Streamlit-ing
Marisa

1 Like

sorry commented on the wrong post

I just noticed there is some confusing with the attributes of checkbox. I thought the ‘value’=True/False would control the state of checkbox, but now I realised it is the ‘key’ which sets the state of the checkbox!

This solved my problem!