Search code examples
pythoncallbackjupyter-notebooktraitsipywidgets

Set ipywidget options with nothing selected


When I want to set options in an ipywidget with nothing selected I would simply do this:

import ipywidgets as w
w.Select(options=[1, 2], value=None)

ipywidget Select

What if I wanted to set new items (with nothing selected) on an existing object? I would have to:

import ipywidgets as w
s = w.Select()
...
s.options = [1,2]
s.value = None

But if there is a callback registered to receive notifications on value changes, it would be called twice: with autoselected 1 and then with None. None can be caught easily, but 1 seems like a valid value chosen by the user. That would be a false callback, since user never really chose it.

Is there an easy way around it?


Solution

  • It looks like s.options = [None, 1, 2] is good enough. Adding None in front avoids firing any unwanted callbacks.