API

class scwidgets.CheckRegistry(**kwargs: Any)[source]

Manages the assignment of checks to widgets and the execution of checks. It allows to run the checks of all widgets and properly pipes the result to the corresponding function of the widget.

add_check(widget: CheckableWidget, asserts: List[Callable[[Tuple[Any, ...], Tuple[Any, ...]], str] | Callable[[FingerprintT, FingerprintT], str] | Callable[[Tuple[Any, ...]], str] | Callable[[], str]] | Callable[[Tuple[Any, ...], Tuple[Any, ...]], str] | Callable[[FingerprintT, FingerprintT], str] | Callable[[Tuple[Any, ...]], str] | Callable[[], str], inputs_parameters: List[dict] | dict | None = None, outputs_references: List[Tuple[Any, ...]] | Tuple[Any, ...] | None = None, fingerprint: Callable[[Tuple[Any, ...]], FingerprintT] | None = None, suppress_fingerprint_asserts: bool = True, stop_on_assert_error_raised: bool = False)[source]

Adds a new check for the specified widget. The check is defined using assert functions, and optional input parameters, output references, and fingerprint function. :param widget:

The widget to which the check is being added.

Parameters:
  • asserts – Functions to validate the widget’s output.

  • inputs_parameters – Inputs to provide when calling the widget’s output computation method.

  • outputs_references – Expected reference outputs used for assertions.

  • fingerprint – Optional function to obfuscate outputs before assertions.

  • suppress_fingerprint_asserts – If True, suppresses assert messages involving fingerprinted outputs.

property checks

Returns all checks registered for each widget.

nb_conducted_asserts(widget: CheckableWidget)[source]

The total number of asserts that will be conducted for the widget

Parameters:

widget – the checks of the widget are targeted

property registered_widgets

Returns a copy of all widgets currently registered with this registry.

class scwidgets.CodeExercise(**kwargs: Any)[source]

A widget to demonstrate code interactively in a variety of ways. It is a combination of the several widgets that allow to check, run and visualize code.

Parameters:
  • code – A function or CodeInput that is the input of code

  • check_registry – A check registry that is used to register checks

  • exercise_registry – A exercise registry that is used to register the answers to save them later. If specified, the save and load panel will appear.

  • key – The key that is used to store the exercise in the json file.

  • parameters – Input parameters for the ParametersPanel class or an initialized ParametersPanel object. Specifies the arguments in the parameter panel.

  • update_mode – Determines how the parameters are refreshed on changes of the code input or parameters

  • outputs – List of CueOutputs that are drawn and refreshed

  • update – A function that is run during the update process. The function takes as argument the CodeExercise, so it can update all cue_outputs

  • description – A string describing the exercises that will be put into an HTML widget above the exercise.

  • title – A title for the exercise. If not given, key is used.

property answer: dict

Translates the widget state into a string

compute_output_to_check(*args, **kwargs) Tuple[Any, ...][source]

The widget returns the output that will be verified by the added checks.

handle_checks_result(results: List[CheckResult | Exception])[source]

Function that controls how results of the checks are handled.

handle_load_result(result: str | Exception)[source]

Function that controls how a load result is handled. If the result is a string, the loading was successful. The result contains a string that can be outputed.

handle_save_result(result: str | Exception)[source]

Function that controls how a save result is handled. If the result is a string, the saving was successful. The result contains a string that can be outputed.

property panel_parameters: Dict[str, FunInParamT]
Returns:

Only parameters that are tunable in the parameter panel are returned. Fixed parameters are ignored.

property parameters: Dict[str, FunInParamT]
Returns:

All parameters that were given on initialization are returned, including fixed parameters.

property parameters_panel: ParametersPanel | None
Returns:

The parameters panel widget.

run_code(*args, **kwargs) Tuple[Any, ...][source]

Runs the code with the given (keyword) arguments and returns the output of the code. If no code was given on initialization, then a ValueError is raised.

run_update()[source]

Invokes an update run, the same that is invoked by a click on the update button, or when a parameter is changed for update_mode “release” and “continuous”

class scwidgets.CodeInput(**kwargs: Any)[source]

Small wrapper around WidgetCodeInput that controls the output

Parameters:
  • function – A Python function to be parse automatically. Note that the parsing may alter the original formatting or lose certain syntactical nuances. If this behavior is undesired, provide the function explicitly using other parameters.

  • function_name – The name of the function

  • function_paramaters – The parameters as a continuous string as specified in the signature of the function. e.g for foo(x, y = 5) it should be “x, y = 5”

  • docstring – The docstring of the function

  • function_body – The function definition without indentation

  • builtins – A dictionary containing variable names and values that are added to the globals __builtins__ and thus available on initialization

compatible_with_signature(parameters: List[str]) str[source]

This function checks if the arguments are compatible with the function signature and returns an explanatory message if this is not the case.

property function: LambdaType

Returns the compiled function object wrapped by an try-catch block raising a CodeValidationError.

This can be assigned to a variable and then called, for instance:

func = widget.function # This can raise a CodeValidationError retval = func(parameters)

Raises:

CodeValidationError – if the function code has syntax errors (or if the function name is not a valid identifier)

property function_parameters_name: List[str]

Returns the names of the function parameters

static get_docstring(function: LambdaType) str | None[source]

Returns the docstring of a function, if it exists, without leading or trailing whitespace or triple quotes.

static get_function_body(function: LambdaType) str[source]

Extracts the body of the given function, removing the signature, docstrings, and adjusting indentation appropriately.

static get_function_parameters(function: LambdaType) str[source]

Returns the parameters of a function as a continuous string, e.g for foo(x, y = 5) it would return “x, y = 5”

property unwrapped_function: LambdaType

Returns the compiled function object.

This can be assigned to a variable and then called, for instance:

func = widget.wrapped_function # This can raise a SyntaxError retval = func(parameters)

Raises:

SyntaxError – if the function code has syntax errors (or if the function name is not a valid identifier)

class scwidgets.CueFigure(**kwargs: Any)[source]

A cued, displayable ipywidget.Output for a matplotlib figure. Provides utilities to clear and draw the updated figure. For the matplotlib inline backend, it closes the active figure to prevent any display outside of the container, which happens on the creation of the figure because pyplot does magic behind the curtain that is hard to suppress. For the matplotlib interactive widget backend, named “nbagg”, it wraps the figure within.

Parameters:
  • figure – The matplotlib figure

  • widgets_to_observe – The widget to observe if the :param traits_to_observe: has changed.

  • traits_to_observe – The trait from the :param widgets_to_observe: to observe if changed. Specify traitlets.All to observe all traits.

  • cued – Specifies if it is cued on initialization

  • show_toolbars – Hide toolbars and headers when using in widget mode.

  • css_style

    • base: the css style of the box during initialization

    • cue: the css style that is added when :param traits_to_observe: in widget :param widgets_to_observe: changes. It is supposed to change the style of the box such that the user has a visual cue that :param widget_to_cue: has changed.

clear_display(wait=False)[source]
Parameters:

wait – same meaning as for the wait parameter in the ipywidgets.clear_output function

clear_figure()[source]

Clears the figure while retaining axes as figure.clear() removes the axes sometimes.

draw_display()[source]

Enforces redrawing the figure

class scwidgets.CueObject(**kwargs: Any)[source]

A cued displayable ipywidget.Output for any Python object. Provides utilities to clear and redraw the object, for example, after an update.

Parameters:
  • object – The object to display

  • widgets_to_observe – The widget to observe if the traits_to_observe has changed.

  • traits_to_observe – The trait from the widgets_to_observe to observe if changed. Specify traitlets.All to observe all traits.

  • cued – Specifies if it is cued on initialization

  • css_style

    • base: the css style of the box during initialization

    • cue: the css style that is added when traits_to_observe in widget widgets_to_observe changes. It is supposed to change the style of the box such that the user has a visual cue that widget_to_cue has changed.

class scwidgets.CueOutput(**kwargs: Any)[source]

A cued displayable ipywidget.Output for any Python object.

Parameters:
  • widgets_to_observe – The widget to observe if the traits_to_observe has changed.

  • traits_to_observe – The trait from the widgets_to_observe to observe if changed. Specify traitlets.All to observe all traits.

  • cued – Specifies if it is cued on initialization

  • css_style

    • base: the css style of the box during initialization

    • cue: the css style that is added when traits_to_observe in widget widgets_to_observe changes. It is supposed to change the style of the box such that the user has a visual cue that widget_to_cue has changed.

class scwidgets.ParametersPanel(**kwargs: Any)[source]

A wrapper around ipywidgets.interactive to have more control on how the parameters are connected and the parameters are observed by the buttons and panels :param parameters:

Can be any input that is allowed as keyword arguments in ipywidgets.interactive for the parameters. _option and other widget layout parameters are controlled by CodeExercise.

property panel_parameters: Dict[str, Any]
Returns:

Only parameters that are tunable in the parameter panel are returned. Fixed parameters are ignored.

property panel_parameters_widget: List[Widget]
Returns:

Only parameters that are tunable in the parameter panel are returned. Fixed parameters are ignored.

property param_to_widget_map: dict[str, Widget]
Returns:

A dictionary mapping parameter names to their corresponding widgets.

property parameters: Dict[str, Any]
Returns:

All parameters that were given on initialization are returned, also including fixed parameters.

