Skip to main content

Jupyter widget where students drag scrambled code lines into the correct order (the %%puzzle cell magic)

Project description

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. 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 + b
b = 5
a = 10

The student sees the three lines in that scrambled order; dragging a = 10 and b = 5 above a + b produces 15 and the widget shows "✓ Correct!".

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 + b", "b = 5", "a = 10"], 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

Project details


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.12.tar.gz (120.3 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.12-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for puzzle_widget-0.1.12.tar.gz
Algorithm Hash digest
SHA256 b603a33e65f422913668d2c2caf3983c860d42d1d30d02b51492648a6f74d10e
MD5 4076864875efc7a12c33242aa97bfbc0
BLAKE2b-256 416b170455a7f698f8abc8b2e4d707db39c277834a72e12f6deb62a8692f0fb6

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for puzzle_widget-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 affd65ec862b850c537d1ebe1b66a0dc20fbbfd075bd9a91b31ce52b4b896b53
MD5 081652cf3d3eb980bbd05ebcea3a1505
BLAKE2b-256 a62589076aa5cd9a9ba0321ee33a11392052848055afcfb1c4a1044a2959f732

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page