-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from scipp/inherit_from_widget
Inherit from widget to make toolbar, figures and widgets embedabble inside other widgets
- Loading branch information
Showing
14 changed files
with
106 additions
and
211 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp) | ||
|
||
from ipywidgets import VBox, HBox | ||
|
||
|
||
class Box(VBox): | ||
""" | ||
Container widget that accepts a list of items. For each item in the list, if the | ||
item is itself a list, it will be bade into a horizontal row of the underlying | ||
items, if not, the item will span then entire row. | ||
Finally, all the rows will be placed inside a vertical box container. | ||
""" | ||
|
||
def __init__(self, widgets): | ||
self.widgets = widgets | ||
children = [] | ||
for view in self.widgets: | ||
children.append(HBox(view) if isinstance(view, (list, tuple)) else view) | ||
return super().__init__(children) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,52 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp) | ||
|
||
from functools import partial | ||
from html import escape | ||
import ipywidgets as ipw | ||
from typing import Callable | ||
from ..displayable import Displayable | ||
|
||
|
||
class Checkboxes(Displayable): | ||
class Checkboxes(ipw.HBox): | ||
""" | ||
Widget providing a list of checkboxes, along with a button to toggle them all. | ||
""" | ||
|
||
def __init__(self, entries: list, description=""): | ||
|
||
self._callback = None | ||
|
||
self.checkboxes = {} | ||
self._lock = False | ||
self._description = ipw.Label(value=description) | ||
|
||
for key in entries: | ||
self.checkboxes[key] = ipw.Checkbox(value=True, | ||
description=f"{escape(key)}", | ||
indent=False, | ||
layout={"width": "initial"}) | ||
|
||
if len(self.checkboxes): | ||
self._description = ipw.Label(value=description) | ||
to_hbox = [ | ||
self._description, | ||
ipw.HBox(list(self.checkboxes.values()), | ||
layout=ipw.Layout(flex_flow='row wrap')) | ||
] | ||
|
||
if len(self.checkboxes): | ||
# Add a master button to control all masks in one go | ||
self.toggle_all_button = ipw.ToggleButton(value=True, | ||
description="De-select all", | ||
disabled=False, | ||
button_style="", | ||
layout={"width": "100px"}) | ||
layout={"width": "initial"}) | ||
for cbox in self.checkboxes.values(): | ||
ipw.jsdlink((self.toggle_all_button, 'value'), (cbox, 'value')) | ||
to_hbox.insert(1, self.toggle_all_button) | ||
|
||
def to_widget(self) -> ipw.Widget: | ||
""" | ||
Gather all widgets in a single container box. | ||
""" | ||
out = ipw.HBox() | ||
if len(self.checkboxes): | ||
out.children = [ | ||
self._description, self.toggle_all_button, | ||
ipw.HBox(list(self.checkboxes.values())) | ||
] | ||
return out | ||
super().__init__(to_hbox) | ||
|
||
def observe(self, callback: Callable, **kwargs): | ||
def _plopp_observe_(self, callback: Callable, **kwargs): | ||
for chbx in self.checkboxes.values(): | ||
chbx.observe(self._toggle, **kwargs) | ||
if len(self.checkboxes): | ||
self.toggle_all_button.observe(self._toggle_all, **kwargs) | ||
self._callback = partial(callback, None) | ||
chbx.observe(callback, **kwargs) | ||
|
||
@property | ||
def value(self) -> dict: | ||
""" | ||
""" | ||
return {key: chbx.value for key, chbx in self.checkboxes.items()} | ||
|
||
def _toggle(self, _): | ||
if self._lock: | ||
return | ||
self._callback() | ||
|
||
def _toggle_all(self, change: dict): | ||
""" | ||
A main button to hide or show all masks at once. | ||
""" | ||
self._lock = True | ||
for key in self.checkboxes: | ||
self.checkboxes[key].value = change["new"] | ||
change["owner"].description = ("De-select all" | ||
if change["new"] else "Select all") | ||
self._lock = False | ||
self._callback() |
Oops, something went wrong.