class scwidgets.TextExercise(**kwargs: Any)[source]
Parameters:
  • textarea – a custom textarea with custom styling. If not specified, the standard parameters are given.

  • key – The key that is used to store the exercise in the JSON file.

  • description – A string describing the exercises that will be put into an HTML widget above the exercise.

  • title – A title for the exercise. If not given, the key is used.

property answer: dict

Translates the widget state into a string

handle_load_result(result: str | Exception) None[source]

Function that controls how a load result is handled. If the result is a string, the loading was successful. The result contains a string that can be outputed.

handle_save_result(result: str | Exception) None[source]

Function that controls how a save result is handled. If the result is a string, the saving was successful. The result contains a string that can be outputed.

scwidgets.assert_equal(output_parameters: Tuple[Any, ...], output_references: Tuple[Any, ...], parameters_to_check: Iterable[int] | str = 'all') AssertResult[source]

Check if output_parameters are equal to output_references using simple Python equality check.

scwidgets.assert_numpy_allclose(output_parameters: Tuple[Any, ...], output_references: Tuple[Any, ...], parameters_to_check: Iterable[int] | str = 'auto', rtol=1e-05, atol=1e-08, equal_nan=False) AssertResult[source]

Check if output_parameters are numerically close to output_references using numpy.allclose().

scwidgets.assert_numpy_floating_sub_dtype(output_parameters: ~typing.Tuple[~typing.Any, ...] | tuple[~scwidgets.check._check.FingerprintT], *, numpy_type: ~numpy.dtype | type = <class 'numpy.floating'>, parameters_to_check: ~typing.Iterable[int] | str = 'all') AssertResult

Check that output parameters have the correct numpy sub-dtype.

scwidgets.assert_numpy_sub_dtype(output_parameters: Tuple[Any, ...] | tuple[FingerprintT], numpy_type: dtype | type, parameters_to_check: Iterable[int] | str = 'all') AssertResult[source]

Check that output parameters have the correct numpy sub-dtype.

scwidgets.assert_shape(output_parameters: Tuple[Any, ...], output_references: Tuple[Any, ...], parameters_to_check: Iterable[int] | str = 'auto') AssertResult[source]

Check that the shape of output parameters matches the reference.

scwidgets.assert_type(output_parameters: Tuple[Any, ...], output_references: Tuple[Any, ...], parameters_to_check: Iterable[int] | str = 'all') AssertResult[source]

Check that output parameters have the correct type.

exercise

class scwidgets.exercise.CodeExercise(**kwargs: Any)[source]

A widget to demonstrate code interactively in a variety of ways. It is a combination of the several widgets that allow to check, run and visualize code.

Parameters:
  • code – A function or CodeInput that is the input of code

  • check_registry – A check registry that is used to register checks

  • exercise_registry – A exercise registry that is used to register the answers to save them later. If specified, the save and load panel will appear.

  • key – The key that is used to store the exercise in the json file.

  • parameters – Input parameters for the ParametersPanel class or an initialized ParametersPanel object. Specifies the arguments in the parameter panel.

  • update_mode – Determines how the parameters are refreshed on changes of the code input or parameters

  • outputs – List of CueOutputs that are drawn and refreshed

  • update – A function that is run during the update process. The function takes as argument the CodeExercise, so it can update all cue_outputs

  • description – A string describing the exercises that will be put into an HTML widget above the exercise.

  • title – A title for the exercise. If not given, key is used.

property answer: dict

Translates the widget state into a string

compute_output_to_check(*args, **kwargs) Tuple[Any, ...][source]

The widget returns the output that will be verified by the added checks.

handle_checks_result(results: List[CheckResult | Exception])[source]

Function that controls how results of the checks are handled.

handle_load_result(result: str | Exception)[source]

Function that controls how a load result is handled. If the result is a string, the loading was successful. The result contains a string that can be outputed.

handle_save_result(result: str | Exception)[source]

Function that controls how a save result is handled. If the result is a string, the saving was successful. The result contains a string that can be outputed.

property panel_parameters: Dict[str, FunInParamT]
Returns:

Only parameters that are tunable in the parameter panel are returned. Fixed parameters are ignored.

property parameters: Dict[str, FunInParamT]
Returns:

All parameters that were given on initialization are returned, including fixed parameters.

property parameters_panel: ParametersPanel | None
Returns:

The parameters panel widget.

run_code(*args, **kwargs) Tuple[Any, ...][source]

Runs the code with the given (keyword) arguments and returns the output of the code. If no code was given on initialization, then a ValueError is raised.

run_update()[source]

Invokes an update run, the same that is invoked by a click on the update button, or when a parameter is changed for update_mode “release” and “continuous”

