Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Book
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MUDE
Book
Commits
5a4975d5
Commit
5a4975d5
authored
1 year ago
by
sheepmax
Browse files
Options
Downloads
Patches
Plain Diff
Basic exercise checking example
parent
c2ce207f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!51
Blank book
,
!34
Exercise checking
Pipeline
#205531
passed
1 year ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
book/_toc.yml
+1
-0
1 addition, 0 deletions
book/_toc.yml
book/cookbook/exercise_checking_dont_execute.ipynb
+91
-0
91 additions, 0 deletions
book/cookbook/exercise_checking_dont_execute.ipynb
with
92 additions
and
0 deletions
book/_toc.yml
+
1
−
0
View file @
5a4975d5
...
...
@@ -66,6 +66,7 @@ parts:
-
file
:
cookbook/benchmarks/benchmark_tags_dont_execute.ipynb
-
file
:
cookbook/benchmarks/benchmark_load_dont_execute.ipynb
-
file
:
cookbook/widgets_dont_execute.ipynb
-
file
:
cookbook/exercise_checking_dont_execute.ipynb
-
caption
:
Old Material
chapters
:
-
file
:
old/blank
...
...
This diff is collapsed.
Click to expand it.
book/cookbook/exercise_checking_dont_execute.ipynb
0 → 100644
+
91
−
0
View file @
5a4975d5
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise checking with interactive elements\n",
"\n",
"Exercise checking can be intuitively incorporated into Python using tags. These examples show different approaches to achieving this goal. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"%pip install ipywidgets\n",
"import ipywidgets as widgets\n",
"from IPython.display import display\n",
"import operator\n",
"\n",
"def check_answer(variable_name, expected, comparison = operator.eq):\n",
" output = widgets.Output()\n",
" button = widgets.Button(description=\"Check answer\")\n",
" def _inner_check(button):\n",
" with output:\n",
" if comparison(globals()[variable_name], expected):\n",
" output.outputs = [{'name': 'stdout', 'text': 'Correct!', 'output_type': 'stream'}]\n",
" else:\n",
" output.outputs = [{'name': 'stdout', 'text': 'Incorrect!', 'output_type': 'stream'}]\n",
" button.on_click(_inner_check)\n",
" display(button, output)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-init"
]
},
"outputs": [],
"source": [
"# This example has the user type in the answer as a Python variable, but they need to run the cell to update the answer checked!\n",
"pi = 3.14"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"import math\n",
"check_answer(\"pi\", 3.14, math.isclose)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
# Exercise checking with interactive elements
Exercise checking can be intuitively incorporated into Python using tags. These examples show different approaches to achieving this goal.
%% Cell type:code id: tags:thebe-remove-input-init
```
python
%
pip
install
ipywidgets
import
ipywidgets
as
widgets
from
IPython.display
import
display
import
operator
def
check_answer
(
variable_name
,
expected
,
comparison
=
operator
.
eq
):
output
=
widgets
.
Output
()
button
=
widgets
.
Button
(
description
=
"
Check answer
"
)
def
_inner_check
(
button
):
with
output
:
if
comparison
(
globals
()[
variable_name
],
expected
):
output
.
outputs
=
[{
'
name
'
:
'
stdout
'
,
'
text
'
:
'
Correct!
'
,
'
output_type
'
:
'
stream
'
}]
else
:
output
.
outputs
=
[{
'
name
'
:
'
stdout
'
,
'
text
'
:
'
Incorrect!
'
,
'
output_type
'
:
'
stream
'
}]
button
.
on_click
(
_inner_check
)
display
(
button
,
output
)
```
%% Cell type:code id: tags:thebe-init
```
python
# This example has the user type in the answer as a Python variable, but they need to run the cell to update the answer checked!
pi
=
3.14
```
%% Cell type:code id: tags:thebe-remove-input-init
```
python
import
math
check_answer
(
"
pi
"
,
3.14
,
math
.
isclose
)
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment