VSCODE 1.4.1版| Python v3.8.2 | Jupyter Notebook v。6.0.3
我正在尝试使用jupyter笔记本构建一门课程,其中我有兴趣在笔记本课程之后的某个时间点进行作业。我希望笔记本在学生运行时与他们互动。为此,我使用了VSCODE。
感兴趣的人可以加入GitHub
上的私人项目
我已经使用ipywidgets使我的笔记本进行交互。
MCQ定义单元格
##Basic mcq
from ipywidgets import widgets, Layout, Box, GridspecLayout
def create_multipleChoice_widget(description, options, correct_answer, hint):
if correct_answer not in options:
options.append(correct_answer)
correct_answer_index = options.index(correct_answer)
radio_options = [(words, i) for i, words in enumerate(options)]
alternativ = widgets.RadioButtons(
options = radio_options,
description = '',
disabled = False,
indent = False,
align = 'center',
)
description_out = widgets.Output(layout=Layout(width='auto'))
with description_out:
print(description)
feedback_out = widgets.Output()
def check_selection(b):
a = int(alternativ.value)
if a==correct_answer_index:
s = '\x1b[6;30;42m' + "correct" + '\x1b[0m' +"\n"
else:
s = '\x1b[5;30;41m' + "try again" + '\x1b[0m' +"\n"
with feedback_out:
feedback_out.clear_output()
print(s)
return
check = widgets.Button(description="check")
check.on_click(check_selection)
hint_out = widgets.Output()
def hint_selection(b):
with hint_out:
print(hint)
with feedback_out:
feedback_out.clear_output()
print(hint)
hintbutton = widgets.Button(description="hint")
hintbutton.on_click(hint_selection)
return widgets.VBox([description_out,
alternativ,
widgets.HBox([hintbutton, check]), feedback_out],
layout=Layout(display='flex',
flex_flow='column',
align_items='stretch',
width='auto'))
问题单元格:
test = create_multipleChoice_widget('1.What version of Python is used throughout this course?',['1.x','2.x','2.7','3.x'],'3.x','[hint]: Refer Lesson Again')
输出单元格: