Skip to main content

Run a Jupyter notebook into your script's namespace with full IPython magic support

Project description

nb2ns

CI

Run a Jupyter notebook into your script's namespace — with full IPython magic support.

Install

pip install nb2ns

Usage

import nb2ns

# Variables, functions, and imports from the notebook land in your script's globals.
nb2ns.run_notebook("analysis.ipynb")
print(result)  # defined in the notebook

# Or run into an explicit dict.
ns = {}
nb2ns.run_notebook("analysis.ipynb", ns=ns)
print(ns["result"])

Magic support

Because nb2ns uses IPython's InteractiveShell internally, all built-in IPython magics work out of the box. The table below covers the most commonly used ones, with notes on what you can observe in the namespace after the cell runs.

Cell magics (%%)

Magic Supported Test Notes
%%time Yes test_cell_magic_time Prints timing; cell body executes normally, variables persist
%%timeit Yes test_cell_magic_timeit Prints timing stats; body runs in isolation, variables do not persist
%%capture <var> Yes test_cell_magic_capture Stdout/stderr stored in <var>.stdout / <var>.stderr in the namespace
%%writefile <path> Yes test_cell_magic_writefile Writes cell body to <path>; respects chdir=True
%%html Yes test_cell_magic_html Display-only; no namespace side effects
%%markdown Yes test_cell_magic_markdown Display-only; no namespace side effects
%%svg Yes test_cell_magic_svg Display-only; no namespace side effects
%%latex Yes test_cell_magic_latex Display-only; no namespace side effects
%%javascript / %%js Yes test_cell_magic_javascript Display-only; no namespace side effects
%%bash / %%sh Platform Requires bash on PATH; works on Linux/macOS, may fail on Windows
%%cmd Windows only Windows cmd.exe shell; not available on Linux/macOS
%%script <cmd> Yes Runs cell body via the given interpreter
%%python / %%python3 Yes Runs body in a subprocess; variables do not persist
%%capture (no var) Yes Silently swallows output; nothing added to namespace

Line magics (%)

Magic Supported Test Notes
%run <file> Yes test_line_magic_run Executes .py or .ipynb; definitions land in the shared namespace
%timeit [-o] <stmt> Yes test_line_magic_timeit With -o, returns a TimeitResult that can be assigned
%who_ls Yes test_line_magic_who_ls Returns list of user-defined names currently in the namespace
%env VAR=value Yes test_line_magic_env Sets environment variable; visible to os.environ immediately
%set_env VAR=value Yes test_line_magic_set_env Alternative env-var setter; same effect as %env VAR=value
%pwd Yes test_line_magic_pwd Returns current working directory as a string
%load_ext <ext> Yes test_line_magic_load_ext Loads an IPython extension (e.g. autoreload)
%reload_ext <ext> Yes test_line_magic_ext_lifecycle Reloads a previously loaded extension
%unload_ext <ext> Yes test_line_magic_ext_lifecycle Unloads a previously loaded extension
%sx <cmd> / %system Yes test_line_magic_sx Explicit shell-capture magic; identical semantics to !<cmd>
%alias_magic <name> <magic> Yes test_line_magic_alias_magic Creates a new magic alias; alias is immediately callable
%xdel <var> Yes test_line_magic_xdel Deletes a variable and clears all references to it
%reset_selective -f <regex> Yes test_line_magic_reset_selective Removes variables matching regex; leaves non-matching ones intact
%psearch <pattern> Yes test_line_magic_psearch Searches namespace by wildcard; display-only
%xmode <mode> Yes test_line_magic_xmode Switches exception display mode (Minimal / Context / Verbose)
%matplotlib <backend> Yes Sets matplotlib backend; requires matplotlib installed
%cd <path> Yes Changes the shell's working directory (persists after the cell)
%pinfo <obj> / %pinfo2 Yes Prints docstring / source; no namespace side effects
!<cmd> (shell escape) Yes test_shell_escape Runs shell command; assign to capture output as a list of lines

How it works

nb2ns wires your script's globals() dict directly into an IPython InteractiveShell and executes each code cell with shell.run_cell(). This is the same execution path Jupyter itself uses, so line magics (%run, %matplotlib), cell magics (%%time, %%capture), and shell escapes (!ls) all work exactly as they would inside a notebook. No subprocess, no separate kernel — the notebook runs in the same Python process as your script, sharing the same namespace.

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

nb2ns-0.1.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

nb2ns-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file nb2ns-0.1.0.tar.gz.

File metadata

  • Download URL: nb2ns-0.1.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for nb2ns-0.1.0.tar.gz
Algorithm Hash digest
SHA256 62c408d3481d9eb824b6fe0023df7f76a6fa7f76c9664d300ae88feac17a4a3a
MD5 34295cafcd0d5064cd7c3bb710ffc934
BLAKE2b-256 0218640d48cfa0e7fa64fa960a2db2347614faa9f2a13b2c4db7f6073067e539

See more details on using hashes here.

File details

Details for the file nb2ns-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nb2ns-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for nb2ns-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 143a97c64feb17e983f02709bf865064cb34afba45ca1a8fdca762dbfc1cfe16
MD5 dac0d7b45d2e4bf1a3398a35f8261874
BLAKE2b-256 7d07a01f08464682ded439c643fc6b80160f908de967aeccdcb063567df50f79

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