Skip to main content

puzzle-widget

A Jupyter widget where students drag scrambled code lines into the correct order, built on anywidget.

Tag a cell %%puzzle <result>; every line below it becomes a draggable row, shuffled so the student starts from a scrambled order. Drag (or focus a row and press the arrow keys) to reorder them -- the widget re-runs the current order after every move and shows a checkmark once it produces <result>.

import puzzle_widget  # registers the %%puzzle cell magic
%%puzzle 15
a = 10
b = 5
a + b

Write the lines in whatever order reads best -- the correct one included: the widget shuffles them before showing them, and never starts from an order that already produces <result>. Dragging them back into a working order produces 15 and the widget shows "✓ Correct!".

The shuffle is seeded, so a given puzzle always starts in the same scrambled order -- every student sees the same starting point, and a puzzle you tested looks the same when you teach it. Pass seed= to PuzzleWidget for a different one.

Installation

# pixi
pixi workspace channel add munch-group
pixi add puzzle-widget

# conda
conda install -c munch-group puzzle-widget

# pip
pip install puzzle-widget

Writing a puzzle

  • <result> is a Python literal (a number, string, list, ...), parsed with ast.literal_eval -- not an arbitrary expression, since the puzzle runs in its own isolated namespace, not the notebook's.
  • Every line must be a complete, independent, non-indented statement. A puzzle can't contain multi-line blocks (if/for/def/...) that need to stay attached to each other, since any permutation of the lines has to at least parse -- only the values it produces should depend on the order.
  • The result is the value of the last line, which must be a bare expression (a + b, or a variable's name on its own) -- exactly like a notebook cell only shows output for a trailing expression, not for an assignment. If the puzzle's final step is total = a + b, add a total line after it so there's a value to check.

You can also build a widget directly from a list of lines:

from puzzle_widget import PuzzleWidget

PuzzleWidget(["a = 10", "b = 5", "a + b"], expected=15)

Documentation

Full docs live at munch-group.org/puzzle-widget.

Development

This repo is managed with pixi:

pixi run install-dev   # editable install into the pixi environment
pixi run test          # run the pytest suite
pixi run docs          # execute the documentation notebooks
pixi run api           # build the quartodoc API reference

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

puzzle_widget-0.1.20.tar.gz (132.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

puzzle_widget-0.1.20-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file puzzle_widget-0.1.20.tar.gz.

File metadata

  • Download URL: puzzle_widget-0.1.20.tar.gz
  • Upload date:
  • Size: 132.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for puzzle_widget-0.1.20.tar.gz
Algorithm Hash digest
SHA256 bb3bc77bb68fbfccbbdc300097b6dff6884cb076875c8522d8c6dcc79af5739a
MD5 7c8bcdf987149e430a8f1e1ae9f26959
BLAKE2b-256 e1cd3a0189e330fde8da669d71d476f46c9fefa2d2286b7e6385730b1d0af0b6

See more details on using hashes here.

File details

Details for the file puzzle_widget-0.1.20-py3-none-any.whl.

File metadata

  • Download URL: puzzle_widget-0.1.20-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for puzzle_widget-0.1.20-py3-none-any.whl
Algorithm Hash digest
SHA256 4a9545c3dbe47d56809b4ad689fa0fd2b5e168122000543c92c78997fba78f3f
MD5 fd6d66eeefd499f4e5904592021dbe20
BLAKE2b-256 3a920e2bfa8bd85067bfdea2069980a5f80bf379254c0acaeaabb4bc0c1b590d

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.21

2 files

This release

0.1.20 This release

2 files

0.1.19

2 files

0.1.18

2 files

0.1.16

2 files

0.1.14

2 files

0.1.12

2 files

0.1.10

2 files

0.1.8

2 files

0.1.6

2 files

0.1.4

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page