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
else:
st.session_state["test"] = False
if "test" not in st.session_state:
st.session_state["test"] = True
st.checkbox(
"Flip the switch", value=st.session_state["test"], key="check", on_change=flip
)
st.write(st.session_state["test"])
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.
kelengkapan_klaim = {
'suket_dokter':{
'name':'Surat Keterangan Dokter',
'key':'kelengkapan_klaim_suket_dokter'
},
'invoice':{
'name':'Invoice',
'key':'kelengkapan_klaim_invoice'
},
'kwitansi':{
'name':'Kwitansi / Official Receipt / Bukti Bayar',
'key':'kelengkapan_klaim_kwitansi'
},
'tax_invoice':{
'name':'Tax Invoice (bila ada. Bila tidak ada maka tetap diceklis)',
'key':'kelengkapan_klaim_tax_invoice'
},
'resume_medis':{
'name':'Resume Medis / Discharge Summary',
'key':'kelengkapan_klaim_resume_medis'
},
'billing_details':{
'name':'Rincian biaya, perawatan, dan obat-obatan selama Rawat Inap',
'key':'kelengkapan_klaim_billing_details'
},
'hasil_lab':{
'name':'Hasil Laboratorium dan analisa dokter (baca rontgen, MRI,CT Scan, dll)',
'key':'kelengkapan_klaim_hasil_lab'
}
,'ktp':{
'name':'Copy KTP Tertanggung',
'key':'kelengkapan_klaim_ktp_tertanggung'
},
'rekening':{
'name':'Copy Buku Rekening Tabungan untuk pembayaran klaim',
'key':'kelengkapan_klaim_buku_rekening'
}
}
for item in kelengkapan_klaim:
st.checkbox(kelengkapan_klaim[item]['name'],key=kelengkapan_klaim[item]['key'],on_change=calc_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]:
st.session_state[item]=False
else:
st.session_state[item]=True
# 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.
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.
code
# Build checkbox.
for item in kelengkapan_klaim:
st.checkbox(kelengkapan_klaim[item]['name'], value=False, key=kelengkapan_klaim[item]['key'])
st.markdown('---')
# Get status.
for item in kelengkapan_klaim:
st.write(f'{kelengkapan_klaim[item]["name"]} state value is {st.session_state[kelengkapan_klaim[item]["key"]]}.')
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.