1- from typing import Optional , Union , Any , Dict , List
21import random
2+ from typing import Any , Dict , List , Optional , Union
3+
4+ from ipywidgets import (
5+ HTML ,
6+ HBox ,
7+ HTMLMath ,
8+ Layout ,
9+ Output ,
10+ RadioButtons ,
11+ SelectMultiple ,
12+ VBox ,
13+ )
314
4- from ipywidgets import HTML , HBox , HTMLMath , Layout , Output , SelectMultiple , RadioButtons , VBox
515from .._utils import Formatter
616from ..css_style import CssStyle
717from ..cue import SaveCueBox , SaveResetCueButton
1121class MultipleChoiceExercise (VBox , ExerciseWidget ):
1222 """
1323 :param options:
14- Either a dict or a list. If a dict is provided, the widget will display the dictionary’s value
15- but save its key to the registry.
16-
17- :param key:
24+ Either a dict or a list. If a dict is provided, the widget will display the
25+ dictionary’s value but save its key to the registry.
26+
27+ :param key:
1828 Unique key for the exercise.
1929
2030 :param description:
21- A string describing the exercise that will be put into an HTML widget above the exercise.
31+ A string describing the exercise that will be put into an HTML widget above
32+ the exercise.
2233
2334 :param title:
2435 A title for the exercise. If not provided, the key is used.
@@ -27,10 +38,10 @@ class MultipleChoiceExercise(VBox, ExerciseWidget):
2738 An exercise registry that is used to register the answers to save them later.
2839 If specified the save and load panel will appear.
2940
30- :param allow_multiple:
41+ :param allow_multiple:
3142 Whether multiple selections are allowed.
3243
33- :param randomize_order:
44+ :param randomize_order:
3445 Whether to randomize order of options.
3546 """
3647
@@ -100,26 +111,39 @@ def __init__(
100111 self ._load_button = None
101112 self ._button_panel = None
102113 else :
103- self ._cue_selection = SaveCueBox (self ._selection_widget , "value" , self ._selection_widget , cued = True )
114+ self ._cue_selection = SaveCueBox (
115+ self ._selection_widget , "value" , self ._selection_widget , cued = True
116+ )
104117 self ._save_button = SaveResetCueButton (
105118 self ._cue_selection ,
106119 self ._on_click_save_action ,
107- disable_on_successful_action = kwargs .pop ("disable_save_button_on_successful_action" , False ),
108- disable_during_action = kwargs .pop ("disable_save_button_during_action" , True ),
120+ disable_on_successful_action = kwargs .pop (
121+ "disable_save_button_on_successful_action" , False
122+ ),
123+ disable_during_action = kwargs .pop (
124+ "disable_save_button_during_action" , True
125+ ),
109126 description = "Save answer" ,
110127 button_tooltip = "Saves answer to the loaded file" ,
111128 )
112129 self ._load_button = SaveResetCueButton (
113130 self ._cue_selection ,
114131 self ._on_click_load_action ,
115- disable_on_successful_action = kwargs .pop ("disable_load_button_on_successful_action" , False ),
116- disable_during_action = kwargs .pop ("disable_load_button_during_action" , True ),
132+ disable_on_successful_action = kwargs .pop (
133+ "disable_load_button_on_successful_action" , False
134+ ),
135+ disable_during_action = kwargs .pop (
136+ "disable_load_button_during_action" , True
137+ ),
117138 description = "Load answer" ,
118139 button_tooltip = "Loads answer from the loaded file" ,
119140 )
120141 self ._save_button .set_cue_widgets ([self ._cue_selection , self ._load_button ])
121142 self ._load_button .set_cue_widgets ([self ._cue_selection , self ._save_button ])
122- self ._button_panel = HBox ([self ._save_button , self ._load_button ], layout = Layout (justify_content = "flex-end" ))
143+ self ._button_panel = HBox (
144+ [self ._save_button , self ._load_button ],
145+ layout = Layout (justify_content = "flex-end" ),
146+ )
123147
124148 self ._output = Output ()
125149
@@ -154,7 +178,7 @@ def answer(self) -> tuple:
154178 if self .allow_multiple :
155179 return tuple (self ._selection_widget .value )
156180 else :
157- return (self ._selection_widget .value , )
181+ return (self ._selection_widget .value ,)
158182
159183 @answer .setter
160184 def answer (self , answer ) -> None :
@@ -164,7 +188,7 @@ def answer(self, answer) -> None:
164188 self ._save_button .unobserve_widgets ()
165189 if self ._load_button is not None :
166190 self ._load_button .unobserve_widgets ()
167-
191+
168192 if len (answer ) == 1 :
169193 self ._selection_widget .value = answer [0 ]
170194 else :
@@ -238,4 +262,12 @@ def handle_load_result(self, result: Union[str, Exception]) -> None:
238262 self ._load_button .cued = False
239263 if self ._save_button is not None :
240264 self ._save_button .cued = False
241- print (Formatter .color_success_message (result ))
265+ print (Formatter .color_success_message (result ))
266+
267+ def check_correct_answers (self , * correct_answers ) -> bool :
268+ if isinstance (correct_answers [0 ], (list , tuple , set )):
269+ correct_answers = correct_answers [0 ]
270+ if self .allow_multiple :
271+ return sorted (self .answer ) == sorted (correct_answers )
272+ else :
273+ return self .answer [0 ] == correct_answers [0 ]
0 commit comments