Checkbox callback (on_change) and checkbox checked (return)

Why are the results of checkbox and button different after selection?

import streamlit as st

def outJSON():
	jsonClass ={"dict1": {"A": agree1,
			              "B": agree2,
		    	     	  "C": agree3,
					      "D": agree4,
					      "E": agree5}}
	return jsonClass
# =========================================================
# return error
def stream_return():  
	st.write(outJSON())
# =========================================================

A_col1, A_col2 , _ = st.columns(3)
with A_col2:
	BU3 = st.button('out',key ='button3')

B_col1, B_col2, B_col3, B_col4, B_col5 = st.columns(5)
with B_col1:
	agree1 = st.checkbox('A', key = 'checkbox1',
						on_change = stream_return)
with B_col2:
	agree2 = st.checkbox('B', key = 'checkbox2', 
					     on_change = stream_return)
with B_col3:
	agree3 = st.checkbox('C', key = 'checkbox3', 
						 on_change = stream_return)
with B_col4:
	agree4 = st.checkbox('D', key = 'checkbox4', 
						 on_change = stream_return)
with B_col5:
	agree5 = st.checkbox('E', key = 'checkbox5', 
						 on_change = stream_return)

# =========================================================
# return correct
if BU3:
	st.write(outJSON())
# =========================================================

Solution (update)
The value of agree1, agree2, etc. are not updated until after the stream_return function runs.

Hi @tuhlnaa,

Can you share the specific results that you’re not understanding? I ran your code and the results make sense given that a checkbox’s callback function runs as soon as you check or uncheck the box (in other words, the value of agree1, agree2, etc. are not updated until after the stream_return function runs).

Best,

Caroline

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