class scwidgets.exercise.ExerciseWidget(exercise_registry: None | ExerciseRegistry, exercise_key: None | Hashable)[source]

Any widget inheriting from this class can be (de)serialized by WidgetStateRegistry. The serialization offered by ipywidgets cannot be loaded out-of-the-box for a restarted notebook since the widget IDs change

Parameters:
  • exercise_registry – the exercise registry that registers the answers for this widget

  • exercise_key – Identifier for the widget, must be unique for each registered widget

Reference

https://ipywidgets.readthedocs.io/en/7.x/examples/Widget%20Low%20Level.html https://github.com/jupyter-widgets/ipywidgets/issues/2369

property answer: dict

Translates the widget state into a string

handle_load_result(result: str | Exception) None[source]

Function that controls how a load result is handled. If the result is a string, the loading was successful. The result contains a string that can be outputed.

handle_save_result(result: str | Exception) None[source]

Function that controls how a save result is handled. If the result is a string, the saving was successful. The result contains a string that can be outputed.

class scwidgets.exercise.MultipleChoiceExercise(**kwargs: Any)[source]
Parameters:
  • options – Either a dict or a list. If a dict is provided, the widget will display the dictionary’s value but save its key to the registry.

  • key – Unique key for the exercise.

  • description – A string describing the exercise that will be put into an HTML widget above the exercise.

  • title – A title for the exercise. If not provided, the key is used.

  • exercise_registry – An exercise registry that is used to register the answers to save them later. If specified the save and load panel will appear.

  • allow_multiple – Whether multiple selections are allowed.

  • randomize_order – Whether to randomize order of options.

property answer: dict

Translates the widget state into a string

handle_load_result(result: str | Exception) None[source]

Function that controls how a load result is handled. If the result is a string, the loading was successful. The result contains a string that can be outputed.

handle_save_result(result: str | Exception) None[source]

Function that controls how a save result is handled. If the result is a string, the saving was successful. The result contains a string that can be outputed.

class scwidgets.exercise.TextExercise(**kwargs: Any)[source]
Parameters:
  • textarea – a custom textarea with custom styling. If not specified, the standard parameters are given.

  • key – The key that is used to store the exercise in the JSON file.

  • description – A string describing the exercises that will be put into an HTML widget above the exercise.

  • title – A title for the exercise. If not given, the key is used.

property answer: dict

Translates the widget state into a string

handle_load_result(result: str | Exception) None[source]

Function that controls how a load result is handled. If the result is a string, the loading was successful. The result contains a string that can be outputed.

handle_save_result(result: str | Exception) None[source]

Function that controls how a save result is handled. If the result is a string, the saving was successful. The result contains a string that can be outputed.

check

class scwidgets.check.AssertResult(assert_name: str, parameter_indices: int | List[int], parameter_values: Any | List[Any], messages: str | List[str])[source]

Represents the result of an assertion check, storing information about the assertion name, parameter indices, parameter values, and messages. If any of parameter_indices, parameter_values, or messages is a list, then all must be lists of the same length.

Parameters:
  • assert_name – The name of the assertion being checked.

  • parameter_indices – The index or indices of the parameters that were evaluated in the assertion. If a single index is provided, it will be converted into a list.

  • parameter_values – The value(s) of the parameters at the given indices that were checked in the assertion. If a single value is provided, it will be converted into a list.

  • messages – A message or list of messages describing the assertion result for each parameter index. If a single message is provided, it will be converted into a list.

class scwidgets.check.Check(function_to_check: Callable[..., FunOutParamsT], asserts: List[AssertFunT] | AssertFunT, inputs_parameters: List[Dict[str, FunInParamT]] | Dict[str, FunInParamT] | None = None, outputs_references: List[tuple] | tuple | None = None, fingerprint: Callable[[Check.FunOutParamsT], Check.FingerprintT] | None = None, suppress_fingerprint_asserts: bool = True, stop_on_assert_error_raised: bool = True)[source]

A check verifies the correctness of a function for a set of input parameters using a list of univariate and bivariate asserts with the option to obscure the reference outputs.

Parameters:
  • function_to_check – The function that accepts each input parameters in input_parameters

  • input_parameters – A dict or a list of dictionaries each containing the argument name and its value as (key, value) pair that is used as input for the function function_to_check.

  • outputs_references – A tuple or a list of tuples each containing the expected output of the function function_to_check for the inputs in the input_parameters

  • asserts – A list of assert functions. Each assert function verifies the outputs from function_to_check. Assert functions can be univariate (accepting only output), bivariate (accepting output and reference output), or nullvariate (accepting no arguments). If output references have been set, it can take additional output references to compare with. If a fingerprint is given, the it is compared while assert functions with a single argument are always applied on the output parameters.

  • fingerprint – A one-way function that transforms the outputs from function_to_check, obscuring direct comparisons with the output_references.

  • suppress_fingerprint_asserts – Specifies if the assert messages that use the fingerprint function output for tests are suppressed. The message might be confusing to a student as the output is converted by the fingerprint function.

  • stop_on_assert_error_raised – Specifies if running the asserts is stopped as soon as an error is raised in an assert. If a lot of asserts are specified, the printing of a lot of error tracebacks might make debugging harder.

