On_change selectbox with only one option

I have searched the problem but I didn’t come across a solution.
I have several selectboxes chained dynamically. If there appear only one option in one of those selectboxes, the rest won’t work because I am not able to call on_change function. I mean that on_change callback is not called with selectbox with one option.

What is recommended solution for this problem?

Hi @dnelub,

Thanks for posting!

Can you share a code snippet so we can reproduce this behavior?

Caroline :balloon:

Hi Caroline

def exp_list():
    st.session_state['exp_list'] = ['2001','2002','2003']
first_sb = st.selectbox('please select an option', options= ['option1'], key ='first_sb', on_change=exp_list)
second_sb = st.selectbox('please select a year', options= st.session_state['exp_list'], key ='second_sb')

I can reproduce the problem with the code above…
The first select box has only one option (option1) and the second select box is filled by the list of st.session_state[‘exp_list’] which is assigned in the function of exp_list which is called when there is a change in the first one. This is how I created chained select boxes.
As there is only one option in the first select box, ‘on_change callback’ is not working because it won’t change.
In my original code, there are 6 different select boxes with dynamically created options. Whenever there appears one in the middle with an option, the chain is broken because the rest cannot be activated.
I may be creating chained select boxes wrong. Is there any other way to do it?

Thanks in advance for the help.

Thanks for clarifying – I think I understand the issue now, but the reason the on_change function doesn’t get called with a one-option selectbox is that the value of that selectbox will not change (since there’s just one option/nothing for the user to change the value to).

Since there’s only one option for the user to click at that point in the app logic, I would recommend using a button rather a selectbox. You can then move the function that would be the on_change function for the one-option selectbox to be the on_click function for that button.

As I have 6 different chained select boxes with dynamically created options according to the imported data, I dont know which one is going to have one option and when it is going to have one option. So, I cannot use a button.
The simple code I provided above with two select boxes is just an example to reproduce the problem.
According to the documentation, on_click is not a property of select box.
A workaround would be adding a default option for all select boxes in order to have at least two options for each select boxes.
but I don’t think I am the only one going through this problem, am I?

In this case, the on_change function not being called when the value of the selectbox widget isn’t changing is expected behavior.

One other workaround would be to have an if statement that checks if the length of the (dynamically generated) list options is equal to 1, and if so, create a button rather than a selectbox.

1 Like