Created
August 20, 2016 15:51
-
-
Save aplavin/19f3607240c4c0a63cb1d223609ddc3b to your computer and use it in GitHub Desktop.
ipywidgets extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ipywidgets as iw | |
import traitlets | |
class SelectionSliderBtns(iw.Box): | |
description = traitlets.Unicode() | |
value = traitlets.Any() | |
options = traitlets.Union([traitlets.List(), traitlets.Dict()]) | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
self.slider = iw.SelectionSlider() | |
buttons = [] | |
for icon, ind_func in [ | |
('fa-step-backward', lambda ind: 0), ('fa-chevron-left', lambda ind: ind - 1), | |
('fa-chevron-right', lambda ind: ind + 1), ('fa-step-forward', lambda ind: -1)]: | |
btn = iw.Button(icon=icon) | |
buttons.append(btn) | |
btn.layout.width = '32px' | |
@btn.on_click | |
def _(*_, ind_func=ind_func): | |
ind = self.slider._options_values.index(self.value) | |
self.value = self.slider._options_values[ind_func(ind)] | |
index_input = iw.BoundedIntText(layout=iw.Layout(width='50px')) | |
len_label = iw.HTML(layout=iw.Layout(padding='5px 0 0 0')) | |
traitlets.link((self, 'options'), (self.slider, 'options')) | |
try: | |
traitlets.link((self, 'value'), (self.slider, 'value')) | |
except: | |
traitlets.link((self.slider, 'value'), (self, 'value')) | |
traitlets.link((self, 'description'), (self.slider, 'description')) | |
@observe(self, 'value') | |
def _(*_): | |
buttons[1].disabled = (not self.options or self.value == self.slider._options_values[0]) | |
buttons[2].disabled = (not self.options or self.value == self.slider._options_values[-1]) | |
for btn in buttons: | |
btn.button_style = '' if btn.disabled else 'success' | |
try: | |
ind = self.slider._options_values.index(self.value) + 1 | |
except: | |
ind = 0 | |
index_input.value = ind | |
@observe(self, 'options') | |
def _(*_): | |
len_label.value = '/ {}'.format(len(self.options)) | |
index_input.min = 0 | |
index_input.max = len(self.options) | |
@observe(index_input, 'value') | |
def _(*_): | |
self.value = self.slider._options_values[index_input.value - 1] | |
self.children = [iw.HBox([*buttons[:2], self.slider, index_input, len_label, *buttons[2:]])] | |
self.add_class('panel') | |
self.add_class('panel-default') | |
self.children[0].add_class('panel-body') | |
def select_first(self): | |
self.value = self.slider._options_values[0] | |
class ToggleButtonsMult(iw.Box): | |
description = traitlets.Unicode() | |
value = traitlets.Tuple() | |
options = traitlets.Union([traitlets.List(), traitlets.Dict()]) | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
self._selection_obj = iw.widget_selection._MultipleSelection() | |
traitlets.link((self, 'options'), (self._selection_obj, 'options')) | |
traitlets.link((self, 'value'), (self._selection_obj, 'value')) | |
@observe(self, 'options') | |
def _(*_): | |
self.buttons = [iw.ToggleButton(description=label, | |
layout=iw.Layout(margin='0', width='auto')) | |
for label in self._selection_obj._options_labels] | |
self.children = self.buttons | |
@observe(self.buttons, 'value') | |
def _(*_): | |
for btn in self.buttons: | |
btn.button_style = 'primary' if btn.value else '' | |
self.value = tuple(value | |
for btn, value in zip(self.buttons, self._selection_obj._options_values) | |
if btn.value) | |
self.add_class('btn-group') | |
def reset(self): | |
opts = self.options | |
self.options = [] | |
self.options = opts | |
def observe(widgets, trait_name): | |
def wrapper(func): | |
if isinstance(widgets, iw.Widget): | |
widgets.observe(func, trait_name) | |
else: | |
for w in widgets: | |
w.observe(func, trait_name) | |
func() | |
return wrapper | |
def _with_classes(self, *classes, remove=False): | |
if remove: | |
for c in classes: | |
self.remove_class(c) | |
else: | |
for c in classes: | |
self.add_class(c) | |
return self | |
iw.DOMWidget.with_classes = _with_classes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment