On_change behavior for checkboxes

Hi All,
Iโ€™m wondering whether Iโ€™m doing something wrong or whether it is intentional behavior. Iโ€™m creating multiple checkboxes with an on_change function. The on_change function seems to be called for all checkboxes on initialization. In addition, when one changes 1 of the checkboxes, the code seems to call the function for all checkboxes.

My expected behavior was: No callbacks on initialization and one callback of the relevant checkbox on change.

Steps to reproduce

Code snippet:

import streamlit as st

def on_change_checkbox(filter_id):
    print(filter_id)

for i in range(0,10):
    st.checkbox(str(i), on_change=on_change_checkbox(i))

You are calling the function and then passing None

You need to pass the callable to the on_change keyword argument, and the arguments that callable takes in the args and kwargs:

st.checkbox(str(i), on_change=on_change_checkbox, args=(i,))

1 Like

Thanks. Canโ€™t believe Iโ€™ve missed that input option. Marked it as the solution!

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