Beispiel #1
0
    def __init__(self, keys, scheduler=None, minimum=0, dt=0.1, func=key_split,
                 complete=False):
        self.func = func
        keys = {k.key if hasattr(k, 'key') else k for k in keys}
        self.setup_pre(keys, scheduler, minimum, dt, complete)

        def clear_errors(errors):
            for k in errors:
                self.task_erred(None, k, None, True)

        # Get keys and all-keys
        if self.scheduler.loop._thread_ident == threading.current_thread().ident:
            errors = self.setup(keys, complete)
        else:
            errors = sync(self.scheduler.loop, self.setup, keys, complete)

        # Set up widgets
        from ipywidgets import FloatProgress, VBox, HTML, HBox
        self.bars = {key: FloatProgress(min=0, max=1, description=key)
                        for key in self.all_keys}
        self.texts = {key: HTML() for key in self.all_keys}
        self.boxes = {key: HBox([self.bars[key], self.texts[key]])
                        for key in self.all_keys}
        self.time = HTML()
        self.widget = HBox([self.time, VBox([self.boxes[key] for key in
                                            sorted(self.bars, key=str)])])

        from tornado.ioloop import IOLoop
        loop = IOLoop.instance()
        self.pc = PeriodicCallback(self._update, 1000 * self._dt, io_loop=loop)
        self.pc.start()

        # Clear out errors
        clear_errors(errors)
Beispiel #2
0
def test_widget_utils():
    box = HBox()
    i0 = IntText()
    i0._ngl_name = 'i0'
    i1 = IntText()
    i1._ngl_name = 'i1'
    box.children = [i0, i1]

    assert i0 is widget_utils.get_widget_by_name(box, 'i0')
    assert i1 is widget_utils.get_widget_by_name(box, 'i1')

    box.children = [i1, i0]
    assert i0 is widget_utils.get_widget_by_name(box, 'i0')
    assert i1 is widget_utils.get_widget_by_name(box, 'i1')

    assert widget_utils.get_widget_by_name(box, 'i100') is None
    assert widget_utils.get_widget_by_name(None, 'i100') is None
Beispiel #3
0
def _default_toolbar(figure):
    pz = panzoom(figure.marks)
    normal_btn = ToggleButton(icon='fa-circle-o', tooltip='Normal', value=True)
    pz_btn = ToggleButton(icon='fa-arrows', tooltip='Pan and Zoom', value=False)
    snapshot_btn = Button(icon='fa-thumb-tack', tooltip='Snapshot View')
    reset_btn = Button(icon='fa-refresh', tooltip='Reset View')
    save_btn = Button(icon='fa-save', tooltip='Save as .png Image')

    def tog(btn, *args):
        # Traitlets closure
        def cb():
            for other in args:
                other.value = not btn.value
        return cb

    def overl(btn, value):
        # Traitlets closure
        def cb():
            if btn.value:
                figure.interaction = value
        return cb

    def snapshot(_):
        pz.snapshot()

    def reset(_):
        pz.reset()

    def save(_):
        figure.save()

    pz_btn.on_trait_change(tog(pz_btn, normal_btn))
    pz_btn.on_trait_change(overl(pz_btn, pz))

    normal_btn.on_trait_change(tog(normal_btn, pz_btn))
    normal_btn.on_trait_change(overl(normal_btn, None))

    snapshot_btn.on_click(snapshot)
    reset_btn.on_click(reset)
    save_btn.on_click(save)
    figure.interaction = None

    button_group = HBox([normal_btn, pz_btn, snapshot_btn, reset_btn, save_btn])
    button_group._dom_classes = list(button_group._dom_classes) + ['btn-group']
    return button_group
Beispiel #4
0
class ProgressIPy(ProgressBase):  # pragma: no cover
    HTMLBOX = '<div class="widget-hbox widget-progress"><div class="widget-label" style="display:block;">{0}</div></div>'

    def __init__(self, *args, **kargs):

        # Ipython gives warnings when using widgets about the API potentially changing
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            try:
                from ipywidgets import IntProgress, HTML, HBox  # type: ignore
            except ImportError:  # Support IPython < 4.0
                from IPython.html.widgets import IntProgress, HTML, HBox  # type: ignore

        super(ProgressIPy, self).__init__(*args, **kargs)
        self.prog = IntProgress(max=self.length)
        self._label = HTML()
        self._box = HBox((self.prog, self._label))

    def start(self):
        from IPython.display import display  # type: ignore
        display(self._box)
        super(ProgressIPy, self).start()

    @property
    def value(self):
        """This is the current value, -1 allowed (automatically fixed for display)"""
        return self._value

    @value.setter
    def value(self, val):
        self._value = val
        self.prog.value = max(val, 0)
        self.prog.description = "{0:.2%}".format(self.value / self.length)
        if self.timer and val > 0:
            self._label.value = self.HTMLBOX.format(self.str_time_remaining())

    def display(self):
        pass

    def done(self):
        if self.clear:
            self._box.close()