check_function() CheckResult[source]

For each input (first depth list) returns the result message for each assert (second depth list). If a result message is empty, the assert was successful, otherwise it contains information about the failure.

class scwidgets.check.CheckRegistry(**kwargs: Any)[source]

Manages the assignment of checks to widgets and the execution of checks. It allows to run the checks of all widgets and properly pipes the result to the corresponding function of the widget.

add_check(widget: CheckableWidget, asserts: List[Callable[[Tuple[Any, ...], Tuple[Any, ...]], str] | Callable[[FingerprintT, FingerprintT], str] | Callable[[Tuple[Any, ...]], str] | Callable[[], str]] | Callable[[Tuple[Any, ...], Tuple[Any, ...]], str] | Callable[[FingerprintT, FingerprintT], str] | Callable[[Tuple[Any, ...]], str] | Callable[[], str], inputs_parameters: List[dict] | dict | None = None, outputs_references: List[Tuple[Any, ...]] | Tuple[Any, ...] | None = None, fingerprint: Callable[[Tuple[Any, ...]], FingerprintT] | None = None, suppress_fingerprint_asserts: bool = True, stop_on_assert_error_raised: bool = False)[source]

Adds a new check for the specified widget. The check is defined using assert functions, and optional input parameters, output references, and fingerprint function. :param widget:

The widget to which the check is being added.

Parameters:
  • asserts – Functions to validate the widget’s output.

  • inputs_parameters – Inputs to provide when calling the widget’s output computation method.

  • outputs_references – Expected reference outputs used for assertions.

  • fingerprint – Optional function to obfuscate outputs before assertions.

  • suppress_fingerprint_asserts – If True, suppresses assert messages involving fingerprinted outputs.

property checks

Returns all checks registered for each widget.

nb_conducted_asserts(widget: CheckableWidget)[source]

The total number of asserts that will be conducted for the widget

Parameters:

widget – the checks of the widget are targeted

property registered_widgets

Returns a copy of all widgets currently registered with this registry.

class scwidgets.check.CheckResult[source]

Represents the result of a check, storing information about the assert results, assert names, input parameters, and suppressed assert messages.

class scwidgets.check.CheckableWidget(check_registry: CheckRegistry | None, name: str | None = None)[source]

A base class for any widget to inherit from to be compatible with the CheckRegistry. The widget can be registered to a CheckRegistry, which allows applying checks.

Parameters:
  • check_registry – the check registry that registers the checks for this widget

  • name – Optional name of the widget that is shown in the messages of the checks

add_check(*args, **kwargs)[source]

Adds checks to the widget. Accepts either a Check object (or list of Check objects) directly, or parameters that define a new check.

compute_output_to_check(*input_args: FunInParamT) Tuple[Any, ...][source]

The widget returns the output that will be verified by the added checks.

handle_checks_result(results: List[CheckResult | Exception]) None[source]

Function that controls how results of the checks are handled.

scwidgets.check.assert_equal(output_parameters: Tuple[Any, ...], output_references: Tuple[Any, ...], parameters_to_check: Iterable[int] | str = 'all') AssertResult[source]

Check if output_parameters are equal to output_references using simple Python equality check.

scwidgets.check.assert_numpy_allclose(output_parameters: Tuple[Any, ...], output_references: Tuple[Any, ...], parameters_to_check: Iterable[int] | str = 'auto', rtol=1e-05, atol=1e-08, equal_nan=False) AssertResult[source]

Check if output_parameters are numerically close to output_references using numpy.allclose().

scwidgets.check.assert_numpy_floating_sub_dtype(output_parameters: ~typing.Tuple[~typing.Any, ...] | tuple[~scwidgets.check._check.FingerprintT], *, numpy_type: ~numpy.dtype | type = <class 'numpy.floating'>, parameters_to_check: ~typing.Iterable[int] | str = 'all') AssertResult

Check that output parameters have the correct numpy sub-dtype.

scwidgets.check.assert_numpy_sub_dtype(output_parameters: Tuple[Any, ...] | tuple[FingerprintT], numpy_type: dtype | type, parameters_to_check: Iterable[int] | str = 'all') AssertResult[source]

Check that output parameters have the correct numpy sub-dtype.

