-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
70 lines (55 loc) · 1.97 KB
/
stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import aiidalab_widgets_base as awb
import ipywidgets as ipw
import traitlets as tl
class HorizontalItemWidget(ipw.HBox):
stack_class = None
def __init__(self, *args, **kwargs):
# Delete button.
self.delete_button = ipw.Button(
description="x", button_style="danger", layout={"width": "30px"}
)
self.delete_button.on_click(self.delete_myself)
children = kwargs.pop("children", [])
children.append(self.delete_button)
super().__init__(children=children, *args, **kwargs)
def delete_myself(self, _):
self.stack_class.delete_item(self)
class VerticalStackWidget(ipw.VBox):
items = tl.Tuple()
item_class = None
def __init__(self, item_class, mode, add_button_text="Add"):
self.item_class = item_class
self.mode = mode
self.add_item_button = ipw.Button(description=add_button_text, button_style="")
self.add_item_button.on_click(self.add_item)
self.items_output = ipw.VBox()
tl.link((self, "items"), (self.items_output, "children"))
# Outputs
self.add_item_message = awb.utils.StatusHTML()
super().__init__(
children=[
self.items_output,
self.add_item_button,
self.add_item_message,
]
)
def add_item(self, _):
self.items += (self.item_class(),)
@tl.observe("items")
def _observe_fragments(self, change):
"""Update the list of fragments."""
if change["new"]:
self.items_output.children = change["new"]
for item in change["new"]:
item.stack_class = self
else:
self.items_output.children = []
def delete_item(self, item):
try:
index = self.items.index(item)
except ValueError:
return
self.items = self.items[:index] + self.items[index + 1 :]
del item
def length(self):
return len(self.items)