Skip to content

Commit

Permalink
Make the setup new codes widget part of the code selection widget opt…
Browse files Browse the repository at this point in the history
…ional (#646)

This PR provides a backwards compatible path to make the new code setup widget part of the code selection widget optional.

The PR also adds a flag (true by default) to control if the codes should be pre-fetched. This is useful, for example, in the QE app, where the MVC redesign (#802) provides these codes via models, removing the responsibility from the UI widgets.
  • Loading branch information
edan-bainglass authored Dec 9, 2024
1 parent 2677368 commit fd8c1b0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
60 changes: 37 additions & 23 deletions aiidalab_widgets_base/computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"},
)
Expand All @@ -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."""
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
18 changes: 18 additions & 0 deletions tests/test_computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fd8c1b0

Please sign in to comment.