Skip to content

Commit cbb2d6b

Browse files
committed
Only create update button when update_mode manual or code input is not None
This is a reimplementation of 186a495 that still shows the update button when update_mode is manaul and it does not create the update button in the first place.
1 parent 9d3abbe commit cbb2d6b

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

src/scwidgets/exercise/_widget_code_exercise.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def __init__(
244244
[],
245245
[],
246246
self._parameter_panel,
247+
cued = False,
247248
)
248249
else:
249250
widgets_to_observe = None
@@ -295,30 +296,34 @@ def __init__(
295296
if self._cue_outputs is not None:
296297
reset_update_cue_widgets.extend(self._cue_outputs)
297298

298-
if self._code is not None:
299-
description = "Run Code"
300-
button_tooltip = (
301-
"Runs the code and updates outputs with the specified parameters"
299+
if self._code is not None or self._update_mode == "manual":
300+
if self._code is not None:
301+
description = "Run Code"
302+
button_tooltip = (
303+
"Runs the code and updates outputs with the specified parameters"
304+
)
305+
else:
306+
description = "Update"
307+
button_tooltip = "Updates outputs with the specified parameters"
308+
309+
self._update_button = UpdateResetCueButton(
310+
reset_update_cue_widgets, # type: ignore[arg-type]
311+
self._on_click_update_action,
312+
disable_on_successful_action=kwargs.pop(
313+
"disable_update_button_on_successful_action", False
314+
),
315+
disable_during_action=kwargs.pop(
316+
"disable_update_button_during_action",
317+
update_button_disable_during_action,
318+
),
319+
widgets_to_observe=widgets_to_observe,
320+
traits_to_observe=traits_to_observe,
321+
description=description,
322+
button_tooltip=button_tooltip,
302323
)
303324
else:
304-
description = "Update"
305-
button_tooltip = "Updates outputs with the specified parameters"
325+
self._update_button = None
306326

307-
self._update_button = UpdateResetCueButton(
308-
reset_update_cue_widgets, # type: ignore[arg-type]
309-
self._on_click_update_action,
310-
disable_on_successful_action=kwargs.pop(
311-
"disable_update_button_on_successful_action", False
312-
),
313-
disable_during_action=kwargs.pop(
314-
"disable_update_button_during_action",
315-
update_button_disable_during_action,
316-
),
317-
widgets_to_observe=widgets_to_observe,
318-
traits_to_observe=traits_to_observe,
319-
description=description,
320-
button_tooltip=button_tooltip,
321-
)
322327

323328
if self._exercise_registry is None or (
324329
self._code is None and self._parameter_panel is None
@@ -447,6 +452,11 @@ def __init__(
447452
*args,
448453
**kwargs,
449454
)
455+
# In this case there is no code to be written by the student, so the code
456+
# exercise should work out of the box. Since the cues for the parameters
457+
# are also disabled, we update at the beginning once.
458+
if self._update_mode in ["release", "continuous"] and self._code is None:
459+
self.run_update()
450460

451461
@property
452462
def answer(self) -> dict:
@@ -511,16 +521,7 @@ def exercise_description(self) -> Union[str, None]:
511521
return self._exercise_description
512522

513523
def _on_trait_parameters_changed(self, change: dict):
514-
if self._update_button is None:
515-
self._output.clear_output(wait=True)
516-
error = ValueError(
517-
"Invalid state: _on_trait_parameters_changed was "
518-
"invoked but no update button was defined"
519-
)
520-
with self._output:
521-
raise error
522-
raise error
523-
self._update_button.click()
524+
self.run_update()
524525

525526
def _on_click_check_action(self) -> bool:
526527
self._output.clear_output(wait=True)

0 commit comments

Comments
 (0)