Skip to content

Commit

Permalink
Make figure directly accesable through CodeExercise
Browse files Browse the repository at this point in the history
If one output is only given that is translated to a `CueFigure` then it
can be accessed with `code_exercise.figure` instead of
`code_exercise.output.figure`
  • Loading branch information
agoscinski committed Nov 29, 2024
1 parent 93e1198 commit 3f582f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/scwidgets/exercise/_widget_code_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,15 @@ def output(self) -> CueOutput | None:
def outputs(self) -> List[CueOutput]:
return self._cue_outputs

@property
def figure(self) -> Figure | None:
return (
self._cue_outputs[0].figure
if len(self._cue_outputs) > 0
and isinstance(self._cue_outputs[0], CueFigure)
else None
)

def _on_click_update_action(self) -> bool:
self._output.clear_output(wait=True)
raised_error = False
Expand Down
10 changes: 10 additions & 0 deletions tests/test_code.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
from typing import Callable, List, Literal, Union

import matplotlib.pyplot as plt
import numpy as np
import pytest
from ipywidgets import fixed
from matplotlib.figure import Figure
from widget_code_input.utils import CodeValidationError

from scwidgets.check import Check, CheckRegistry, CheckResult
Expand Down Expand Up @@ -345,3 +347,11 @@ def failing_update(a, b):
code=TestCodeInput.mock_function_0,
update_func=failing_update,
)

def test_figure(self):
"""Test figure"""

code_ex = CodeExercise(outputs=plt.figure())
assert isinstance(code_ex.figure, Figure)
assert code_ex.output.figure is code_ex.figure
assert code_ex.outputs[0] is code_ex.output

0 comments on commit 3f582f6

Please sign in to comment.