{ "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,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 }