Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement caching in AiidaNodeViewWidget #686

Merged
merged 3 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions aiidalab_widgets_base/static/styles/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.aiida-node-view-widget {
border: var(--jp-widgets-border-width) solid var(--jp-border-color1);
padding: 12px;
margin: 2px;
height: auto;
width: auto;
}
26 changes: 18 additions & 8 deletions aiidalab_widgets_base/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from matplotlib.colors import to_rgb

from .dicts import RGB_COLORS, Colors, Radius
from .loaders import LoadingWidget
from .misc import CopyToClipboardButton, ReversePolishNotation
from .utils import ase2spglib, list_to_string_range, string_range_to_list

Expand Down Expand Up @@ -67,20 +68,29 @@ class AiidaNodeViewWidget(ipw.VBox):

def __init__(self, **kwargs):
self._output = ipw.Output()
super().__init__(
children=[
self._output,
],
**kwargs,
)
self.node_views = {}
self.node_view_loading_message = LoadingWidget("Loading node view")
super().__init__(**kwargs)
self.add_class("aiida-node-view-widget")

@tl.observe("node")
def _observe_node(self, change):
if change["new"] != change["old"]:
if not ((node := change["new"]) and node != change["old"]):
return
if node.uuid in self.node_views:
self.children = [self.node_views[node.uuid]]
return
self.children = [self.node_view_loading_message]
node_view = viewer(node)
if isinstance(node_view, ipw.DOMWidget):
self.node_views[node.uuid] = node_view
self.children = [node_view]
else:
with self._output:
clear_output()
if change["new"]:
display(viewer(change["new"]))
display(node_view)
self.children = [self._output]


@register_viewer_widget("data.core.dict.Dict.")
Expand Down
10 changes: 10 additions & 0 deletions tests/test_loaders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path

from aiidalab_widgets_base.loaders import LoadingWidget
from aiidalab_widgets_base.utils.loaders import load_css


Expand All @@ -8,3 +9,12 @@ def test_load_css():
css_dir = Path("aiidalab_widgets_base/static/styles")
load_css(css_path=css_dir)
load_css(css_path=css_dir / "global.css")


def test_loading_widget():
"""Test `LoadingWidget`."""
widget = LoadingWidget(message="Loading some widget")
assert widget.message.value == "Loading some widget"
assert widget.children[0].value == "Loading some widget"
assert widget.children[1].value == "<i class='fa fa-spinner fa-spin fa-2x fa-fw'/>"
assert "loading" in widget._dom_classes
28 changes: 28 additions & 0 deletions tests/test_viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,31 @@ def __init__(self, node=None):
"Viewer is not an instance of the expected viewer class."
)
assert viewer.node == process, "Viewer's node does not match the test process node."


def test_node_view_for_non_widget_viewer():
"""Test that a node with no registered viewer is displayed in an output widget"""
import sys
from io import StringIO

# Intercepting stdout because `ipw.Output` does not
# store outputs in non-interactive environments.
captured = StringIO()
sys.stdout = captured

node_view = viewers.AiidaNodeViewWidget()
node = orm.Int(1)
node_view.node = node
assert node_view.children[0] is node_view._output
assert str(node) in sys.stdout.getvalue()


def test_node_view_caching():
"""Test that providing a given node a second time returns the cached viewer."""
node_view = viewers.AiidaNodeViewWidget()
node = orm.Int(1)
node_view.node = node
viewer = node_view.children[0]
node_view.node = None
node_view.node = node
assert node_view.children[0] is viewer
Loading