Skip to content

Commit

Permalink
Rename parameter panel (#106)
Browse files Browse the repository at this point in the history
* Rename parameter panel class and property

`parameter_panel` to `parameters_panel`
`ParameterPanel` to `ParametersPanel`

* Rename parameter panel file

`_widget_parameter_panel.py` to `_widget_parameters_panel.py`
  • Loading branch information
agoscinski authored Dec 19, 2024
1 parent d9f3a35 commit 33a4b31
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/scwidgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"CueFigure",
# code
"CodeInput",
"ParameterPanel",
"ParametersPanel",
# check
"CheckRegistry",
"assert_equal",
Expand Down
4 changes: 2 additions & 2 deletions src/scwidgets/code/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ._widget_code_input import CodeInput
from ._widget_parameter_panel import ParameterPanel
from ._widget_parameters_panel import ParametersPanel

__all__ = [
"CodeInput",
"ParameterPanel",
"ParametersPanel",
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ..check import Check


class ParameterPanel(VBox):
class ParametersPanel(VBox):
"""
A wrapper around ipywidgets.interactive to have more control how to connect the
parameters and the observation of parameters by buttons and the panels
Expand All @@ -25,7 +25,7 @@ def __init__(
if "_option" in parameters.keys():
raise ValueError(
"Found interactive argument `_option` in paramaters, but "
"ParameterPanels should be controled by an exercise widget "
"ParametersPanels should be controled by an exercise widget "
"to ensure correct initialization."
)

Expand Down
99 changes: 52 additions & 47 deletions src/scwidgets/exercise/_widget_code_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .._utils import Formatter
from ..check import Check, CheckableWidget, CheckRegistry, CheckResult
from ..code._widget_code_input import CodeInput
from ..code._widget_parameter_panel import ParameterPanel
from ..code._widget_parameters_panel import ParametersPanel
from ..css_style import CssStyle
from ..cue import (
CheckCueBox,
Expand Down Expand Up @@ -43,8 +43,8 @@ class CodeExercise(VBox, CheckableWidget, ExerciseWidget):
a check registry that is used to register checks
:param parameters:
Input parameters for the :py:class:`ParameterPanel` class or an initialized
:py:class:`ParameterPanel` object. Specifies the arguments in the parameter
Input parameters for the :py:class:`ParametersPanel` class or an initialized
:py:class:`ParametersPanel` object. Specifies the arguments in the parameter
panel.
:param update_mode:
Expand All @@ -67,7 +67,7 @@ def __init__(
exercise_registry: Optional[ExerciseRegistry] = None,
exercise_key: Optional[str] = None,
parameters: Optional[
Union[Dict[str, Union[Check.FunInParamT, Widget]], ParameterPanel]
Union[Dict[str, Union[Check.FunInParamT, Widget]], ParametersPanel]
] = None,
update_mode: str = "manual",
outputs: Union[None, Figure, CueOutput, List[CueOutput]] = None,
Expand Down Expand Up @@ -140,7 +140,7 @@ def __init__(

# verify if input argument `parameter` is valid
if parameters is not None:
allowed_parameter_types = [dict, ParameterPanel]
allowed_parameter_types = [dict, ParametersPanel]
parameter_type_allowed = False
for allowed_parameter_type in allowed_parameter_types:
if isinstance(parameters, allowed_parameter_type):
Expand All @@ -167,7 +167,7 @@ def __init__(
compatibility_result = code.compatible_with_signature(
list(parameters.keys())
)
elif isinstance(parameters, ParameterPanel):
elif isinstance(parameters, ParametersPanel):
compatibility_result = code.compatible_with_signature(
list(parameters.parameters.keys())
)
Expand Down Expand Up @@ -196,21 +196,21 @@ def __init__(
for output in outputs:
if isinstance(output, Figure):
# This needs to happen before the creation of the
# ParameterPanel otherwise the figure is not properly closed. I
# ParametersPanel otherwise the figure is not properly closed. I
# am not sure why, I guess it is something related to interact
self._cue_outputs.append(CueFigure(output))
elif isinstance(output, CueOutput):
self._cue_outputs.append(output)
else:
self._cue_outputs.append(CueObject(output))

self._parameter_panel: Union[ParameterPanel, None]
self._parameters_panel: Union[ParametersPanel, None]
if isinstance(parameters, dict):
self._parameter_panel = ParameterPanel(**parameters)
elif isinstance(parameters, ParameterPanel):
self._parameter_panel = parameters
self._parameters_panel = ParametersPanel(**parameters)
elif isinstance(parameters, ParametersPanel):
self._parameters_panel = parameters
else:
self._parameter_panel = None
self._parameters_panel = None

self._cue_code = self._code

Expand All @@ -233,14 +233,14 @@ def __init__(
button_tooltip="Check the correctness of your code",
)

self._cue_parameter_panel = self._parameter_panel
self._cue_parameters_panel = self._parameters_panel
if (
self._parameter_panel is None
self._parameters_panel is None
and self._update_func is None
and self._code is None
):
self._update_button = None
self._cue_parameter_panel = None
self._cue_parameters_panel = None
else:
# set up update button and cueing
# -------------------------------
Expand All @@ -257,18 +257,18 @@ def __init__(
# set up parameter panel
# ----------------------

if self._parameter_panel is not None:
if self._parameters_panel is not None:
if self._update_mode == "continuous":
self._parameter_panel.set_parameters_widget_attr(
self._parameters_panel.set_parameters_widget_attr(
"continuous_update", True
)
elif self._update_mode == "release":
self._parameter_panel.set_parameters_widget_attr(
self._parameters_panel.set_parameters_widget_attr(
"continuous_update", False
)

if self._update_mode in ["continuous", "release"]:
self._parameter_panel.observe_parameters(
self._parameters_panel.observe_parameters(
self._on_trait_parameters_changed, "value"
)

Expand All @@ -289,47 +289,47 @@ def __init__(
cue_output._traits_to_observe = ["function_body"]
cue_output.observe_widgets()

self._cue_parameter_panel = UpdateCueBox(
self._cue_parameters_panel = UpdateCueBox(
[],
[],
self._parameter_panel,
self._parameters_panel,
cued=self._code is not None,
)
else:
widgets_to_observe = None
traits_to_observe = None
update_button_disable_during_action = True

self._cue_parameter_panel = UpdateCueBox(
self._parameter_panel.panel_parameters_widget,
self._parameter_panel.panel_parameters_trait, # type: ignore
self._parameter_panel,
self._cue_parameters_panel = UpdateCueBox(
self._parameters_panel.panel_parameters_widget,
self._parameters_panel.panel_parameters_trait, # type: ignore
self._parameters_panel,
)

for cue_output in self._cue_outputs:
if self._code is not None:
# TODO this has to be made public
cue_output._widgets_to_observe = [
self._code
] + self._parameter_panel.panel_parameters_widget
] + self._parameters_panel.panel_parameters_widget
# fmt: off
cue_output._traits_to_observe = (

[ # type: ignore[assignment]
"function_body"
]
+ self._parameter_panel.panel_parameters_trait
+ self._parameters_panel.panel_parameters_trait
)
# fmt: on

cue_output.observe_widgets()
else:
# TODO this has to be made public
cue_output._widgets_to_observe = (
self._parameter_panel.panel_parameters_widget
self._parameters_panel.panel_parameters_widget
)
cue_output._traits_to_observe = (
self._parameter_panel.panel_parameters_trait # type: ignore[assignment] # noqa: E501
self._parameters_panel.panel_parameters_trait # type: ignore[assignment] # noqa: E501
)
cue_output.observe_widgets()
elif self._code is not None:
Expand All @@ -347,8 +347,8 @@ def __init__(
reset_update_cue_widgets = []
if self._cue_code is not None:
reset_update_cue_widgets.append(self._cue_code)
if self._cue_parameter_panel is not None:
reset_update_cue_widgets.append(self._cue_parameter_panel)
if self._cue_parameters_panel is not None:
reset_update_cue_widgets.append(self._cue_parameters_panel)
if self._cue_outputs is not None:
reset_update_cue_widgets.extend(self._cue_outputs)

Expand Down Expand Up @@ -382,7 +382,7 @@ def __init__(
self._update_button = None

if self._exercise_registry is None or (
self._code is None and self._parameter_panel is None
self._code is None and self._parameters_panel is None
):
self._save_button = None
self._load_button = None
Expand All @@ -395,12 +395,12 @@ def __init__(
save_widgets_to_observe.append(self._code)
save_traits_to_observe.append("function_body")

if self._parameter_panel is not None:
if self._parameters_panel is not None:
save_widgets_to_observe.extend(
self._parameter_panel.panel_parameters_widget
self._parameters_panel.panel_parameters_widget
)
save_traits_to_observe.extend(
self._parameter_panel.panel_parameters_trait
self._parameters_panel.panel_parameters_trait
)

if self._cue_code is not None:
Expand Down Expand Up @@ -445,7 +445,7 @@ def __init__(
widget
for widget in [
self._cue_code,
self._cue_parameter_panel,
self._cue_parameters_panel,
self._load_button,
]
if widget is not None
Expand All @@ -456,7 +456,7 @@ def __init__(
widget
for widget in [
self._cue_code,
self._cue_parameter_panel,
self._cue_parameters_panel,
self._save_button,
]
if widget is not None
Expand All @@ -471,8 +471,8 @@ def __init__(

if self._cue_code is not None:
demo_children.append(self._cue_code)
if self._cue_parameter_panel is not None:
demo_children.append(self._cue_parameter_panel)
if self._cue_parameters_panel is not None:
demo_children.append(self._cue_parameters_panel)

buttons = []
if self._check_button is None and self._update_button is None:
Expand Down Expand Up @@ -522,10 +522,10 @@ def __init__(
def answer(self) -> dict:
return {
"code": None if self._code is None else self._code.function_body,
"parameter_panel": (
"parameters_panel": (
None
if self._parameter_panel is None
else self._parameter_panel.parameters
if self._parameters_panel is None
else self._parameters_panel.parameters
),
}

Expand All @@ -540,8 +540,11 @@ def answer(self, answer: dict):

if answer["code"] is not None and self._code is not None:
self._code.function_body = answer["code"]
if answer["parameter_panel"] is not None and self._parameter_panel is not None:
self._parameter_panel.update_parameters(answer["parameter_panel"])
if (
answer["parameters_panel"] is not None
and self._parameters_panel is not None
):
self._parameters_panel.update_parameters(answer["parameters_panel"])

if self._save_cue_box is not None:
self._save_cue_box.observe_widgets()
Expand All @@ -558,8 +561,8 @@ def panel_parameters(self) -> Dict[str, Check.FunInParamT]:
"""
return (
{}
if self._parameter_panel is None
else self._parameter_panel.panel_parameters
if self._parameters_panel is None
else self._parameters_panel.panel_parameters
)

@property
Expand All @@ -568,7 +571,9 @@ def parameters(self) -> Dict[str, Check.FunInParamT]:
:return: All parameters that were given on initialization are returned,
also including also fixed parameters.
"""
return {} if self._parameter_panel is None else self._parameter_panel.parameters
return (
{} if self._parameters_panel is None else self._parameters_panel.parameters
)

@property
def exercise_title(self) -> Union[str, None]:
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
CueOutput,
ExerciseRegistry,
ExerciseWidget,
ParameterPanel,
ParametersPanel,
TextExercise,
assert_equal,
assert_numpy_allclose,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
from widget_code_input.utils import CodeValidationError

from scwidgets.check import Check, CheckRegistry, CheckResult
from scwidgets.code import CodeInput, ParameterPanel
from scwidgets.code import CodeInput, ParametersPanel
from scwidgets.cue import CueObject
from scwidgets.exercise import CodeExercise, ExerciseRegistry

from .test_check import multi_param_check, single_param_check


class TestParameterPanel:
class TestParametersPanel:

def test_parameters(self):
from ipywidgets import fixed

panel = ParameterPanel(**{"x": (0, 1, 0.5), "y": (2, 3, 1), "z": fixed(5)})
panel = ParametersPanel(**{"x": (0, 1, 0.5), "y": (2, 3, 1), "z": fixed(5)})
assert panel.parameters == {"x": 0.0, "y": 2, "z": 5}
assert panel.panel_parameters == {"x": 0.0, "y": 2}

Expand Down

0 comments on commit 33a4b31

Please sign in to comment.