scwidgets.check.assert_shape(output_parameters: Tuple[Any, ...], output_references: Tuple[Any, ...], parameters_to_check: Iterable[int] | str = 'auto') AssertResult[source]

Check that the shape of output parameters matches the reference.

scwidgets.check.assert_type(output_parameters: Tuple[Any, ...], output_references: Tuple[Any, ...], parameters_to_check: Iterable[int] | str = 'all') AssertResult[source]

Check that output parameters have the correct type.

code

class scwidgets.code.CodeInput(**kwargs: Any)[source]

Small wrapper around WidgetCodeInput that controls the output

Parameters:
  • function – A Python function to be parse automatically. Note that the parsing may alter the original formatting or lose certain syntactical nuances. If this behavior is undesired, provide the function explicitly using other parameters.

  • function_name – The name of the function

  • function_paramaters – The parameters as a continuous string as specified in the signature of the function. e.g for foo(x, y = 5) it should be “x, y = 5”

  • docstring – The docstring of the function

  • function_body – The function definition without indentation

  • builtins – A dictionary containing variable names and values that are added to the globals __builtins__ and thus available on initialization

compatible_with_signature(parameters: List[str]) str[source]

This function checks if the arguments are compatible with the function signature and returns an explanatory message if this is not the case.

property function: LambdaType

Returns the compiled function object wrapped by an try-catch block raising a CodeValidationError.

This can be assigned to a variable and then called, for instance:

func = widget.function # This can raise a CodeValidationError retval = func(parameters)

Raises:

CodeValidationError – if the function code has syntax errors (or if the function name is not a valid identifier)

property function_parameters_name: List[str]

Returns the names of the function parameters

static get_docstring(function: LambdaType) str | None[source]

Returns the docstring of a function, if it exists, without leading or trailing whitespace or triple quotes.

static get_function_body(function: LambdaType) str[source]

Extracts the body of the given function, removing the signature, docstrings, and adjusting indentation appropriately.

static get_function_parameters(function: LambdaType) str[source]

Returns the parameters of a function as a continuous string, e.g for foo(x, y = 5) it would return “x, y = 5”

property unwrapped_function: LambdaType

Returns the compiled function object.

This can be assigned to a variable and then called, for instance:

func = widget.wrapped_function # This can raise a SyntaxError retval = func(parameters)

Raises:

SyntaxError – if the function code has syntax errors (or if the function name is not a valid identifier)

class scwidgets.code.ParametersPanel(**kwargs: Any)[source]

A wrapper around ipywidgets.interactive to have more control on how the parameters are connected and the parameters are observed by the buttons and panels :param parameters:

Can be any input that is allowed as keyword arguments in ipywidgets.interactive for the parameters. _option and other widget layout parameters are controlled by CodeExercise.

property panel_parameters: Dict[str, Any]
Returns:

Only parameters that are tunable in the parameter panel are returned. Fixed parameters are ignored.

property panel_parameters_widget: List[Widget]
Returns:

Only parameters that are tunable in the parameter panel are returned. Fixed parameters are ignored.

property param_to_widget_map: dict[str, Widget]
Returns:

A dictionary mapping parameter names to their corresponding widgets.

property parameters: Dict[str, Any]
Returns:

All parameters that were given on initialization are returned, also including fixed parameters.

cue

class scwidgets.cue.CheckCueBox(**kwargs: Any)[source]

A box around the widget widget_to_cue that adds a visual cue related to checking when the trait traits_to_observe in the widget widgets_to_observe changes.

Parameters:
  • widgets_to_observe – The widget to observe if the traits_to_observe has changed.

  • traits_to_observe – The trait from the widgets_to_observe to observe if changed. Specify traitlets.All to observe all traits.

  • widget_to_cue – The widget to wrap the box around to give a visual cue, once traits_to_observe has changed If None, then the widget_to_cue is set to widgets_to_observe.

  • cued – Specifies if it is cued on initialization

Further accepts the same (keyword) arguments as ipywidgets.Box.

class scwidgets.cue.CheckResetCueButton(**kwargs: Any)[source]

A button that resets the cueing of the :param cue_widgets: on a successful check action.

Parameters:
  • cue_widgets – List of cue boxes the button resets on successful click We assert that all boxes observe the same traits of the same widget

  • action – A callable that returns a boolean that specifies if the action was successful. It is called on a button click, and the cues in cue_widgets are removed if it was successful. If set to False, nothing happens.

  • disable_on_successful_action – Specifies if the button should be disabled on a successful action

  • disable_during_action – Specifies if the button should be disabled during the action

  • widgets_to_observe – The widget to observe if the traits_to_observe has changed. If None then widgets from cue_widgets are taken.

  • traits_to_observe – The trait from the widgets_to_observe to observe changes of. Specify traitlets.All to observe all traits. If None, traits from cue_widgets are taken.

  • cued – Specifies if it is cued on initialization. If None then the button is cued when cue_widget: is cued.

