{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "451c8f58", "metadata": {}, "outputs": [], "source": [ "%%capture\n", "%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\n", "from pathlib import Path\n", "\n", "import nbformat\n", "\n", "from nb import export_nb,clean_nb\n", "from nb.config import get_config,Config" ] }, { "cell_type": "code", "execution_count": null, "id": "12e3162c", "metadata": {}, "outputs": [], "source": [ "import contextlib,json,sys,tempfile,uuid\n", "from nb.core import read_nb\n", "from nb import test" ] }, { "cell_type": "code", "execution_count": null, "id": "96737c5a", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def main():\n", " cfg = get_config()\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", " nbs = _get_nbs(args.notebooks, cfg)\n", " exp_dir = args.export_dir or cfg.export_dir or '.'\n", " if args.clean:\n", " for nb in nbs: nbformat.write(clean_nb(nb), nb)\n", " for nb in nbs: export_nb(nb, exp_dir)\n", " if args.cmd == 'clean':\n", " nbs = _get_nbs(args.notebooks, cfg)\n", " for nb in nbs: nbformat.write(clean_nb(nb), nb)" ] }, { "cell_type": "code", "execution_count": null, "id": "09b8407e", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def _get_nbs(nbs:list[str], cfg:Config):\n", " if nbs: return nbs\n", " if cfg.nb_dir: return list(Path(cfg.nb_dir).glob('*.ipynb'))\n", " raise SystemExit('No notebooks specified and nb_dir not set in config')" ] }, { "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=None, help='Directory to export Python files to')\n", " export_parser.add_argument('--clean', action='store_true', help='Clean the notebooks before exporting')\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": "ce775b53", "metadata": {}, "outputs": [], "source": [ "def mk_nb(nb_path, **nb_kwargs):\n", " nb = nbformat.v4.new_notebook(**nb_kwargs)\n", " with open(nb_path, mode='w+') as f: nbformat.write(nb, f)\n", " return nb" ] }, { "cell_type": "code", "execution_count": null, "id": "19889cd7", "metadata": {}, "outputs": [], "source": [ "def mk_nb_cfg(cfg_dir:str|Path, nb_dir:str|Path='', export_dir:str|Path=''):\n", " pyproj = Path(cfg_dir)/'pyproject.toml'\n", " lines = ['[tool.nb]']\n", " if nb_dir: lines.append(f'nb_dir=\"{str(nb_dir)}\"')\n", " if export_dir: lines.append(f'export_dir=\"{str(export_dir)}\"')\n", " pyproj.write_text('\\n'.join(lines))\n", " return pyproj" ] }, { "cell_type": "code", "execution_count": null, "id": "4f17e8d6", "metadata": {}, "outputs": [], "source": [ "def run_cli(*args):\n", " sys.argv = ['nb', *args]\n", " main()" ] }, { "cell_type": "code", "execution_count": null, "id": "c93ffa7c", "metadata": {}, "outputs": [], "source": [ "@contextlib.contextmanager\n", "def tmp_nb(cells, nbkwargs={}):\n", " with tempfile.TemporaryDirectory() as tmp:\n", " nb_path = tmp + f'/tmp-{uuid.uuid4().hex}.ipynb'\n", " nb = mk_nb(nb_path, cells=cells, **nbkwargs)\n", " with contextlib.chdir(tmp): yield (tmp, nb_path)" ] }, { "cell_type": "code", "execution_count": null, "id": "25e8195c", "metadata": {}, "outputs": [], "source": [ "def mk_nb_src(cells):\n", " cells = cells if isinstance(cells, list) else [cells]\n", " lines = ['# AUTOGENERATED! DO NOT EDIT!\\n']\n", " for cell in cells:\n", " if cell['source'].startswith('#| export'):\n", " lines.append(f'# cell: {cell[\"id\"]}')\n", " lines.extend([l for l in cell['source'].splitlines() if not l.startswith('#|')])\n", " lines.append('')\n", " return '\\n'.join(lines)" ] }, { "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": [ "with test.env(NO_COLOR='1'), test.raises(exc=SystemExit, contains='1'): run_cli()" ] }, { "cell_type": "code", "execution_count": null, "id": "2a23b5af", "metadata": {}, "outputs": [], "source": [ "CLEAN_VALID_EXPORTED_CELLS = [\n", " nbformat.v4.new_code_cell(f'#| default_exp: tmp'),\n", " nbformat.v4.new_code_cell(f'#| export\\ndef hello(): return \"hi\"'),\n", " nbformat.v4.new_code_cell('def goodbye(): return \"goodbye\"'),\n", "]" ] }, { "cell_type": "code", "execution_count": null, "id": "0e96c23d", "metadata": {}, "outputs": [], "source": [ "err_msg = 'No notebooks specified and nb_dir not set in config'\n", "for subcmd in ('export', 'clean'):\n", " with tmp_nb(CLEAN_VALID_EXPORTED_CELLS), test.raises(exc=SystemExit, contains=err_msg):\n", " run_cli(subcmd)" ] }, { "cell_type": "code", "execution_count": null, "id": "fc918ca3", "metadata": {}, "outputs": [], "source": [ "with tmp_nb(CLEAN_VALID_EXPORTED_CELLS) as (tmp, nb_path):\n", " run_cli('export', '--export-dir', tmp, nb_path)\n", " test.equal((Path(tmp)/'tmp.py').read_text(), mk_nb_src(CLEAN_VALID_EXPORTED_CELLS))" ] }, { "cell_type": "code", "execution_count": null, "id": "422d92ad", "metadata": {}, "outputs": [], "source": [ "with tmp_nb(CLEAN_VALID_EXPORTED_CELLS) as (tmp, nb_path):\n", " mk_nb_cfg(tmp, nb_dir=tmp, export_dir=tmp)\n", " run_cli('export')\n", " test.equal((Path(tmp)/'tmp.py').read_text(), mk_nb_src(CLEAN_VALID_EXPORTED_CELLS))" ] }, { "cell_type": "code", "execution_count": null, "id": "e6bbc5ad", "metadata": {}, "outputs": [], "source": [ "DIRTY_VALID_EXPORTED_CELLS = [\n", " nbformat.v4.new_code_cell('#| default_exp: tmp'),\n", " nbformat.v4.new_code_cell('#| export\\nprint(\"hi\")', execution_count=42),\n", "]" ] }, { "cell_type": "code", "execution_count": null, "id": "7fedfaff", "metadata": {}, "outputs": [], "source": [ "with tmp_nb(DIRTY_VALID_EXPORTED_CELLS) as (tmp, nb_path):\n", " mk_nb_cfg(tmp, nb_dir=tmp, export_dir=tmp)\n", " run_cli('export', '--clean')\n", " test.equal((Path(tmp)/'tmp.py').read_text(), mk_nb_src(DIRTY_VALID_EXPORTED_CELLS))\n", " with open(nb_path) as f: cleaned_nb = nbformat.v4.reads(f.read())\n", " test.equal(cleaned_nb.cells[1].execution_count, None)" ] }, { "cell_type": "code", "execution_count": null, "id": "f361af2b", "metadata": {}, "outputs": [], "source": [ "with tmp_nb(DIRTY_VALID_EXPORTED_CELLS, nbkwargs={'metadata': {'bogus': 'should be removed'}}) as (tmp, nb_path):\n", " run_cli('clean', nb_path)\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": "61f3911b", "metadata": {}, "outputs": [], "source": [ "with tmp_nb(DIRTY_VALID_EXPORTED_CELLS, nbkwargs={'metadata': {'bogus': 'should be removed'}}) as (tmp, nb_path):\n", " mk_nb_cfg(tmp, nb_dir=tmp, export_dir=tmp)\n", " run_cli('clean')\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": { "kernelspec": { "display_name": "python3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }