Skip to content

Commit 695ef92

Browse files
Wrap structure importers in accordion panel (#683)
Following #666, I make structure selection consistent with viewing and editing, as well as guard against no importers following #681.
1 parent 5e8f18d commit 695ef92

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

aiidalab_widgets_base/structures.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,13 @@ def __init__(
114114
f"Unknown data format '{node_class}'. Options: {list(self.SUPPORTED_DATA_FORMATS.keys())}"
115115
)
116116

117-
viewer_panel = ipw.Accordion(
117+
select_panel = ipw.Accordion(
118+
children=self._structure_importers(importers),
119+
selected_index=0 if importers else None,
120+
)
121+
select_panel.set_title(0, "Select structure")
122+
123+
view_panel = ipw.Accordion(
118124
children=[
119125
ipw.VBox(
120126
children=[
@@ -131,33 +137,32 @@ def __init__(
131137
],
132138
selected_index=0,
133139
)
134-
viewer_panel.set_title(0, "View structure")
140+
view_panel.set_title(0, "View structure")
135141

136142
structure_editors = self._structure_editors(editors)
137143

138-
if structure_editors:
139-
structure_editors = (
144+
edit_panel = ipw.Accordion(
145+
children=[
140146
ipw.VBox(
141147
children=[
142148
btn_undo,
143149
*structure_editors,
144150
]
145151
),
146-
)
147-
148-
editor_panel = ipw.Accordion(
149-
children=structure_editors,
152+
]
153+
if structure_editors
154+
else [],
150155
selected_index=None,
151156
)
152-
editor_panel.set_title(0, "Edit structure")
157+
edit_panel.set_title(0, "Edit structure")
153158

154159
self.output = ipw.HTML("")
155160

156161
super().__init__(
157162
children=[
158-
self._structure_importers(importers),
159-
viewer_panel,
160-
editor_panel,
163+
select_panel,
164+
view_panel,
165+
edit_panel,
161166
self.output,
162167
],
163168
**kwargs,
@@ -170,11 +175,8 @@ def _structure_importers(self, importers):
170175
if not isinstance(importers, (list, tuple)):
171176
raise exceptions.ListOrTuppleError(importers)
172177

173-
# If there is only one importer - no need to make tabs.
174-
if len(importers) == 1:
175-
# Assigning a function which will be called when importer provides a structure.
176-
tl.dlink((importers[0], "structure"), (self, "input_structure"))
177-
return importers[0]
178+
if not importers:
179+
return []
178180

179181
# Otherwise making one tab per importer.
180182
importers_tab = ipw.Tab()
@@ -183,19 +185,18 @@ def _structure_importers(self, importers):
183185
# Labeling tabs.
184186
importers_tab.set_title(i, importer.title)
185187
tl.dlink((importer, "structure"), (self, "input_structure"))
186-
return importers_tab
188+
return [importers_tab]
187189

188190
def _structure_editors(self, editors):
189191
"""Preparing structure editors."""
190-
191-
# Link selected traits of the editors with the those of the viewer.
192-
if not len(editors):
192+
if not editors:
193193
return []
194194

195195
editors_tab = ipw.Tab()
196196
editors_tab.children = tuple(editors)
197197
for i, editor in enumerate(editors):
198198
editors_tab.set_title(i, editor.title)
199+
# Link selected traits of the editors with the those of the viewer.
199200
tl.link((editor, "structure"), (self, "structure"))
200201
if editor.has_trait("input_selection"):
201202
tl.dlink((editor, "input_selection"), (self.viewer, "input_selection"))

0 commit comments

Comments
 (0)