Skip to content

Commit

Permalink
Update view/controller observables
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Oct 6, 2024
1 parent e3532b2 commit 7d26bf6
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 167 deletions.
25 changes: 9 additions & 16 deletions src/aiidalab_qe/app/configuration/hubbard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import ipywidgets as ipw
import traitlets as tl

from aiida import orm
from aiida_quantumespresso.data.hubbard_structure import HubbardStructureData

from .model import ConfigurationModel
Expand All @@ -10,14 +8,6 @@
class HubbardSettings(ipw.VBox):
"""Widget for setting up Hubbard parameters."""

input_structure = tl.Union(
[
tl.Instance(orm.StructureData),
tl.Instance(orm.KpointsData),
],
allow_none=True,
)

def __init__(self, model: ConfigurationModel, **kwargs):
from aiidalab_qe.common.widgets import LoadingWidget

Expand All @@ -27,6 +17,10 @@ def __init__(self, model: ConfigurationModel, **kwargs):
)

self._model = model
self._model.observe(
self._on_input_structure_change,
"input_structure",
)

self.links = []
self.eigenvalues_widget_links = []
Expand Down Expand Up @@ -78,6 +72,7 @@ def render(self):

self.hubbard_widget = ipw.VBox()
self.eigenvalues_widget = ipw.VBox()

self.container = ipw.VBox()

self.children = [
Expand All @@ -90,19 +85,15 @@ def render(self):
self.container,
]

ipw.dlink(
(self._model, "input_structure"),
(self, "input_structure"),
)

self.rendered = True

self._build_hubbard_widget()

def reset(self):
"""Reset the widget."""
self._unsubscribe()
self._model.advanced.hubbard.reset()

@tl.observe("input_structure")
def _on_input_structure_change(self, change):
self._unsubscribe()
self._model.advanced.hubbard.update()
Expand All @@ -124,6 +115,8 @@ def _build_hubbard_widget(self):
hubbard_widget (ipywidgets.VBox):
The widget containing the input fields for defining Hubbard U values.
"""
if not self.rendered:
return

children = []

Expand Down
65 changes: 26 additions & 39 deletions src/aiidalab_qe/app/configuration/magnetization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import ipywidgets as ipw
import traitlets as tl

from aiida import orm

from .model import ConfigurationModel

Expand All @@ -20,16 +17,6 @@ class MagnetizationSettings(ipw.VBox):
input_structure(StructureData): trait that contains the input_structure (confirmed structure from previous step)
"""

input_structure = tl.Union(
[
tl.Instance(orm.StructureData),
tl.Instance(orm.KpointsData),
],
allow_none=True,
)
electronic_type = tl.Unicode()
magnetization_type = tl.Unicode()

def __init__(self, model: ConfigurationModel, **kwargs):
from aiidalab_qe.common.widgets import LoadingWidget

Expand All @@ -40,8 +27,20 @@ def __init__(self, model: ConfigurationModel, **kwargs):
)

self._model = model
self._model.observe(
self._on_input_structure_change,
"input_structure",
)
self._model.workchain.observe(
self._on_electronic_type_change,
"electronic_type",
)
self._model.advanced.magnetization.observe(
self._on_magnetization_type_change,
"type",
)

self.kind_widget_links = []
self.links = []

self.rendered = False

Expand Down Expand Up @@ -96,48 +95,36 @@ def render(self):

self.children = [self.description]

with self.hold_trait_notifications():
ipw.dlink(
(self._model, "input_structure"),
(self, "input_structure"),
)
ipw.dlink(
(self._model.workchain, "electronic_type"),
(self, "electronic_type"),
)
ipw.dlink(
(self._model.advanced.magnetization, "type"),
(self, "magnetization_type"),
)

self.rendered = True

self._build_kinds_widget()

def reset(self):
self._model.advanced.magnetization.reset()

@tl.observe("input_structure")
def _on_input_structure_change(self, change):
def _on_input_structure_change(self, _):
self._model.advanced.magnetization.update()
self._build_kinds_widget(change)
self._build_kinds_widget()

@tl.observe("electronic_type")
def _on_electronic_type_change(self, change):
self._switch_widgets(change)

@tl.observe("magnetization_type")
def _on_magnetization_type_change(self, change):
self._toggle_widgets(change)

def _build_kinds_widget(self, change):
def _build_kinds_widget(self):
if not self.rendered:
return

children = []

if (input_structure := change["new"]) is None:
if self._model.input_structure is None:
labels = []
for link in self.kind_widget_links:
for link in self.links:
link.unlink()
self.kind_widget_links.clear()
self.links.clear()
else:
labels = input_structure.get_kind_names()
labels = self._model.input_structure.get_kind_names()

for label in labels:
kind_widget = ipw.BoundedFloatText(
Expand All @@ -158,7 +145,7 @@ def _build_kinds_widget(self, change):
},
],
)
self.kind_widget_links.append(link)
self.links.append(link)
ipw.dlink(
(self._model.advanced, "override"),
(kind_widget, "disabled"),
Expand Down
5 changes: 1 addition & 4 deletions src/aiidalab_qe/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,15 @@ def __init__(self, qe_auto_setup=True):
model=struct_model,
auto_advance=True,
)

