From 145f5c0c51d05421fd5bf0715aa635b389e80752 Mon Sep 17 00:00:00 2001 From: superstar54 Date: Mon, 20 Nov 2023 14:06:40 +0000 Subject: [PATCH 1/6] use `process_type` as the node viewer's key --- aiidalab_widgets_base/viewers.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/aiidalab_widgets_base/viewers.py b/aiidalab_widgets_base/viewers.py index 89192e446..1dae3ce62 100644 --- a/aiidalab_widgets_base/viewers.py +++ b/aiidalab_widgets_base/viewers.py @@ -47,9 +47,12 @@ def viewer(obj, **kwargs): f"This viewer works only with AiiDA objects, got {type(obj)}", stacklevel=2 ) return obj - - if obj.node_type in AIIDA_VIEWER_MAPPING: - _viewer = AIIDA_VIEWER_MAPPING[obj.node_type] + if isinstance(obj, orm.Data): + key = obj.node_type + else: + key = obj.process_type + if key in AIIDA_VIEWER_MAPPING: + _viewer = AIIDA_VIEWER_MAPPING[key] return _viewer(obj, **kwargs) else: # No viewer registered for this type, return object itself From a478890e690101efa2871b4f8728c1fe744e212e Mon Sep 17 00:00:00 2001 From: superstar54 Date: Mon, 20 Nov 2023 14:22:47 +0000 Subject: [PATCH 2/6] fix default process node viewer --- aiidalab_widgets_base/viewers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aiidalab_widgets_base/viewers.py b/aiidalab_widgets_base/viewers.py index 1dae3ce62..fbbfb7638 100644 --- a/aiidalab_widgets_base/viewers.py +++ b/aiidalab_widgets_base/viewers.py @@ -47,13 +47,13 @@ def viewer(obj, **kwargs): f"This viewer works only with AiiDA objects, got {type(obj)}", stacklevel=2 ) return obj - if isinstance(obj, orm.Data): - key = obj.node_type - else: - key = obj.process_type + + key = obj.process_type if isinstance(obj, orm.ProcessNode) else obj.node_type if key in AIIDA_VIEWER_MAPPING: _viewer = AIIDA_VIEWER_MAPPING[key] return _viewer(obj, **kwargs) + elif isinstance(obj, orm.ProcessNode): + return ProcessNodeViewerWidget(obj, **kwargs) else: # No viewer registered for this type, return object itself return obj From 74ef2a1a1c967d84907fbf69612fc655309ae9fc Mon Sep 17 00:00:00 2001 From: superstar54 Date: Mon, 20 Nov 2023 15:44:51 +0000 Subject: [PATCH 3/6] add backward capacity --- aiidalab_widgets_base/viewers.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aiidalab_widgets_base/viewers.py b/aiidalab_widgets_base/viewers.py index fbbfb7638..9b47a287d 100644 --- a/aiidalab_widgets_base/viewers.py +++ b/aiidalab_widgets_base/viewers.py @@ -52,8 +52,10 @@ def viewer(obj, **kwargs): if key in AIIDA_VIEWER_MAPPING: _viewer = AIIDA_VIEWER_MAPPING[key] return _viewer(obj, **kwargs) - elif isinstance(obj, orm.ProcessNode): - return ProcessNodeViewerWidget(obj, **kwargs) + # backward capacity + elif isinstance(obj, orm.ProcessNode) and obj.node_type in AIIDA_VIEWER_MAPPING: + _viewer = AIIDA_VIEWER_MAPPING[obj.node_type] + return _viewer(obj, **kwargs) else: # No viewer registered for this type, return object itself return obj From e5c883a0eb6e20ba8386d4c6ad1ca46127edbd28 Mon Sep 17 00:00:00 2001 From: superstar54 Date: Tue, 21 Nov 2023 15:47:00 +0000 Subject: [PATCH 4/6] fix test_computational_resources.py --- tests/test_computational_resources.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_computational_resources.py b/tests/test_computational_resources.py index 90c01708b..4b2fb8b45 100644 --- a/tests/test_computational_resources.py +++ b/tests/test_computational_resources.py @@ -511,7 +511,7 @@ def test_resource_setup_widget_default(): # and the computer/code setup widget will be updated accordingly. w.comp_resources_database.domain_selector.value = "daint.cscs.ch" w.comp_resources_database.computer_selector.value = "mc" - w.comp_resources_database.code_selector.value = "QuantumESPRESSO-7.2" + w.comp_resources_database.code_selector.value = "QuantumESPRESSO" # Test before the template is filled, the warning message is displayed. w._on_quick_setup() @@ -560,7 +560,7 @@ def test_resource_setup_widget_default(): w._on_quick_setup() assert w.success - assert orm.load_code("pw-7.2@daint-mc") + assert orm.load_code("pw-7.1@daint-mc") # test select new resource reset the widget, success trait, and message trait. w.reset() @@ -660,7 +660,7 @@ def test_resource_setup_widget_computer_change_code_reset(): # and the computer/code setup widget will be updated accordingly. w.comp_resources_database.domain_selector.value = "daint.cscs.ch" w.comp_resources_database.computer_selector.value = "mc" - w.comp_resources_database.code_selector.value = "QuantumESPRESSO-7.2" + w.comp_resources_database.code_selector.value = "QuantumESPRESSO" assert w.template_code._help_text.layout.display == "block" From ca9a17a3967f1b5b03d692dd95f6d9af4cc57091 Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Sun, 17 Mar 2024 14:19:03 +0000 Subject: [PATCH 5/6] Simplify logic --- aiidalab_widgets_base/viewers.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/aiidalab_widgets_base/viewers.py b/aiidalab_widgets_base/viewers.py index 68b2d9819..53850d3bf 100644 --- a/aiidalab_widgets_base/viewers.py +++ b/aiidalab_widgets_base/viewers.py @@ -47,13 +47,12 @@ def viewer(obj, **kwargs): ) return obj - key = obj.process_type if isinstance(obj, orm.ProcessNode) else obj.node_type - if key in AIIDA_VIEWER_MAPPING: - _viewer = AIIDA_VIEWER_MAPPING[key] - return _viewer(obj, **kwargs) - # backward capacity - elif isinstance(obj, orm.ProcessNode) and 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 From 763d84ec22dc0580fa8d982c5d269faf9d957fa8 Mon Sep 17 00:00:00 2001 From: superstar54 Date: Tue, 23 Apr 2024 15:21:48 +0000 Subject: [PATCH 6/6] Test loading a viewer widget based on the process type of the process node. --- tests/test_viewers.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_viewers.py b/tests/test_viewers.py index f1b81335e..1de4500e0 100644 --- a/tests/test_viewers.py +++ b/tests/test_viewers.py @@ -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."