I have searched the problem but I didn’t come across a solution.
Problem:
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.
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 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.
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.