Further accepts the same (keyword) arguments as ipywidgets.Button.

class scwidgets.cue.CueBox(**kwargs: Any)[source]

A box around the widget widget_to_cue that adds a visual cue defined in the css_style when the trait traits_to_observe in the widget widgets_to_observe changes. If the widgets_to_observe is a list, each widget is observed separately.

Parameters:
  • widgets_to_observe – The widget to observe if the traits_to_observe has changed.

  • traits_to_observe – The trait from the widgets_to_observe to observe if changed. Specify traitlets.All to observe all traits.

  • widget_to_cue – The widget to wrap the box around to give a visual cue, once traits_to_observe has changed If None, then the widget_to_cue is set to widgets_to_observe.

  • cued – Specifies if it is cued on initialization

  • css_style

    • base: the css style of the box during initialization

    • cue: the css style that is added when traits_to_observe in widget widgets_to_observe changes. It is supposed to change the style of the box such that the user has a visual cue that widget_to_cue has changed.

Further accepts the same (keyword) arguments as ipywidgets.Box.

class scwidgets.cue.CueFigure(**kwargs: Any)[source]

A cued, displayable ipywidget.Output for a matplotlib figure. Provides utilities to clear and draw the updated figure. For the matplotlib inline backend, it closes the active figure to prevent any display outside of the container, which happens on the creation of the figure because pyplot does magic behind the curtain that is hard to suppress. For the matplotlib interactive widget backend, named “nbagg”, it wraps the figure within.

Parameters:
  • figure – The matplotlib figure

  • widgets_to_observe – The widget to observe if the :param traits_to_observe: has changed.

  • traits_to_observe – The trait from the :param widgets_to_observe: to observe if changed. Specify traitlets.All to observe all traits.

  • cued – Specifies if it is cued on initialization

  • show_toolbars – Hide toolbars and headers when using in widget mode.

  • css_style

    • base: the css style of the box during initialization

    • cue: the css style that is added when :param traits_to_observe: in widget :param widgets_to_observe: changes. It is supposed to change the style of the box such that the user has a visual cue that :param widget_to_cue: has changed.

clear_display(wait=False)[source]
Parameters:

wait – same meaning as for the wait parameter in the ipywidgets.clear_output function

clear_figure()[source]

Clears the figure while retaining axes as figure.clear() removes the axes sometimes.

draw_display()[source]

Enforces redrawing the figure

class scwidgets.cue.CueObject(**kwargs: Any)[source]

A cued displayable ipywidget.Output for any Python object. Provides utilities to clear and redraw the object, for example, after an update.

Parameters:
  • object – The object to display

  • widgets_to_observe – The widget to observe if the traits_to_observe has changed.

  • traits_to_observe – The trait from the widgets_to_observe to observe if changed. Specify traitlets.All to observe all traits.

  • cued – Specifies if it is cued on initialization

  • css_style

    • base: the css style of the box during initialization

    • cue: the css style that is added when traits_to_observe in widget widgets_to_observe changes. It is supposed to change the style of the box such that the user has a visual cue that widget_to_cue has changed.

class scwidgets.cue.CueOutput(**kwargs: Any)[source]

A cued displayable ipywidget.Output for any Python object.

Parameters:
  • widgets_to_observe – The widget to observe if the traits_to_observe has changed.

  • traits_to_observe – The trait from the widgets_to_observe to observe if changed. Specify traitlets.All to observe all traits.

  • cued – Specifies if it is cued on initialization

  • css_style

    • base: the css style of the box during initialization

    • cue: the css style that is added when traits_to_observe in widget widgets_to_observe changes. It is supposed to change the style of the box such that the user has a visual cue that widget_to_cue has changed.

class scwidgets.cue.CueWidget(widgets_to_observe: List[Widget] | Widget, traits_to_observe: str | Sentinel | List[str | Sentinel | List[str]] = 'value', cued: bool = True, *args, **kwargs)[source]

Observes a list of traits of widgets and sets cue when one of the widgets’ traits change. The behavior when the cue is set has to be implemented by children class.

Parameters:
  • widgets_to_observe – The widget to observe if the traits_to_observe has changed.

  • traits_to_observe – The trait from the widgets_to_observe to observe if changed. Specify traitlets.All to observe all traits.

  • cued – Specifies if it is cued on initialization

class scwidgets.cue.ResetCueButton(**kwargs: Any)[source]

A button that resets the cueing of the cue_widgets on a successful action.