self.configure_step = ConfigureQeAppWorkChainStep(
model=config_model,
auto_advance=True,
)

self.submit_step = SubmitQeAppWorkChainStep(
model=submit_model,
auto_advance=True,
qe_auto_setup=qe_auto_setup,
)

self.results_step = ViewQeAppWorkChainStatusAndResultsStep(model=results_model)

# Link the models of the application steps
Expand Down Expand Up @@ -105,7 +102,7 @@ def __init__(self, qe_auto_setup=True):
)

ipw.dlink(
(self.submit_step, "process"),
(submit_model, "process"),
(self.work_chain_selector, "value"),
transform=lambda node: None if node is None else node.pk,
)
Expand Down
19 changes: 6 additions & 13 deletions src/aiidalab_qe/app/result/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@


class ViewQeAppWorkChainStatusAndResultsStep(ipw.VBox, WizardAppWidgetStep):
process = tl.Unicode(allow_none=True)

def __init__(self, model: ResultsModel, **kwargs):
from aiidalab_qe.common.widgets import LoadingWidget

Expand Down Expand Up @@ -107,11 +105,6 @@ def render(self):
"process",
)

ipw.dlink(
(self._model, "process"),
(self, "process"),
)

self.rendered = True

def can_reset(self):
Expand All @@ -129,14 +122,14 @@ def _on_process_change(self, _):
self._update_kill_button_layout()

def _on_click_kill_button(self, _=None):
workchain = [orm.load_node(self.process)]
workchain = [orm.load_node(self._model.process)]
control.kill_processes(workchain)
self._update_kill_button_layout()

def _update_kill_button_layout(self):
if (
self.process is None
or self.process == ""
self._model.process is None
or self._model.process == ""
or self.state
in (
self.State.SUCCESS,
Expand All @@ -145,17 +138,17 @@ def _update_kill_button_layout(self):
):
self.kill_button.layout.display = "none"
else:
process = orm.load_node(self.process)
process = orm.load_node(self._model.process)
if process.is_finished or process.is_excepted:
self.kill_button.layout.display = "none"
else:
self.kill_button.layout.display = "block"

def _update_state(self):
if self.process is None:
if self._model.process is None:
self.state = self.State.INIT
else:
process = orm.load_node(self.process)
process = orm.load_node(self._model.process)
process_state = process.process_state
if process_state in (
ProcessState.CREATED,
Expand Down
28 changes: 11 additions & 17 deletions src/aiidalab_qe/app/structure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pathlib

import ipywidgets as ipw
import traitlets as tl

from aiida import orm
from aiida_quantumespresso.data.hubbard_structure import HubbardStructureData
Expand Down Expand Up @@ -48,8 +47,6 @@ class StructureSelectionStep(ipw.VBox, WizardAppWidgetStep):
structure importers and the structure editors can be extended by plugins.
"""

structure = tl.Instance(orm.StructureData, allow_none=True)

def __init__(self, model: StructureModel, **kwargs):
from aiidalab_qe.common.widgets import LoadingWidget

Expand All @@ -63,6 +60,10 @@ def __init__(self, model: StructureModel, **kwargs):
self._on_confirmation_change,
"confirmed",
)
self._model.observe(
self._on_structure_change,
"structure",
)

self.rendered = False

Expand Down Expand Up @@ -135,13 +136,12 @@ def render(self):
lambda state: state != self.State.CONFIGURED,
)
self.confirm_button.on_click(self.confirm)

self.message_area = ipw.HTML()

# Create directional link from the (read-only) 'structure_node' traitlet of the
# structure manager to our 'structure' traitlet:
ipw.dlink(
(self.manager, "structure_node"),
(self, "structure"),
(self._model, "structure"),
)

self.children = [
Expand All @@ -163,23 +163,18 @@ def render(self):
self.confirm_button,
]

ipw.dlink(
(self._model, "structure"),
(self, "structure"),
)

self.rendered = True

def set_structure(self, structure):
self._model.structure = structure

def is_saved(self):
"""Check if the current structure is confirmed."""
return self.structure == self._model.confirmed_structure
return self._model.structure == self._model.confirmed_structure

def confirm(self, _=None):
self.manager.store_structure()
self._model.confirmed_structure = self.structure
self._model.confirmed_structure = self._model.structure
self._model.confirmed = True
self.message_area.value = ""

Expand All @@ -194,7 +189,6 @@ def reset(self):
self.manager.viewer.structure = None
self.manager.output.value = ""

@tl.observe("structure")
def _on_structure_change(self, _):
self._model.reset()
self._update_widget_text()
Expand All @@ -204,17 +198,17 @@ def _on_confirmation_change(self, _):
self._update_state()

def _update_widget_text(self):
if self.structure is None:
if self._model.structure is None:
self.structure_name_text.value = ""
self.message_area.value = ""
else:
self.manager.output.value = ""
self.structure_name_text.value = str(self.structure.get_formula())
self.structure_name_text.value = str(self._model.structure.get_formula())

def _update_state(self):
if self._model.confirmed:
self.state = self.State.SUCCESS
elif self.structure is None:
elif self._model.structure is None:
self.state = self.State.READY
else:
self.state = self.State.CONFIGURED
Loading

0 comments on commit 7d26bf6

Please sign in to comment.