execnb
Install
Install the lightweight shell:
pip install execnb
For the full IPython shell, including its extensions and magics:
pip install 'execnb[ipython]'
For conda, run conda install -c fastai execnb (mamba also works).
How to use
CaptureShell runs cell strings and captures notebook outputs without a notebook file or a Jupyter server. By default it uses IPython if installed, or the built-in lean shell otherwise. To require real IPython, pass use_ipython=True. To select lean explicitly, create CaptureShell(use_ipython=False) before anything imports IPython. All shells in a process use the same backend.
After creating a lean shell, both from IPython.display import Markdown, display and from IPython import display work in cells and imported Python modules. The first imports the callable display; the second imports the module, so use display.Markdown(...) or display.display(...). This makes the display API available without installing full IPython.
Each shell runs cells on its own event loop in a background thread. Call run from a script, notebook, or async server; the cell can use top-level await:
from execnb.shell import *
from fastcore.utils import *
from fastcore.nbio import *
s = CaptureShell()
s.run('1+1')
[{'data': {'text/plain': ['2']},
'metadata': {},
'output_type': 'execute_result',
'execution_count': 1}]
To execute a notebook and save it with outputs filled in, call s.execute:
try:
s.execute('../tests/clean.ipynb', 'tmp.ipynb')
print(read_nb('tmp.ipynb').cells[1].outputs)
finally: Path('tmp.ipynb').unlink()
[{'name': 'stdout', 'output_type': 'stream', 'text': '1\n'}, {'data': {'text/plain': '2'}, 'execution_count': 3, 'metadata': {}, 'output_type': 'execute_result'}]
You can also execute notebooks from the command line with exec_nb:
!exec_nb --help
usage: exec_nb [-h] [--dest (str)] [--exc-stop] [--inject-code (str)] [--inject-path (str)] [--inject-idx (int)]
[--verbose] [--cell-timeout (int)]
src
Execute notebook from `src` and save with outputs to `dest`
positional arguments:
src Notebook path to read from
options:
-h, --help show this help message and exit
--dest (str) Notebook path to write to (default: '')
--exc-stop Stop on exceptions? (default: False)
--inject-code (str) Code to inject into a cell
--inject-path (str) Path to file containing code to inject into a cell
--inject-idx (int) Cell to replace with `inject_code` (default: 0)
--verbose Show stdout/stderr during execution (default: False)
--cell-timeout (int) Seconds before each cell times out (None: no limit)
execnb 0.3.5
Parsing notebook commands
Use execnb.minipy.inputtransformer2 to recognize notebook commands without installing IPython or running a shell. A linter can find the position of a command in the original source:
from execnb.minipy.inputtransformer2 import TransformerManager, TokenTransformBase, make_tokens_by_line
manager = TransformerManager()
source = 'files = !ls'
tokens = make_tokens_by_line(source.splitlines(keepends=True))
matches = [m for cls in manager.token_transformers if (m := cls.find(tokens))]
first = min(matches, key=TokenTransformBase.sortby)
first.start_line, first.start_col
(0, 8)
Line and column numbers start at zero. Each transformer finds its first match or returns None. sortby orders matches by position, then by priority. Retokenize after each edit to find the next command. To translate a whole cell to Python, including cell magics such as %%bash, use manager.transform_cell(source).
Metadata
Release files for execnb 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| execnb-0.4.0.tar.gz | 73.3 kB | Without provenance | Details for execnb-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| execnb-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for execnb-0.4.0-py3-none-any.whl |
Total release size: 127.5 kB
Release files / execnb-0.4.0.tar.gz
| Download URL | execnb-0.4.0.tar.gz |
|---|---|
| Size | 73.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f7a6b0c7873068ee39c73eaaa15666431a8c994046b11c978570d7c03c4135bc
|
|
BLAKE2b-256 checksum How to use checksums |
ea1f48e8bd63eb2aee98eafa2308d3d92fb94aee3c0230691e00e74816996e97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.15
|
Release files / execnb-0.4.0-py3-none-any.whl
| Download URL | execnb-0.4.0-py3-none-any.whl |
|---|---|
| Size | 54.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d396cc9a0f99a7b146bd27d758659373b61f406ef67b22f9b55cdda168ffa3d5
|
|
BLAKE2b-256 checksum How to use checksums |
c9ce3c5a423c2b0f4d16fd15481627c7bfed5189b5a922796f38e5cbc4111702
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.15
|
Security for execnb 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.