class ImageButton(DOMWidget): disabled = Bool(False, help="Enable or disable user changes.", sync=True) _view_name = Unicode('ImageButtonView', sync=True) format = Unicode('png', sync=True) width = CUnicode(sync=True) height = CUnicode(sync=True) _b64value = Unicode(sync=True) value = Bytes() def _value_changed(self, name, old, new): self._b64value = base64.b64encode(new) def __init__(self, **kwargs): super(ImageButton, self).__init__(**kwargs) self._click_handlers = CallbackDispatcher() self.on_msg(self._handle_button_msg) def on_click(self, callback, remove=False): self._click_handlers.register_callback(callback, remove=remove) def _handle_button_msg(self, _, content): if content.get('event', '') == 'click': self._click_handlers(self, content)
class ChildrenChangeNotifierMixin: def __init__(self, *args, **kwargs): self._children_changed_handlers = CallbackDispatcher() def on_children_changed(self, callback, remove=False): """Register a callback to execute when the children are changed. The callback will be called with one argument, this Innotation winstance. Parameters ---------- callback: method handle Method to be registered or unregistered. remove: bool (optional) Set to true to remove the callback from the list of callbacks. """ self._children_changed_handlers.register_callback(callback, remove=remove) def children_changed(self, newchildren): """Programmatically trigger a click event. This will call the callbacks registered to the clicked button widget instance. """ self._children_changed_handlers(self, newchildren)
class ExecuteButton(DOMWidget): _view_name = Unicode('ExecuteButtonView').tag(sync=True) _model_name = Unicode('ExecuteButtonModel').tag(sync=True) _view_module = Unicode('jupyter-button_execute').tag(sync=True) _model_module = Unicode('jupyter-button_execute').tag(sync=True) _view_module_version = Unicode('^0.4.0').tag(sync=True) _model_module_version = Unicode('^0.4.0').tag(sync=True) # Attributes button_text = Unicode('Execute next cells', help="The button's caption.").tag(sync=True) n_next_cells = Int(1, help="The number of cells to execute.").tag(sync=True) disabled = Bool(False, help="Disable the button").tag(sync=True) button_style = CaselessStrEnum( values=['primary', 'success', 'info', 'warning', 'danger', ''], default_value='', help="""Use a predefined styling for the button.""").tag(sync=True) style = InstanceDict(ButtonStyle).tag(sync=True, **widget_serialization) def __init__(self, **kwargs): super(ExecuteButton, self).__init__(**kwargs) self._click_handlers = CallbackDispatcher() self.on_msg(self._handle_button_msg) def on_click(self, callback, remove=False): """Register a callback to execute when the button is clicked. The callback will be called with one argument, the clicked button widget instance. Parameters ---------- callback: The function to call remove: bool (optional) Set to true to remove the callback from the list of callbacks. """ self._click_handlers.register_callback(callback, remove=remove) def click(self): """Programmatically trigger a click event. This will call the callbacks registered to the clicked button widget instance. """ self._click_handlers(self) def _handle_button_msg(self, _, content, buffers): """Handle a msg from the front-end. Parameters ---------- content: dict Content of the msg. """ if content.get('event', '') == 'click': self.click()
class ButtonInnotation(Innotation): """ Allow embeding of an arbitrary widget object, e.g. for text display Must still have a data attribute of correct len, even if dummy values """ requires_data = False has_data_changed_notifier = True def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.on_click_hook = kwargs.get('on_click', None) self.uindex = None self._data_changed_handlers = CallbackDispatcher() def _create_widget(self): btn = Button(description=self.desc, disabled=self.disabled, layout=self.layout) if self.on_click_hook: btn.on_click(lambda b: self.call_on_click_hook(b)) return btn def call_on_click_hook(self, b): och = self.on_click_hook changed = och(self.uindex, self.repeat_index, name=self.name, desc=self.desc) if changed: self.data_changed() def update_ui(self, uindex): self.uindex = uindex def update_data(self, uindex): pass def on_data_changed(self, callback, remove=False): """Register a callback to execute when the children are changed. The callback will be called with one argument, this Innotation winstance. Parameters ---------- remove: bool (optional) Set to true to remove the callback from the list of callbacks. """ self._data_changed_handlers.register_callback(callback, remove=remove) def data_changed(self): """Programmatically trigger a click event. This will call the callbacks registered to the clicked button widget instance. """ self._data_changed_handlers(self)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.rows_count = 0 self.childinnotationconfigs = args self.childinnotations = [] self.min_repeats = kwargs.get('min_repeats', 0) self.max_repeats = kwargs.get('max_repeats', 10) if self.max_repeats < self.min_repeats: raise Exception("min_repeats is greater than max_repeats") self._children_changed_handlers = CallbackDispatcher()
class FocusText(widgets.Text): _view_name = Unicode('FocusTextView').tag(sync=True) #_model_name = Unicode('FocusTextModel').tag(sync=True) _view_module = Unicode('jupyter-innotater').tag(sync=True) #_model_module = Unicode('jupyter-innotater').tag(sync=True) _view_module_version = Unicode('~0.1.0').tag(sync=True) #_model_module_version = Unicode('~0.1.0').tag(sync=True) def __init__(self, **kwargs): super(FocusText, self).__init__(**kwargs) self._click_handlers = CallbackDispatcher() self.on_msg(self._handle_focustext_msg) def on_click(self, callback, remove=False): """Register a callback to execute when the button is clicked. The callback will be called with one argument, the clicked button widget instance. Parameters ---------- remove: bool (optional) Set to true to remove the callback from the list of callbacks. """ self._click_handlers.register_callback(callback, remove=remove) def click(self): """Programmatically trigger a click event. This will call the callbacks registered to the clicked button widget instance. """ self._click_handlers(self) def _handle_focustext_msg(self, _, content, buffers): """Handle a msg from the front-end. Parameters ---------- content: dict Content of the msg. """ if content.get('event', '') == 'click': self.click()
def __init__(self, *args, **kwargs): self._children_changed_handlers = CallbackDispatcher()
def __init__(self, *args, **kwargs): self.repeat_index = kwargs.get('repeat_index', -1) self._data_changed_handlers = CallbackDispatcher()
def __init__(self, **kwargs): super(ImageButton, self).__init__(**kwargs) self._click_handlers = CallbackDispatcher() self.on_msg(self._handle_button_msg)
class RepeatInnotation(Innotation): has_children_changed_notifier = True requires_data = False def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.rows_count = 0 self.childinnotationconfigs = args self.childinnotations = [] self.min_repeats = kwargs.get('min_repeats', 0) self.max_repeats = kwargs.get('max_repeats', 10) if self.max_repeats < self.min_repeats: raise Exception("min_repeats is greater than max_repeats") self._children_changed_handlers = CallbackDispatcher() def post_widget_create(self, datamanager): while self.rows_count < self.min_repeats: self.add_row() def _create_widget(self): self.addbtn = Button(description='Add') self.addbtn.on_click(self.add_row_handler) vbox = VBox([self.addbtn]) vbox.add_class('repeat-innotation') return vbox def add_row_handler(self, btn): self.add_row() def add_row(self): newchildren = [] for c in self.childinnotationconfigs: if isinstance(c, tuple) or isinstance(c, list): kwargs = {} if len(c) <= 2 else c[2] kwargs['repeat_index'] = self.rows_count if 'name' in kwargs: kwargs['name'] = '{}_{}'.format(kwargs['name'], self.rows_count) newchildren.append(c[0](c[1], **kwargs)) else: newchildren.append(c(self.data)) self.rows_count += 1 self.children_changed(newchildren) self.get_widget().children = tuple( list(self.get_widget().children) + [HBox([c.get_widget() for c in newchildren])]) self.childinnotations.extend(newchildren) if self.max_repeats == self.rows_count: self.addbtn.disabled = True def update_ui(self, uindex): for innot in self.childinnotations: innot.update_ui(uindex) def update_data(self, uindex): for innot in self.childinnotations: innot.update_data(uindex) def on_children_changed(self, callback, remove=False): """Register a callback to execute when the children are changed. The callback will be called with one argument, this Innotation winstance. Parameters ---------- remove: bool (optional) Set to true to remove the callback from the list of callbacks. """ self._children_changed_handlers.register_callback(callback, remove=remove) def children_changed(self, newchildren): """Programmatically trigger a click event. This will call the callbacks registered to the clicked button widget instance. """ self._children_changed_handlers(self, newchildren)
def __init__(self, **kwargs): super(FocusText, self).__init__(**kwargs) self._click_handlers = CallbackDispatcher() self.on_msg(self._handle_focustext_msg)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.on_click_hook = kwargs.get('on_click', None) self.uindex = None self._data_changed_handlers = CallbackDispatcher()