I have a dataset having different country name and as the count of unique countries are large I can’t put it manually in the checkbox. Also I want to add “All” & “None” option in the checkbox. I want to do the whole process dynamically. For better understanding I attach
Hi @udo,
Welcome to the community,
I implemented a very hacky way of achieving what you want, but it works
import streamlit as st
if 'dummy_data' not in st.session_state.keys():
dummy_data = ['IND','USA','BRA','MEX','ARG']
st.session_state['dummy_data'] = dummy_data
else:
dummy_data = st.session_state['dummy_data']
def checkbox_container(data):
st.header('Select A country')
new_data = st.text_input('Enter country Code to add')
cols = st.columns(10)
if cols[0].button('Add Coutry'):
dummy_data.append(new_data)
if cols[1].button('Select All'):
for i in data:
st.session_state['dynamic_checkbox_' + i] = True
st.experimental_rerun()
if cols[2].button('UnSelect All'):
for i in data:
st.session_state['dynamic_checkbox_' + i] = False
st.experimental_rerun()
for i in data:
st.checkbox(i, key='dynamic_checkbox_' + i)
def get_selected_checkboxes():
return [i.replace('dynamic_checkbox_','') for i in st.session_state.keys() if i.startswith('dynamic_checkbox_') and st.session_state[i]]
checkbox_container(dummy_data)
st.write('You selected:')
st.write(get_selected_checkboxes())
Hi @akshanshkmr thanks it worked for me but I want to use this code in multiple time my project so I defined it as a function. But it showed me an error as I don’t give unique key. So can you help me to solve this problem? I attached the error.
Glad it worked for you, it’s just a matter of providing a unique key to each checkbox that you create, maybe you can add a “key” parameter to your function and prefix that key to the checkbox keys.
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.