Skip to content

Changing options of Select widget triggers Callback #2230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nickpollari opened this issue Sep 27, 2018 · 9 comments · Fixed by #3284
Closed

Changing options of Select widget triggers Callback #2230

nickpollari opened this issue Sep 27, 2018 · 9 comments · Fixed by #3284
Assignees
Milestone

Comments

@nickpollari
Copy link

@jasongrout we should have a way to change the options of a Select widget without triggering the callback function. Whenever the options are changed for the widget the value attribute is automatically set which forces the callback to be triggered. See code below;

from ipywidgets import Select

# callback
def cb(change):
    print(change)

# widget
s_w = Select(value=None, options=['a','b'])
s_w.observe(cb, names='value')

# triggers no callback
s_w.value = None
# triggers callback
s_w.options = ['1', '2', '3']
@jasongrout
Copy link
Member

Thanks!

As a dev note, I think we should special-case the code at https://github.com/jupyter-widgets/ipywidgets/blob/master/ipywidgets/widgets/widget_selection.py#L205-L212 so that if nothing is selected (as in the above code, where we explicitly set the value to None), we don't force a new selection.

@jasongrout jasongrout added this to the Minor release milestone Sep 30, 2018
@crahan
Copy link

crahan commented Apr 1, 2019

I'm currently running into this issue. I'm writing a file browser widget. As soon as I click a folder entry in the list it triggers the callback function that configures the options with the new folder contents (as expected). However, the first option is always being selected and triggers a callback, making the code go into a loop as long as the automatically selected entry is a folder.

I'd be very interested in any potential workarounds until a more permanent solution is available.

@nickpollari
Copy link
Author

@crahan my workaround has been to utilize the .unobserve method before updating the options.

s_w.unobserve(cb, 'value')
s_w.options = ['1','2','3']
s_w.observe(cb, 'value')

@crahan
Copy link

crahan commented Apr 2, 2019

@nickpollari that is awesome! Thank you so much. Everything's working as expected now. I added a s_w.value = None to make sure nothing is marked as selected.

dircontent.unobserve(on_dircontent_select, names='value')
dircontent.options = get_dir_contents(path,showhidden=False)
dircontent.observe(on_dircontent_select, names='value')
dircontent.value=None

@crahan
Copy link

crahan commented Apr 4, 2019

Here's a screenshot of the final result.

Screenshot 2019-04-04 at 21 41 39

@dreme
Copy link

dreme commented Jul 15, 2021

And a +awesome vote from me too for the @nickpollari 'widget unobserve' solution. I was seeing the same problem as @crahan for a RadioButton widget. Every time I updated the widget options I would get weird subtle bug behaviours due to the unexpected call-back trigger when the widget was updated.

@jasongrout jasongrout modified the milestones: Minor release, 8.0 Jul 15, 2021
@jasongrout
Copy link
Member

jasongrout commented Jul 15, 2021

Adding to 8.0 to implement the suggestion above at #2230 (comment) .

@nickpollari
Copy link
Author

thanks @crahan @dreme and @jasongrout for the upcoming fix

@martinRenou
Copy link
Member

Should fix #3284

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants