{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "b92ad576", "metadata": {}, "outputs": [], "source": [ "#| default_exp: export" ] }, { "cell_type": "code", "execution_count": null, "id": "96525ac4", "metadata": {}, "outputs": [], "source": [ "#| export\n", "import enum,pathlib,re\n", "from typing import IO\n", "\n", "import nbformat" ] }, { "cell_type": "code", "execution_count": null, "id": "eb27bbd6", "metadata": {}, "outputs": [], "source": [ "import os,tempfile\n", "from io import StringIO" ] }, { "cell_type": "markdown", "id": "a682b39f", "metadata": {}, "source": [ "# Exporting Notebooks to Python Files" ] }, { "cell_type": "markdown", "id": "a21a7e2c", "metadata": {}, "source": [ "To get our notebook-driven source code bootstrapped, we'll first need to be able to convert `ipynb` files to `py` files. Without that, we'd be unable to later import code we want to use in one notebook from another notebook because it's not in a plain importable Python file yet.\n", "\n", "To accomplish that, for any given code well, we'll need to know:\n", "- What Python file are we exporting our notebook to?\n", "- Which cells are we exporting to our Python file?\n", "\n", "The approach tools like [nbdev](https://nbdev.fast.ai/) or [nbexport](https://github.com/danhorner/nbexport) take is to have certain directives that determine the code export behavior:\n", "- `#|default_exp: mymodule`: Set the output module for this notebook\n", "- `#|export`: Export this cell to the corresponding Python file" ] }, { "cell_type": "markdown", "id": "3b670537", "metadata": {}, "source": [ "To export a notebook, we'll need to:\n", "1. Read in a ipynb notebook\n", "2. Retrieve all code cells from the notebook\n", "3. Find all cells that should be exported:\n", " - Each notebook must have 1 (and only 1) `#| default_exp`. When a `#| default_exp` is found, it must be a single word e.g. `mymodule` or a dotted path e.g. `mymodules.other_module`. If it is a dotted path, the submodules created should have `__init__.py` files also created alongside them.\n", " - Any cells with an `#| export` explicit directive should be marked for export.\n", "4. Write out each cell sequentially into the Python file, stripping out any directives. When each cell is written out, it should have a comment above it describing the cell ID it came from." ] }, { "cell_type": "markdown", "id": "f1c95690", "metadata": {}, "source": [ "## Reading Notebooks" ] }, { "cell_type": "markdown", "id": "037fb28c", "metadata": {}, "source": [ "To read in `ipynb` files, we can use a library provided Jupyter called `nbformat`. And simply because we'll be reading in this notebook as our working copy while developing this export behavior, let's add a raw cell just so we have an example of all 3 cell types." ] }, { "cell_type": "raw", "id": "d9f3cb10", "metadata": {}, "source": [ "This is a raw cell just to have an example." ] }, { "cell_type": "markdown", "id": "4c0671ed", "metadata": {}, "source": [ "Now, we can read in our current notebook:" ] }, { "cell_type": "code", "execution_count": null, "id": "27202360", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def read_nb(\n", " fp:str|IO[str], # a path or file-like object to read the notebook from\n", " v:int=4, # the notebook format major version to read the notebook as\n", ")->dict:\n", " \"Read in a notebook as a dictionary.\"\n", " return nbformat.read(fp, as_version=v)" ] }, { "cell_type": "code", "execution_count": null, "id": "a689a07b", "metadata": {}, "outputs": [], "source": [ "nb = read_nb('export.ipynb')\n", "assert list(nb.keys()) == ['cells', 'metadata', 'nbformat', 'nbformat_minor']" ] }, { "cell_type": "markdown", "id": "7d133fff", "metadata": {}, "source": [ "We can see that the top-level format of a notebook is:\n", "- `cells`: A list of objects for each cell in the notebook.\n", "- `metadata`: Free-form notebook level metadata used by tools and editors.\n", "- `nbformat`: Notebook format major version\n", "- `nbformat_minor`: Notebook format minor version" ] }, { "cell_type": "markdown", "id": "10a6afcb", "metadata": {}, "source": [ "Within cells, there are generally 3 types of cells:\n", "- `code`\n", "- `markdown`\n", "- `raw`" ] }, { "cell_type": "code", "execution_count": null, "id": "1ee0d459", "metadata": {}, "outputs": [], "source": [ "#| export\n", "class CellType(enum.StrEnum):\n", " \"Possible Jupyter notebook cell types.\"\n", " CODE = 'code'\n", " MD = 'markdown'\n", " RAW = 'raw'" ] }, { "cell_type": "markdown", "id": "46ed0445", "metadata": {}, "source": [ "## Retrieving All Code Cells" ] }, { "cell_type": "code", "execution_count": null, "id": "133ebd88", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def filter_cells(\n", " nb:dict, # A notebook that has been read in via `read_nb`\n", " ct:CellType, # The cells to filter down to\n", ")->list[dict]:\n", " \"Retrieve specific cell types from a notebook.\"\n", " return list(filter(lambda c: c['cell_type'] == ct, nb['cells']))" ] }, { "cell_type": "code", "execution_count": null, "id": "76917bc5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'cell_type': 'code',\n", " 'execution_count': None,\n", " 'id': 'b92ad576',\n", " 'metadata': {},\n", " 'outputs': [],\n", " 'source': '#| default_exp: export'}" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "code_cell = filter_cells(nb, CellType.CODE)[0]\n", "assert list(code_cell.keys()) == ['cell_type', 'execution_count', 'id', 'metadata', 'outputs', 'source']\n", "assert code_cell['cell_type'] == CellType.CODE\n", "code_cell" ] }, { "cell_type": "code", "execution_count": null, "id": "4186fffe", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'cell_type': 'markdown',\n", " 'id': 'a682b39f',\n", " 'metadata': {},\n", " 'source': '# Exporting Notebooks to Python Files'}" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "md_cell = filter_cells(nb, CellType.MD)[0]\n", "assert list(md_cell.keys()) == ['cell_type', 'id', 'metadata', 'source']\n", "assert md_cell['cell_type'] == CellType.MD\n", "md_cell" ] }, { "cell_type": "code", "execution_count": null, "id": "46e56abf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'cell_type': 'raw',\n", " 'id': 'd9f3cb10',\n", " 'metadata': {},\n", " 'source': 'This is a raw cell just to have an example.'}" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw_cell = filter_cells(nb, CellType.RAW)[0]\n", "assert list(raw_cell.keys()) == ['cell_type', 'id', 'metadata', 'source']\n", "assert raw_cell['cell_type'] == CellType.RAW\n", "raw_cell" ] }, { "cell_type": "markdown", "id": "4852b261", "metadata": {}, "source": [ "Within each cell object, they will all have:\n", "- `id`: A unique identifier for that cell.\n", "- `cell_type`: One of the 3 cell type names.\n", "- `metadata`: Free-form metadata for that cell used by various tools/editors.\n", "- `source`: The content of the cell, whether it is prose or code.\n", "\n", "Additionally, `code` cell types will have:\n", "- `outputs`: The stdout/stderr of the cell after the code is executed.\n", "- `execution_count`: The number of times the cell was run." ] }, { "cell_type": "markdown", "id": "6b7469e3", "metadata": {}, "source": [ "## Find Code Cells to Export" ] }, { "cell_type": "markdown", "id": "798249d5", "metadata": {}, "source": [ "### Matching Directives" ] }, { "cell_type": "markdown", "id": "f671cafa", "metadata": {}, "source": [ "A regular expression ran against each line in a cell should allow us to find each directive we support. Since we'll need to re-create this regression expression for a few directives, we'll create a small helper." ] }, { "cell_type": "code", "execution_count": null, "id": "83a898c2", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def _mk_directive_re(\n", " name:str, # The name of the directive\n", " arg_re:str|None=None, # A regex pattern to match an argument for the directive\n", ")->re.Pattern:\n", " \"Compile a regex to match a directive, optionally supporting an argument.\"\n", " arg = rf':\\s*{arg_re}' if arg_re else ''\n", " return re.compile(rf'^\\s*#\\|\\s*{name}{arg}\\s*$')" ] }, { "cell_type": "code", "execution_count": null, "id": "698d769b", "metadata": {}, "outputs": [], "source": [ "_TEST_DIRECTIVE_RE = _mk_directive_re('test')" ] }, { "cell_type": "code", "execution_count": null, "id": "f6d654fd", "metadata": {}, "outputs": [], "source": [ "test_dir_match = [\n", " ' #| test \\t \\n', # Lots of whitespace around each token\n", " '#|test', # No whitespace around each token\n", "]\n", "for line in test_dir_match:\n", " assert _TEST_DIRECTIVE_RE.match(line), f\"No match: {line!r}\"" ] }, { "cell_type": "code", "execution_count": null, "id": "02f06ee6", "metadata": {}, "outputs": [], "source": [ "test_dir_no_match = [\n", " '| test', # Missing #\n", " '# test', # Missing |\n", " '#| testt', # Misspelled directive\n", " '#| test test', # Extra word after directive\n", "]\n", "for line in test_dir_no_match:\n", " assert not _TEST_DIRECTIVE_RE.match(line), f\"Unexpected match: {line!r}\"" ] }, { "cell_type": "code", "execution_count": null, "id": "285e2320", "metadata": {}, "outputs": [], "source": [ "_TEST_ARG_DIRECTIVE_RE = _mk_directive_re('test', arg_re=r'\\w')" ] }, { "cell_type": "code", "execution_count": null, "id": "83fd068c", "metadata": {}, "outputs": [], "source": [ "test_dir_arg_match = [\n", " ' #| test: \\t a \\n', # Lots of whitespace around each token\n", " '#|test:b', # No whitespace around each token\n", "]\n", "for line in test_dir_arg_match:\n", " assert _TEST_ARG_DIRECTIVE_RE.match(line), f\"No match: {line!r}\"" ] }, { "cell_type": "code", "execution_count": null, "id": "dd9945ab", "metadata": {}, "outputs": [], "source": [ "test_arg_dir_no_match = [\n", " '#| test a', # Missing :\n", " '#| test: ', # Missing arg\n", " '#| test: aa', # Invalid arg (should be only 1 letter)\n", "]\n", "for line in test_dir_no_match + test_arg_dir_no_match:\n", " assert not _TEST_ARG_DIRECTIVE_RE.match(line), f\"Unexpected match: {line!r}\"" ] }, { "cell_type": "markdown", "id": "dfd6ac40", "metadata": {}, "source": [ "#### Supporting the default_exp directive" ] }, { "cell_type": "code", "execution_count": null, "id": "ac846aaa", "metadata": {}, "outputs": [], "source": [ "#| export\n", "DIRECTIVE_DEFAULT_EXP_RE = _mk_directive_re('default_exp', arg_re=r'(?P\\w+(\\.\\w+)*)')" ] }, { "cell_type": "code", "execution_count": null, "id": "5f890ed5", "metadata": {}, "outputs": [], "source": [ "matching_cells = [\n", " ('#| default_exp: bootstrap', 'bootstrap'), # Module name\n", " ('#| default_exp: bootstrap.another_module', 'bootstrap.another_module'), # Dotted module path\n", "]\n", "for line, module in matching_cells:\n", " match = DIRECTIVE_DEFAULT_EXP_RE.match(line)\n", " assert match, f\"No match: {line!r}\"\n", " assert match.group('module') == module" ] }, { "cell_type": "code", "execution_count": null, "id": "d1f22bb8", "metadata": {}, "outputs": [], "source": [ "not_matching_cells = [\n", " '#| default_exp: ', # Missing module name\n", " '#| default_exp: !$', # Invalid module name\n", " '#| default_export: bootstrap', # Misspelled directive\n", " '#| default_exp: bootstrap.', # Dot without a following word\n", " '#| default_exp: bootstrap..', # Too many dots without a word in between\n", "]\n", "for line in not_matching_cells:\n", " assert not DIRECTIVE_DEFAULT_EXP_RE.match(line), f\"Unexpected match: {line!r}\"" ] }, { "cell_type": "markdown", "id": "50495b37", "metadata": {}, "source": [ "#### Supporting the export directive" ] }, { "cell_type": "code", "execution_count": null, "id": "5b4e8fe6", "metadata": {}, "outputs": [], "source": [ "#| export\n", "DIRECTIVE_EXPORT_RE = _mk_directive_re('export')" ] }, { "cell_type": "markdown", "id": "a7a778d2", "metadata": {}, "source": [ "#### All Directives" ] }, { "cell_type": "code", "execution_count": null, "id": "828159b2", "metadata": {}, "outputs": [], "source": [ "#| export\n", "DIRECTIVES = [DIRECTIVE_DEFAULT_EXP_RE, DIRECTIVE_EXPORT_RE]" ] }, { "cell_type": "markdown", "id": "21e3d799", "metadata": {}, "source": [ "### Identifying Cells to Export" ] }, { "cell_type": "markdown", "id": "295b4a99", "metadata": {}, "source": [ "So, we should now have all the pieces to determine find all cells we should be exporting and the module we should be exporting to.\n", "\n", "**Algorithm:**\n", "1. Always check if there is a `default_exp` directive. There must be one (and only one) `default_exp` directive.\n", "3. Check for an explicit export directive." ] }, { "cell_type": "code", "execution_count": null, "id": "d327883e", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def has_directive(src:str)->bool:\n", " \"Check whether a line or block of source code has a nb directive.\"\n", " return any(any(r.match(l) for r in DIRECTIVES) for l in src.splitlines())" ] }, { "cell_type": "code", "execution_count": null, "id": "6ea5e776", "metadata": {}, "outputs": [], "source": [ "assert has_directive('#| default_exp: bootstrap')\n", "assert has_directive('#| export')\n", "assert has_directive('max(1,20)\\n#| export')\n", "assert not has_directive('# comment\\nmax(1, 20)\\n')" ] }, { "cell_type": "code", "execution_count": null, "id": "b6f47497", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def strip_directives(src:str|list[str])->str:\n", " \"Remove all nb directives from the source code.\"\n", " src = src if isinstance(src, list) else src.splitlines()\n", " return \"\\n\".join(filter(lambda l: not has_directive(l), src))" ] }, { "cell_type": "code", "execution_count": null, "id": "7acbb8ff", "metadata": {}, "outputs": [], "source": [ "assert strip_directives('#| export\\na=5') == 'a=5'\n", "assert strip_directives('#| default_exp: bootstrap\\na=5') == 'a=5'\n", "assert strip_directives(['a=10', 'a+=10', '#|export']) == 'a=10\\na+=10'" ] }, { "cell_type": "code", "execution_count": null, "id": "d1f32654", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def is_cell_exported(cell:dict)->bool:\n", " \"Check whether the code cell should be exported.\"\n", " return any(DIRECTIVE_EXPORT_RE.match(l) for l in cell['source'].splitlines())" ] }, { "cell_type": "code", "execution_count": null, "id": "64175ad3", "metadata": {}, "outputs": [], "source": [ "mk_cell = lambda s,id=1: dict(id=id,source=s,cell_type=CellType.CODE)\n", "assert is_cell_exported(mk_cell('#| export'))\n", "assert is_cell_exported(mk_cell('import re\\n#| export'))\n", "assert not is_cell_exported(mk_cell('import re'))\n", "assert not is_cell_exported(mk_cell('1+1\\nfrom re import Pattern'))" ] }, { "cell_type": "code", "execution_count": null, "id": "ddc5c9e6", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def get_default_export(cells:list[dict])->str:\n", " \"Retrieve the `default_exp` directive argument from a list of cells.\"\n", " matches = [\n", " m.group('module')\n", " for c in cells\n", " for l in c['source'].splitlines()\n", " if c['cell_type'] == CellType.CODE and (m:=DIRECTIVE_DEFAULT_EXP_RE.match(l))\n", " ]\n", " if len(matches) > 1: raise ValueError('More than one default_exp directive')\n", " if len(matches) == 0: raise ValueError('Missing default_exp directive')\n", " return matches[0]" ] }, { "cell_type": "code", "execution_count": null, "id": "7310f069", "metadata": {}, "outputs": [], "source": [ "assert get_default_export(read_nb('export.ipynb')['cells']) == 'export'\n", "\n", "try: get_default_export([mk_cell('a=5')]); assert False, \"Expected ValueError\"\n", "except ValueError: pass\n", "\n", "try:\n", " get_default_export([mk_cell('#| default_exp: hello'), mk_cell('#| default_exp: test')])\n", " assert False, \"Expected ValueError\"\n", "except ValueError: pass" ] }, { "cell_type": "code", "execution_count": null, "id": "9ce855f0", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def find_cells_to_export(\n", " fp:str|IO[str], # a path or file-like object to read the notebook from\n", ")->tuple[str,list[dict]]: # returns the module name and list of cells to export\n", " \"Find the code cells in an ipynb file to export out.\"\n", " code_cells = filter_cells(read_nb(fp), CellType.CODE)\n", " module = get_default_export(code_cells)\n", " cells = [\n", " {**c, 'source': strip_directives(c['source'])}\n", " for c in code_cells\n", " if is_cell_exported(c)\n", " ]\n", " return module, cells" ] }, { "cell_type": "markdown", "id": "66574d29", "metadata": {}, "source": [ "## Exporting a Notebook" ] }, { "cell_type": "code", "execution_count": null, "id": "3070f710", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def fmt_export_cell(cell:dict)->dict:\n", " \"Format a code cell's source with metadata to prepare for export.\"\n", " cell['source'] = f'# cell: {cell[\"id\"]}\\n' + cell['source'] + '\\n'\n", " return cell" ] }, { "cell_type": "code", "execution_count": null, "id": "e24f633f", "metadata": {}, "outputs": [], "source": [ "assert fmt_export_cell(mk_cell('a=5', id=1))['source'] == '# cell: 1\\na=5\\n'" ] }, { "cell_type": "code", "execution_count": null, "id": "633e256b", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def mk_py_mod_scaffold(\n", " mod:str, # The Python module name or dotted path e.g. mod.mymod.file\n", " src_dir:str|pathlib.Path, # The Python package source code directory scaffold\n", "):\n", " \"Create all intermediary __init__.py files and directories, if any, and the final .py file.\"\n", " if not mod: raise ValueError('The module path cannot be empty')\n", " src_dir = pathlib.Path(src_dir)\n", " mod_file = (src_dir/pathlib.Path(*mod.split('.'))).with_suffix('.py')\n", " mod_file.parent.mkdir(parents=True, exist_ok=True)\n", " mod_file.touch()\n", " pkg_dirs = list(mod_file.parents)\n", " for p in pkg_dirs[:pkg_dirs.index(src_dir) + 1]:\n", " (p / '__init__.py').touch()" ] }, { "cell_type": "code", "execution_count": null, "id": "e19b3289", "metadata": {}, "outputs": [], "source": [ "with tempfile.TemporaryDirectory() as tmp:\n", " try: mk_py_mod_scaffold('', src_dir=tmp); assert False, \"Should raise ValueError\"\n", " except ValueError: pass\n", " assert os.listdir(tmp) == []" ] }, { "cell_type": "code", "execution_count": null, "id": "c3a33968", "metadata": {}, "outputs": [], "source": [ "with tempfile.TemporaryDirectory() as tmp:\n", " mk_py_mod_scaffold('file', src_dir=tmp)\n", " assert os.listdir(tmp) == ['__init__.py', 'file.py']" ] }, { "cell_type": "code", "execution_count": null, "id": "f2e3fcb4", "metadata": {}, "outputs": [], "source": [ "with tempfile.TemporaryDirectory() as tmp:\n", " mk_py_mod_scaffold('mod.file', src_dir=tmp)\n", " assert os.listdir(tmp) == ['__init__.py', 'mod']\n", " assert os.listdir(pathlib.Path(tmp)/'mod') == ['__init__.py', 'file.py']" ] }, { "cell_type": "code", "execution_count": null, "id": "f3ab7819", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def export_nb(\n", " fp:str|IO[str], # The notebook to export\n", " src_dir:str|pathlib.Path='.', # The source directory to export the Python module to.\n", "):\n", " \"Export the exportable code cells to a Python file.\"\n", " module, cells = find_cells_to_export(fp)\n", " mk_py_mod_scaffold(module, src_dir)\n", " exp_path = (pathlib.Path(src_dir)/module).with_suffix('.py')\n", " src = '\\n'.join(\n", " ['# AUTOGENERATED! DO NOT EDIT!\\n'] +\n", " list(map(lambda c: fmt_export_cell(c)['source'], cells))\n", " )\n", " pathlib.Path(exp_path).write_text(src)" ] }, { "cell_type": "code", "execution_count": null, "id": "55f57bee", "metadata": {}, "outputs": [], "source": [ "def mk_nb(cells)->StringIO:\n", " nb = nbformat.v4.new_notebook(cells=cells)\n", " buf = StringIO()\n", " nbformat.write(nb, buf)\n", " buf.seek(0)\n", " return buf" ] }, { "cell_type": "code", "execution_count": null, "id": "31bb0f0b", "metadata": {}, "outputs": [], "source": [ "cells = [\n", " nbformat.v4.new_code_cell('#| default_exp: testmod'),\n", " nbformat.v4.new_code_cell('#| export \\nimport os'),\n", " nbformat.v4.new_code_cell('#| export\\ndef foo(): pass'),\n", " nbformat.v4.new_code_cell('import pathlib'),\n", "]\n", "nb = mk_nb(cells)\n", "src = f\"\"\"# AUTOGENERATED! DO NOT EDIT!\n", "\n", "# cell: {cells[1]['id']}\n", "import os\n", "\n", "# cell: {cells[2]['id']}\n", "def foo(): pass\n", "\"\"\"\n", "with tempfile.TemporaryDirectory() as tmp:\n", " export_nb(nb, src_dir=tmp)\n", " result = pathlib.Path(tmp, 'testmod.py').read_text()\n", " assert result == src, result" ] }, { "cell_type": "markdown", "id": "e7c49602", "metadata": {}, "source": [ "And...we've successfully bootstrapped our notebook! Let's export it out." ] }, { "cell_type": "code", "execution_count": null, "id": "8e1d4096", "metadata": {}, "outputs": [], "source": [ "export_nb('export.ipynb', src_dir='../nb/')" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }