Skip to content

Commit

Permalink
Optional widget fields setter/getter
Browse files Browse the repository at this point in the history
  • Loading branch information
YooSunYoung committed Dec 11, 2024
1 parent 62bd310 commit 9288132
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/ess/reduce/widgets/_optional_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from ipywidgets import HTML, HBox, Layout, RadioButtons, Widget

from ._base import WidgetWithFieldsProtocol
from ._config import default_style


Expand Down Expand Up @@ -64,3 +65,25 @@ def value(self, value: Any) -> None:
else:
self._option_box.value = self.name
self.wrapped.value = value

def set_fields(self, new_values: Any) -> None:
# Set the value of the option box
if new_values is not None:
self._option_box.value = self.name
else:
self._option_box.value = None
# Set the value of the wrapped widget
if isinstance(self.wrapped, WidgetWithFieldsProtocol):
self.wrapped.set_fields(new_values)
elif new_values is not None:
self.wrapped.value = new_values
else:
... # Do not set the value of the wrapped widget

def get_fields(self) -> dict[str, Any] | None:
if self._option_box.value is None:
return None
elif isinstance(self.wrapped, WidgetWithFieldsProtocol):
return self.wrapped.get_fields()
else:
return self.wrapped.value
40 changes: 39 additions & 1 deletion tests/widget_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
import scipp as sc
from ipywidgets import FloatText, IntText

from ess.reduce.parameter import BinEdgesParameter, Parameter, parameter_registry
from ess.reduce.parameter import (
BinEdgesParameter,
Parameter,
Vector3dParameter,
parameter_registry,
)
from ess.reduce.ui import WorkflowWidget, workflow_widget
from ess.reduce.widgets import OptionalWidget, SwitchWidget, create_parameter_widget
from ess.reduce.widgets._base import WidgetWithFieldsProtocol
from ess.reduce.workflow import register_workflow, workflow_registry

SwitchableInt = NewType('SwitchableInt', int)
Expand Down Expand Up @@ -166,6 +172,38 @@ def test_switchable_optional_parameter_switchable_first() -> None:
assert isinstance(dummy_widget.wrapped, OptionalWidget)


def test_optional_widget_set_value_get_fields() -> None:
optional_param = Parameter('a', 'a', 1, optional=True)
optional_widget = create_parameter_widget(optional_param)
assert isinstance(optional_widget, WidgetWithFieldsProtocol)
assert isinstance(optional_widget, OptionalWidget)
# Check initial state
assert optional_widget._option_box.value is None
assert optional_widget.get_fields() is None
# Update the value of the wrapped widget
optional_widget.value = 'test'
# Check the fields
assert optional_widget.get_fields() == 'test'


def test_optional_widget_set_fields_get_fields() -> None:
optional_param = Vector3dParameter(
'a', 'a', sc.vector([1, 2, 3], unit='m'), optional=True
)
optional_widget = create_parameter_widget(optional_param)
assert isinstance(optional_widget, WidgetWithFieldsProtocol)
assert isinstance(optional_widget, OptionalWidget)
# Check initial state
assert optional_widget._option_box.value is None
assert optional_widget.get_fields() is None
# Update the value of the wrapped widget
optional_widget.set_fields({'x': 4, 'y': 5, 'z': 6, 'unit': 'm'})
assert optional_widget.value == sc.vector([4, 5, 6], unit='m')
# Check the fields and the option box value
assert optional_widget.get_fields() == {'x': 4, 'y': 5, 'z': 6, 'unit': 'm'}
assert optional_widget._option_box.value == optional_param.name


def test_optional_widget_dispatch() -> None:
optional_param = Parameter('a', 'a', 1, optional=True)
assert isinstance(create_parameter_widget(optional_param), OptionalWidget)
Expand Down

0 comments on commit 9288132

Please sign in to comment.