class DropdownWithEdit(ipywidgets.Widget): """ Dropdown, but displays an edit button if the current selection is a parameterized object. """ # I couldn't figure out which widget class actually declares the # value trait. Might not be needed in ipywidgets>6 because I # can see value trait declared in ValueWidget... value = traitlets.Any() def __init__(self, *args, **kwargs): self._select = Dropdown(*args, **kwargs) self._edit = Button(description='...', layout=Layout(width='15px')) self._composite = HBox([self._select, self._edit]) super(DropdownWithEdit, self).__init__() self.layout = self._composite.layout # so that others looking at this widget's value get the # dropdown's value traitlets.link((self._select, 'value'), (self, 'value')) self._edit.on_click(lambda _: editor(self._select.value)) self._select.observe(lambda e: self._set_editable(e['new']), 'value') self._set_editable(self._select.value) def _set_editable(self, v): if hasattr(v, 'params'): self._edit.layout.display = None # i.e. make it visible else: self._edit.layout.display = 'none' def _ipython_display_(self, **kwargs): self._composite._ipython_display_(**kwargs) def get_state(self, *args, **kw): # support layouts; see CrossSelect.get_state return self._composite.get_state(*args, **kw)