Skip to content

Commit

Permalink
supports using process_type as the node viewer's key (#541)
Browse files Browse the repository at this point in the history
This PR 
* supports using `process_type` as the node viewer's key, thus allowing registering different viewers for different WorkChain
* Test loading a viewer widget based on the process type of the process node.

---------

Co-authored-by: Daniel Hollas <[email protected]>
  • Loading branch information
superstar54 and danielhollas authored Apr 23, 2024
1 parent 70259cc commit 67df249
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions aiidalab_widgets_base/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ def viewer(obj, **kwargs):
)
return obj

if obj.node_type in AIIDA_VIEWER_MAPPING:
_viewer = AIIDA_VIEWER_MAPPING[obj.node_type]
_viewer = AIIDA_VIEWER_MAPPING.get(obj.node_type)
if isinstance(obj, orm.ProcessNode):
# Allow to register specific viewers based on obj.process_type
_viewer = AIIDA_VIEWER_MAPPING.get(obj.process_type, _viewer)

if _viewer:
return _viewer(obj, **kwargs)
else:
# No viewer registered for this type, return object itself
Expand Down
22 changes: 22 additions & 0 deletions tests/test_viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,25 @@ def test_compute_bonds_in_structure_data_viewer():
viewer = viewers.StructureDataViewer()
bonds = viewer._compute_bonds(water)
assert len(bonds) == 4


@pytest.mark.usefixtures("aiida_profile_clean")
def test_loading_viewer_using_process_type(generate_calc_job_node):
"""Test loading a viewer widget based on the process type of the process node."""
from aiidalab_widgets_base import register_viewer_widget

# Define and register a viewer widget for the calculation type identified by "aiida.calculations:abc".
@register_viewer_widget("aiida.calculations:abc")
class AbcViewer:
def __init__(self, node=None):
self.node = node

# Generate a calc job node with the specific entry point "abc".
process = generate_calc_job_node(entry_point_name="abc")
# Load the viewer widget for the generated process node.
viewer = viewers.viewer(process)
# Verify that the loaded viewer is the correct type and is associated with the intended node.
assert isinstance(
viewer, AbcViewer
), "Viewer is not an instance of the expected viewer class."
assert viewer.node == process, "Viewer's node does not match the test process node."

0 comments on commit 67df249

Please sign in to comment.