St.selectbox executes on_change callback when a different selectbox changes value

Hi! As the title says. When I select any value on Selectbox 1 β€˜sb1’ the callback function associated to Selectbox 2 β€˜sb2’ gets activated. Expected functionality is that function is executed ONLY when Selectbox 2 values change (Option 3 or Option 4 are chosen).

Am I doing something wrong or is this a known bug?

import streamlit as st
from datetime import datetime


def print_func(words):
    now = datetime.now()
    print(f'{now.minute}m{now.second}s - {words} changed')
    

sb1 = st.selectbox(
    label='Selectbox 1',
    options=['Option 1', 'Option 2'],
)

sb2 = st.selectbox(
    label='Selectbox 2 - on_change callback',
    options=['Option 3', 'Option 4'],
    on_change=print_func('selectbox')
)```

Tested on Streamlit 1.30 and 1.31.1
Python 3.10

Can you try?

def print_func(words):
    #same

sb1 = st.selectbox(
#same
)

sb2 = st.selectbox(
    label='Selectbox 2 - on_change callback',
    options=['Option 3', 'Option 4'],
    on_change=print_func,
    args=('selectbox',)  # Passing 'selectbox' as an argument to print_func
)

1 Like

I understand the mistake now. Thanks so much!

Your welcome :hatched_chick:

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