diff --git a/aiidalab_widgets_base/computational_resources.py b/aiidalab_widgets_base/computational_resources.py index 7adde22dd..c52a2c6ce 100644 --- a/aiidalab_widgets_base/computational_resources.py +++ b/aiidalab_widgets_base/computational_resources.py @@ -61,6 +61,8 @@ def __init__( enable_detailed_setup=True, clear_after=None, default_calc_job_plugin=None, + include_setup_widget=True, + fetch_codes=True, **kwargs, ): """Dropdown for Codes for one input plugin. @@ -73,7 +75,7 @@ def __init__( self.setup_message = StatusHTML(clear_after=clear_after) self.code_select_dropdown = ipw.Dropdown( description=description, - disabled=True, + disabled=fetch_codes, # disable if handled internally, enable otherwise value=None, style={"description_width": "initial"}, ) @@ -95,33 +97,45 @@ def __init__( self.refresh, names=["allow_disabled_computers", "allow_hidden_codes"] ) - self.btn_setup_new_code = ipw.ToggleButton(description="Setup new code") - self.btn_setup_new_code.observe(self._setup_new_code, "value") + self._default_user_email = orm.User.collection.get_default().email - self._setup_new_code_output = ipw.Output(layout={"width": self._output_width}) + selection_row = ipw.HBox( + children=[ + self.code_select_dropdown, + ] + ) - self._default_user_email = orm.User.collection.get_default().email + children = [selection_row] - children = [ - ipw.HBox([self.code_select_dropdown, self.btn_setup_new_code]), - self._setup_new_code_output, - self.output, - ] - super().__init__(children=children, **kwargs) + if include_setup_widget: + self.btn_setup_new_code = ipw.ToggleButton(description="Setup new code") + self.btn_setup_new_code.observe(self._setup_new_code, "value") - # Computer/code setup - self.resource_setup = _ResourceSetupBaseWidget( - default_calc_job_plugin=self.default_calc_job_plugin, - enable_quick_setup=enable_quick_setup, - enable_detailed_setup=enable_detailed_setup, - ) - self.resource_setup.observe(self.refresh, "success") - tl.dlink( - (self.resource_setup, "message"), - (self.setup_message, "message"), - ) + selection_row.children += (self.btn_setup_new_code,) - self.refresh() + self._setup_new_code_output = ipw.Output( + layout={"width": self._output_width} + ) + children.append(self._setup_new_code_output) + + # Computer/code setup + self.resource_setup = _ResourceSetupBaseWidget( + default_calc_job_plugin=self.default_calc_job_plugin, + enable_quick_setup=enable_quick_setup, + enable_detailed_setup=enable_detailed_setup, + ) + self.resource_setup.observe(self.refresh, "success") + tl.dlink( + (self.resource_setup, "message"), + (self.setup_message, "message"), + ) + + children.append(self.output) + + super().__init__(children=children, **kwargs) + + if fetch_codes: + self.refresh() def _get_codes(self): """Query the list of available codes.""" diff --git a/tests/conftest.py b/tests/conftest.py index 4a81ecdde..edea9b809 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -336,3 +336,12 @@ def get(self): return json.load(f) return _MockElnConfig() + + +@pytest.fixture +def pw_code(aiida_local_code_factory): + """Return a `Code` configured for the pw.x executable.""" + + return aiida_local_code_factory( + label="pw", executable="bash", entry_point="quantumespresso.pw" + ) diff --git a/tests/test_computational_resources.py b/tests/test_computational_resources.py index db0caa3b7..87a55fd6a 100644 --- a/tests/test_computational_resources.py +++ b/tests/test_computational_resources.py @@ -802,3 +802,21 @@ def test_computer_resource_setup_widget_default(monkeypatch, tmp_path): content = f.read() assert "User aiida" in content assert "Host merlin-l-01.psi.ch" in content + + +@pytest.mark.usefixtures("aiida_profile_clean") +def test_optional_new_code_setup_widget(): + widget = ComputationalResourcesWidget(include_setup_widget=False) + assert not hasattr(widget, "btn_setup_new_code") + assert not hasattr(widget, "_setup_new_code_output") + assert not hasattr(widget, "resource_setup") + selection_row = widget.children[0] # type: ignore + assert len(selection_row.children) == 1 # no new code setup button + + +@pytest.mark.usefixtures("aiida_profile_clean") +def test_optional_code_fetching(pw_code): + widget = ComputationalResourcesWidget(fetch_codes=True) + assert len(widget.code_select_dropdown.options) != 0 + widget = ComputationalResourcesWidget(fetch_codes=False) + assert len(widget.code_select_dropdown.options) == 0