Skip to content

Commit

Permalink
Store already added node viewer and load it when needed (#921)
Browse files Browse the repository at this point in the history
In aiidalab-widgets-base/viewer.py, it creates a new viewer every time we select a new AiiDA node, and the viewer is not saved in the app, so we can not reuse it later. This PR stores the already added viewer and reuses it later.
  • Loading branch information
superstar54 authored Nov 11, 2024
1 parent 2924582 commit 5a55a5c
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/aiidalab_qe/app/result/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from aiida.engine import ProcessState
from aiida.engine.processes import control
from aiidalab_widgets_base import (
AiidaNodeViewWidget,
ProcessMonitor,
ProcessNodesTreeWidget,
WizardAppWidgetStep,
Expand All @@ -28,14 +27,10 @@ def __init__(self, **kwargs):
(self, "process"),
(self.process_tree, "value"),
)

self.node_view = AiidaNodeViewWidget(layout={"width": "auto", "height": "auto"})
ipw.dlink(
(self.process_tree, "selected_nodes"),
(self.node_view, "node"),
transform=lambda nodes: nodes[0] if nodes else None,
)
self.process_status = ipw.VBox(children=[self.process_tree, self.node_view])
self.process_tree.observe(self._update_node_view, names="selected_nodes")
# keep track of the node views
self.node_views = {}
self.process_status = ipw.VBox(children=[self.process_tree])

# Setup process monitor
self.process_monitor = ProcessMonitor(
Expand Down Expand Up @@ -215,3 +210,20 @@ def _observe_process(self, _):
self._update_state()
self._update_kill_button_layout()
self._update_clean_scratch_button_layout()

def _update_node_view(self, change):
"""Callback for when the a new node is selected."""
from aiidalab_widgets_base.viewers import viewer

nodes = change["new"]
if not nodes:
return
# only show the first selected node
node = nodes[0]
# check if the viewer is already added
if node.uuid in self.node_views:
node_view = self.node_views[node.uuid]
else:
node_view = viewer(node)
self.node_views[node.uuid] = node_view
self.process_status.children = [self.process_tree, node_view]

0 comments on commit 5a55a5c

Please sign in to comment.