Beispiel #5
0
    def _make_add_widget_repr(self, component_slider):
        dropdown_repr_name = Dropdown(options=REPRESENTATION_NAMES, value='cartoon')
        repr_selection = Text(value='*', description='')
        repr_button = Button(description='Add', tooltip="""Add representation.
        You can also hit Enter in selection box""")
        repr_button.layout = Layout(width='auto', flex='1 1 auto')

        dropdown_repr_name.layout.width = repr_selection.layout.width = default.DEFAULT_TEXT_WIDTH

        def on_click_or_submit(button_or_text_area):
            self._view.add_representation(selection=repr_selection.value.strip(),
                    repr_type=dropdown_repr_name.value,
                    component=component_slider.value)

        repr_button.on_click(on_click_or_submit)
        repr_selection.on_submit(on_click_or_submit)
        add_repr_box = HBox([repr_button, dropdown_repr_name, repr_selection])
        add_repr_box._ngl_name = 'add_repr_box'

        return add_repr_box
Beispiel #6
0
    def __init__(self, *args, **kargs):

        # Ipython gives warnings when using widgets about the API potentially changing
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            try:
                from ipywidgets import IntProgress, HTML, HBox
            except ImportError: # Support IPython < 4.0
                from IPython.html.widgets import IntProgress, HTML, HBox

        super(ProgressIPy, self).__init__(*args, **kargs)
        self.prog = IntProgress(max=self.length)
        self._label = HTML()
        self._box = HBox((self.prog, self._label))
Beispiel #7
0
class MultiProgressWidget(MultiProgress):
    """ Multiple progress bar Widget suitable for the notebook

    Displays multiple progress bars for a computation, split on computation
    type.

    See Also
    --------
    progress: User-level function <--- use this
    MultiProgress: Non-visualization component that contains most logic
    ProgressWidget: Single progress bar widget
    """
    def __init__(self, keys, scheduler=None, minimum=0, dt=0.1, func=key_split,
                 complete=False):
        self.func = func
        keys = {k.key if hasattr(k, 'key') else k for k in keys}
        self.setup_pre(keys, scheduler, minimum, dt, complete)

        def clear_errors(errors):
            for k in errors:
                self.task_erred(None, k, None, True)

        # Get keys and all-keys
        if self.scheduler.loop._thread_ident == threading.current_thread().ident:
            errors = self.setup(keys, complete)
        else:
            errors = sync(self.scheduler.loop, self.setup, keys, complete)

        # Set up widgets
        from ipywidgets import FloatProgress, VBox, HTML, HBox
        self.bars = {key: FloatProgress(min=0, max=1, description=key)
                        for key in self.all_keys}
        self.texts = {key: HTML() for key in self.all_keys}
        self.boxes = {key: HBox([self.bars[key], self.texts[key]])
                        for key in self.all_keys}
        self.time = HTML()
        self.widget = HBox([self.time, VBox([self.boxes[key] for key in
                                            sorted(self.bars, key=str)])])

        from tornado.ioloop import IOLoop
        loop = IOLoop.instance()
        self.pc = PeriodicCallback(self._update, 1000 * self._dt, io_loop=loop)
        self.pc.start()

        # Clear out errors
        clear_errors(errors)

    def _start(self):
        return self._update()

    def _ipython_display_(self, **kwargs):
        return self.widget._ipython_display_(**kwargs)

    def stop(self, exception=None, key=None):
        with ignoring(AttributeError):
            self.pc.stop()
        Progress.stop(self, exception, key)
        self._update()
        for k, v in self.keys.items():
            if not v:
                self.bars[k].bar_style = 'success'
        if exception:
            self.bars[self.func(key)].value = 1
            self.bars[self.func(key)].bar_style = 'danger'

    def _update(self):
        for k in self.all_keys:
            ntasks = len(self.all_keys[k])
            ndone = ntasks - len(self.keys[k])
            self.bars[k].value = ndone / ntasks if ntasks else 1.0
            self.texts[k].value = "%d / %d" % (ndone, ntasks)
            self.time.value = format_time(self.elapsed)