diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2026-05-22 03:20:59 +0000 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2026-05-22 03:20:59 +0000 |
| commit | dc2eb9b2dc1ff5a063af42a61219ce123a6a91f6 (patch) | |
| tree | 9becfca02a10c4d179e7f8934fa86340245f2e89 | |
| parent | Tidy export comments and types (diff) | |
| download | nb-dc2eb9b2dc1ff5a063af42a61219ce123a6a91f6.zip | |
Add cli
| -rw-r--r-- | nb/cli.py | 35 | ||||
| -rw-r--r-- | nbs/cli.ipynb | 190 | ||||
| -rw-r--r-- | pyproject.toml | 3 |
3 files changed, 228 insertions, 0 deletions
diff --git a/nb/cli.py b/nb/cli.py new file mode 100644 index 0000000..f9f8088 --- /dev/null +++ b/nb/cli.py @@ -0,0 +1,35 @@ +# AUTOGENERATED! DO NOT EDIT! + +# cell: a62b4fb4 +import argparse,json,sys +from pathlib import Path + +import nbformat + +from nb import export_nb,clean_nb +from nb.export import read_nb + +# cell: 96737c5a +def main(): + p = _get_parser() + args = p.parse_args() + if not args.cmd: + p.print_help() + raise SystemExit(1) + if args.cmd == 'export': + for nb in args.notebooks: + export_nb(nb, args.export_dir) + if args.cmd == 'clean': + for nb in args.notebooks: + nbformat.write(clean_nb(nb), nb) + +# cell: 69b642d9 +def _get_parser(): + parser = argparse.ArgumentParser(prog='nb') + subparsers = parser.add_subparsers(dest='cmd') + export_parser = subparsers.add_parser('export', help='Export ipynb notebooks to Python') + export_parser.add_argument('notebooks', nargs='+', help='Notebook files to export') + export_parser.add_argument('--export-dir', default='.', help='Directory to export Python files to') + clean_parser = subparsers.add_parser('clean', help='Clean notebooks') + clean_parser.add_argument('notebooks', nargs='+', help='Notebook files to clean') + return parser diff --git a/nbs/cli.ipynb b/nbs/cli.ipynb new file mode 100644 index 0000000..3f9930f --- /dev/null +++ b/nbs/cli.ipynb @@ -0,0 +1,190 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "451c8f58", + "metadata": {}, + "outputs": [], + "source": [ + "%load_ext autoreload\n", + "%autoreload 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f290d8fb", + "metadata": {}, + "outputs": [], + "source": [ + "#| default_exp: cli" + ] + }, + { + "cell_type": "markdown", + "id": "0c27d4d0", + "metadata": {}, + "source": [ + "# CLI\n", + "\n", + "> Command-line interface for using `nb`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a62b4fb4", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "import argparse,json,sys\n", + "from pathlib import Path\n", + "\n", + "import nbformat\n", + "\n", + "from nb import export_nb,clean_nb\n", + "from nb.export import read_nb" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12e3162c", + "metadata": {}, + "outputs": [], + "source": [ + "import tempfile\n", + "from nb import test" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "96737c5a", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "def main():\n", + " p = _get_parser()\n", + " args = p.parse_args()\n", + " if not args.cmd:\n", + " p.print_help()\n", + " raise SystemExit(1)\n", + " if args.cmd == 'export':\n", + " for nb in args.notebooks:\n", + " export_nb(nb, args.export_dir)\n", + " if args.cmd == 'clean':\n", + " for nb in args.notebooks:\n", + " nbformat.write(clean_nb(nb), nb)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "69b642d9", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "def _get_parser():\n", + " parser = argparse.ArgumentParser(prog='nb')\n", + " subparsers = parser.add_subparsers(dest='cmd')\n", + " export_parser = subparsers.add_parser('export', help='Export ipynb notebooks to Python')\n", + " export_parser.add_argument('notebooks', nargs='+', help='Notebook files to export')\n", + " export_parser.add_argument('--export-dir', default='.', help='Directory to export Python files to')\n", + " clean_parser = subparsers.add_parser('clean', help='Clean notebooks')\n", + " clean_parser.add_argument('notebooks', nargs='+', help='Notebook files to clean')\n", + " return parser" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7b95c2d4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "usage: nb [-h] {export,clean} ...\n", + "\n", + "positional arguments:\n", + " {export,clean}\n", + " export Export ipynb notebooks to Python\n", + " clean Clean notebooks\n", + "\n", + "options:\n", + " -h, --help show this help message and exit\n" + ] + } + ], + "source": [ + "sys.argv = ['script']\n", + "with test.raises(exc=SystemExit, contains='1'): main()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5da41c17", + "metadata": {}, + "outputs": [], + "source": [ + "with tempfile.TemporaryDirectory() as tmp:\n", + " nb = nbformat.v4.new_notebook()\n", + " nb.cells = [\n", + " nbformat.v4.new_code_cell('#| default_exp: mymod'),\n", + " nbformat.v4.new_code_cell('#| export\\ndef hello(): return \"hi\"'),\n", + " ]\n", + " nb_path = tmp + '/tmp.ipynb'\n", + " with open(nb_path, mode='w+') as f: nbformat.write(nb, f)\n", + " sys.argv = ['script', 'export', '--export-dir', tmp, nb_path]\n", + " main()\n", + " test.equal(\n", + " Path(tmp + '/mymod.py').read_text(),\n", + " f'# AUTOGENERATED! DO NOT EDIT!\\n\\n# cell: {nb.cells[1]['id']}\\ndef hello(): return \"hi\"\\n',\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f361af2b", + "metadata": {}, + "outputs": [], + "source": [ + "with tempfile.TemporaryDirectory() as tmp:\n", + " nb = nbformat.v4.new_notebook()\n", + " nb.metadata['bogus'] = 'should be removed'\n", + " nb.cells = [\n", + " nbformat.v4.new_code_cell('#| default_exp: mymod'),\n", + " nbformat.v4.new_code_cell('print(\"hi\")', execution_count=42),\n", + " ]\n", + " nb_path = tmp + '/tmp.ipynb'\n", + " with open(nb_path, mode='w+') as f: nbformat.write(nb, f)\n", + " sys.argv = ['script', 'clean', nb_path]\n", + " main()\n", + " cleaned = read_nb(nb_path)\n", + " test.equal(cleaned.cells[1].execution_count, None)\n", + " test.equal('bogus' not in cleaned.metadata, True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c086e4b4", + "metadata": {}, + "outputs": [], + "source": [ + "export_nb('cli.ipynb', src_dir='../nb')" + ] + } + ], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/pyproject.toml b/pyproject.toml index 0e87d5f..7783a3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,9 @@ classifiers = [ "Programming Language :: Python :: 3", ] +[project.scripts] +nb = "nb.cli:main" + [dependency-groups] dev = ["jupyter"] |