Skip to main content

Jupyter widget that runs a cell in a fresh, isolated namespace (the %%exercise cell magic)

Project description

script-widget

A Jupyter cell magic that runs a cell in a fresh, isolated namespace and shows everything it produced -- stdout, stderr, rich display output, any exception -- in a shaded box below the cell, built on anywidget.

import script_widget  # registers the %%exercise cell magic
%%exercise
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])

Nothing the cell defines or imports persists past that one run, and nothing the notebook has already defined is visible to it -- except through the cell's own import/from ... import statements. Because Python caches loaded modules in sys.modules, such an import is essentially free and hands back the exact same live object the notebook already has (the same plt, with the same open figures; the same model, with the same weights) -- nothing is reloaded or reinitialized, and no kernel restart happens.

Installation

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

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

# pip
pip install script-widget

Writing a %%exercise cell

  • Anything the cell needs -- a library, a model, a helper function -- must be imported by the cell itself. Plain notebook variables with no import path (e.g. a dataframe built inline two cells up) aren't visible.
  • A trailing bare expression is displayed, exactly like a normal cell (a + b, or a variable's name on its own); an assignment as the last line still runs, but produces no comparable output.

Documentation

Full docs live at munch-group.org/script-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

script_widget-0.1.18.tar.gz (438.8 kB view details)

Uploaded Source

Built Distribution

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

script_widget-0.1.18-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file script_widget-0.1.18.tar.gz.

File metadata

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

File hashes

Hashes for script_widget-0.1.18.tar.gz
Algorithm Hash digest
SHA256 5ebce61462d0b31a7d46dc7494e8db7d7878b50719e433dc5a248df2a238a72c
MD5 e67af3fce08f614508d4e329481285e1
BLAKE2b-256 5f506dcfc62841f6000384001a1f36a3b2e845f6fb2b59397b5db5510c8b5fa9

See more details on using hashes here.

File details

Details for the file script_widget-0.1.18-py3-none-any.whl.

File metadata

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

File hashes

Hashes for script_widget-0.1.18-py3-none-any.whl
Algorithm Hash digest
SHA256 a916536dfb86db3370bc8d4bb6a8ea7b7dda0cde66ee290970798883f0a448e2
MD5 90d72eb75f6795860157ecdff5f4a128
BLAKE2b-256 2c58e647cec9339fa8c13f7ec5e8e6ac99f1fc0cb495ebb70120dfaa4d8bdef5

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