Checkbox dynamic true and false like toggle on / off

I am quite new to streamlit. I’m looking such a feature that work like this:


When dummy_name checked, then st.session_state[‘dummy_key’] = TRUE
When dummy_name unchecked, then st.session_state[‘dummy_key’] = FALSE

how do i resolve this?
Or is there any feature that suitable to my needs?


Hi @Cignitor

Here’s a code snippet that you can use to achieve what you’re looking for:

import streamlit as st

# callback to update 'test' based on 'check'
def flip():
    if st.session_state["check"]:
        st.session_state["test"] = True
        st.session_state["test"] = False

if "test" not in st.session_state:
    st.session_state["test"] = True

    "Flip the switch", value=st.session_state["test"], key="check", on_change=flip



This is from a previous post with a solution by @snehankekre: Session state issue with st.checkbox - #2 by snehankekre

Hope this helps!


Currently i have 9 checkboxes. Do i have to create 9 flip() according to each key?


You could achieve this by using a for loop to iterate through a list of 9 values/elements and use f-strings to generate unique keys and session state variable names f’check_{element}’ and f’test_{element}’, respectively.

This would give the same effect of manually creating 9 separate callback functions in less lines of code.

Hope this helps!

This is how i create st.checkbox

kelengkapan_klaim = {
        'name':'Surat Keterangan Dokter',
        'name':'Kwitansi / Official Receipt / Bukti Bayar',
        'name':'Tax Invoice (bila ada. Bila tidak ada maka tetap diceklis)',
        'name':'Resume Medis / Discharge Summary',
        'name':'Rincian biaya, perawatan, dan obat-obatan selama Rawat Inap',
        'name':'Hasil Laboratorium dan analisa dokter (baca rontgen, MRI,CT Scan, dll)',
        'name':'Copy KTP Tertanggung',
        'name':'Copy Buku Rekening Tabungan untuk pembayaran klaim',

        for item in kelengkapan_klaim:

Then, here’s the function:

def calc_kelengkapan_klaim():
    count_true = 0 #to have it presented in st.progress
    count_checkbox=0 # count_true / count_checkbox
    for item in st.session_state:        
        if 'kelengkapan_klaim' in item: #this is where st.checkbox is
            print("Item is {}".format(item))
            if st.session_state[item]:
            # count_checkbox+=1
            # print("Checking session state {} its value is {}".format(item,st.session_state[item]))
            # count_true+=1 if st.session_state[item] else count_true + 0
    print("count_true is {}. count_checkbox is {}".format(count_true,count_checkbox))

When i click one of the checkboxes, the function altered the rest checkboxes and vice versa.

The key in:

st.checkbox("dummy_name", key='dummy_key')

is already the session_state key of that checkbox.

  • Install the latest streamlit
  • Do not use on_change in the checkbox.

Try the following code.


# Build checkbox.
for item in kelengkapan_klaim:
    st.checkbox(kelengkapan_klaim[item]['name'], value=False, key=kelengkapan_klaim[item]['key'])


# Get status.
for item in kelengkapan_klaim:
    st.write(f'{kelengkapan_klaim[item]["name"]} state value is {st.session_state[kelengkapan_klaim[item]["key"]]}.')

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.