Enter a key in text box, the App will run a query and return a list of options.
For each option, display a checkbox and check it by default.
The options from step 1 are likely to be the same among different runs but with small difference.
I don’t want to use multiselect drop down here.
The issue:
If I uncheck a checkbox, the state will be memorized even if the checkbox is regenerated.
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()
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!
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.
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.
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 ?
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.
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!
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.