Parameters:
  • cue_widgets – List of cue boxes the button resets on successful click We assert that all boxes observe the same traits of the same widget

  • action – A callable that returns a boolean that specifies if the action was successful. It is called on a button click, and the cues in cue_widgets are removed if it was successful. If set to False, nothing happens.

  • disable_on_successful_action – Specifies if the button should be disabled on a successful action

  • disable_during_action – Specifies if the button should be disabled during the action

  • css_style

    • base: the css style of the box during initialization

    • cue: the css style that is added when traits_to_observe in widget widget_to_observe changes. It is supposed to change the style of the box such that the user has a visual cue that widget_to_cue has changed.

  • widgets_to_observe – The widget to observe if the traits_to_observe has changed. If None then widgets from cue_widgets are taken.

  • traits_to_observe – The trait from the widgets_to_observe to observe changes of. Specify traitlets.All to observe all traits. If None then traits from cue_widgets are taken.

  • cued – Specifies if it is cued on initialization. If None then the button is cued when cue_widgets is cued.

Further accepts the same (keyword) arguments as ipywidgets.Button.

set_cue_widgets(cue_widgets: List[CueWidget], overwrite_cue_observes: bool = True)[source]
Parameters:
  • cue_widgets – List of cue boxes the button resets on successful click We assert that all boxes observe the same traits of the same widget

  • overwrite_cue_observes – If True, the function will override the existing observation settings based on cue_widgets.

class scwidgets.cue.SaveCueBox(**kwargs: Any)[source]

A box around the widget widget_to_cue that adds a visual cue related to saving when the trait traits_to_observe in the widget widgets_to_observe changes.

Parameters:
  • widgets_to_observe – The widget to observe if the traits_to_observe has changed.

  • traits_to_observe – The trait from the widgets_to_observe to observe if changed. Specify traitlets.All to observe all traits.

  • cued – Specifies if it is cued on initialization

  • widget_to_cue – The widget to wrap the box around to give a visual cue, once traits_to_observe has changed If None, then the widget_to_cue is set to widgets_to_observe.

Further accepts the same (keyword) arguments as ipywidgets.Box.

class scwidgets.cue.SaveResetCueButton(**kwargs: Any)[source]

A button that resets the cueing of the cue_widgets on a successful Save action.

Parameters:
  • cue_widgets – List of cue boxes the button resets on successful click We assert that all boxes observe the same traits of the same widget

  • action – A callable that returns a boolean that specifies if the action was successful. It is called on a button click, and the cues in cue_widgets are removed if it was successful. If set to False, nothing happens.

  • disable_on_successful_action – Specifies if the button should be disabled on a successful action

  • disable_during_action – Specifies if the button should be disabled during the action

  • widgets_to_observe – The widget to observe if the traits_to_observe has changed. If None then widgets from cue_widgets are taken.

  • traits_to_observe – The trait from the widgets_to_observe to observe changes of. Specify traitlets.All to observe all traits. If None, traits from cue_widgets are taken.

  • cued – Specifies if it is cued on initialization. If None then the button is cued when cue_widget: is cued.

Further accepts the same (keyword) arguments as ipywidgets.Button.

class scwidgets.cue.UpdateCueBox(**kwargs: Any)[source]

A box around the widget widget_to_cue that adds a visual cue related to updating when the trait traits_to_observe in the widget widgets_to_observe changes.

Parameters:
  • widgets_to_observe – The widget to observe if the traits_to_observe has changed.

  • traits_to_observe – The trait from the widgets_to_observe to observe if changed. Specify traitlets.All to observe all traits.

  • cued – Specifies if it is cued on initialization

  • widget_to_cue – The widget to wrap the box around to give a visual cue, once traits_to_observe has changed If None, then the widget_to_cue is set to widgets_to_observe.

Further accepts the same (keyword) arguments as ipywidgets.Box.

class scwidgets.cue.UpdateResetCueButton(**kwargs: Any)[source]

A button that resets the cueing of the :param cue_widgets: on a successful update action.

Parameters:
  • cue_widgets – List of cue boxes the button resets on successful click We assert that all boxes observe the same traits of the same widget

  • action – A callable that returns a boolean that specifies if the action was successful. It is called on a button click, and the cues in cue_widgets are removed if it was successful. If set to False, nothing happens.

  • disable_on_successful_action – Specifies if the button should be disabled on a successful action

  • disable_during_action – Specifies if the button should be disabled during the action

  • widgets_to_observe – The widget to observe if the traits_to_observe has changed. If None then widgets from cue_widgets are taken.

  • traits_to_observe – The trait from the widgets_to_observe to observe changes of. Specify traitlets.All to observe all traits. If None, traits from cue_widgets are taken.

  • cued – Specifies if it is cued on initialization. If None then the button is cued when cue_widget: is cued.

Further accepts the same (keyword) arguments as ipywidgets.Button.