aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-05-21 01:16:47 +0000
committerEugene Triguba <hello@eugenetriguba.com>2026-05-21 01:28:33 +0000
commitb66a066f74224263f8fab79cb2d8df38baccfa31 (patch)
treeb89d44a12c404dacf638c1d59f12ca2c073aefd5
parentAdd 'export_nb' to pkg __init__ (diff)
downloadnb-b66a066f74224263f8fab79cb2d8df38baccfa31.zip
Remove autoexport of imports and remove noexport directive
-rw-r--r--nb/export.py22
-rw-r--r--nbs/core.ipynb4
-rw-r--r--nbs/export.ipynb151
-rw-r--r--nbs/init.ipynb3
-rw-r--r--nbs/test.ipynb32
5 files changed, 38 insertions, 174 deletions
diff --git a/nb/export.py b/nb/export.py
index d140293..c2a4748 100644
--- a/nb/export.py
+++ b/nb/export.py
@@ -1,10 +1,7 @@
# AUTOGENERATED! DO NOT EDIT!
# cell: 96525ac4
-import ast
-import enum
-import pathlib
-import re
+import enum,pathlib,re
from typing import IO
import nbformat
@@ -47,17 +44,8 @@ DIRECTIVE_DEFAULT_EXP_RE = _mk_directive_re('default_exp', arg_re=r'(?P<module>\
# cell: 5b4e8fe6
DIRECTIVE_EXPORT_RE = _mk_directive_re('export')
-# cell: 54459016
-DIRECTIVE_NOEXPORT_RE = _mk_directive_re('noexport')
-
# cell: 828159b2
-DIRECTIVES = [DIRECTIVE_DEFAULT_EXP_RE, DIRECTIVE_EXPORT_RE, DIRECTIVE_NOEXPORT_RE]
-
-# cell: 10232dd8
-def has_import(src:str)->bool:
- "Check whether a block of Python source code has any import statements."
- try: return any(isinstance(node, (ast.Import, ast.ImportFrom)) for node in ast.walk(ast.parse(src)))
- except SyntaxError: return False
+DIRECTIVES = [DIRECTIVE_DEFAULT_EXP_RE, DIRECTIVE_EXPORT_RE]
# cell: d327883e
def has_directive(src:str)->bool:
@@ -73,11 +61,7 @@ def strip_directives(src:str|list[str])->str:
# cell: d1f32654
def is_cell_exported(cell:dict)->bool:
"Check whether the code cell should be exported."
- src = cell['source']
- lines = src.splitlines()
- if any(DIRECTIVE_NOEXPORT_RE.match(l) for l in lines): return False
- if any(DIRECTIVE_EXPORT_RE.match(l) for l in lines): return True
- return has_import(src)
+ return any(DIRECTIVE_EXPORT_RE.match(l) for l in cell['source'].splitlines())
# cell: ddc5c9e6
def get_default_export(cells:list[dict])->str:
diff --git a/nbs/core.ipynb b/nbs/core.ipynb
index 64856fd..43ece73 100644
--- a/nbs/core.ipynb
+++ b/nbs/core.ipynb
@@ -28,6 +28,7 @@
"metadata": {},
"outputs": [],
"source": [
+ "#| export\n",
"import inspect\n",
"from typing import Callable\n",
"from nb import test"
@@ -287,8 +288,7 @@
"metadata": {},
"outputs": [],
"source": [
- "#| noexport\n",
- "import nb.export; nb.export.export_nb('core.ipynb', src_dir='../nb/')"
+ "import nb; nb.export_nb('core.ipynb', src_dir='../nb/')"
]
}
],
diff --git a/nbs/export.ipynb b/nbs/export.ipynb
index d662f7d..f1618f2 100644
--- a/nbs/export.ipynb
+++ b/nbs/export.ipynb
@@ -17,16 +17,25 @@
"metadata": {},
"outputs": [],
"source": [
- "import ast\n",
- "import enum\n",
- "import pathlib\n",
- "import re\n",
+ "#| 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": {},
@@ -42,15 +51,12 @@
"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",
- "- Are we exporting it to a Python file?\n",
- "- What Python file are we exporting it to?\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\n",
- "- `#|noexport`: Prevent export of this cell. Useful to prevent export of something that would be automatically exported.\n",
- "\n",
- "Furthermore, beyond the explicit directives, nbdev and nbexport both use automatic exports of cells with imports in them for less annotation overhead since nearly every import in a notebook is needed by the exported code. This is the primary rationale for a `#|noexport` since sometimes a import is purely for exploratory programming, and you won't use it in the final artifact."
+ "- `#|export`: Export this cell to the corresponding Python file"
]
},
{
@@ -63,8 +69,8 @@
"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 or cells that have an import and *don't* have an explicit `#| noexport` 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 and the path to the notebook it came from."
+ " - 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."
]
},
{
@@ -80,15 +86,7 @@
"id": "037fb28c",
"metadata": {},
"source": [
- "To read in `ipynb` files, we can use a library provided Jupyter called `nbformat`:"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "39f9ece6",
- "metadata": {},
- "source": [
- "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:"
+ "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."
]
},
{
@@ -494,25 +492,6 @@
},
{
"cell_type": "markdown",
- "id": "50c2d8ff",
- "metadata": {},
- "source": [
- "#### Supporting the noexport directive"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "54459016",
- "metadata": {},
- "outputs": [],
- "source": [
- "#| export\n",
- "DIRECTIVE_NOEXPORT_RE = _mk_directive_re('noexport')"
- ]
- },
- {
- "cell_type": "markdown",
"id": "a7a778d2",
"metadata": {},
"source": [
@@ -527,51 +506,7 @@
"outputs": [],
"source": [
"#| export\n",
- "DIRECTIVES = [DIRECTIVE_DEFAULT_EXP_RE, DIRECTIVE_EXPORT_RE, DIRECTIVE_NOEXPORT_RE]"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "020f82b0",
- "metadata": {},
- "source": [
- "### Matching Python Imports"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "7863f491",
- "metadata": {},
- "source": [
- "Since we know we're working with Python code cells, we can use the `ast` module to robustly detect if any `import`s are in the code cell."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "10232dd8",
- "metadata": {},
- "outputs": [],
- "source": [
- "#| export\n",
- "def has_import(src:str)->bool:\n",
- " \"Check whether a block of Python source code has any import statements.\"\n",
- " try: return any(isinstance(node, (ast.Import, ast.ImportFrom)) for node in ast.walk(ast.parse(src)))\n",
- " except SyntaxError: return False\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "a61b2562",
- "metadata": {},
- "outputs": [],
- "source": [
- "assert has_import('1 + 1\\nimport re\\na = 5\\n')\n",
- "assert has_import('from re import Pattern\\na = 5\\n')\n",
- "assert not has_import('# import re\\n')\n",
- "assert not has_import('a = 5\\n')\n",
- "assert not has_import('max(1, 10)\\n')"
+ "DIRECTIVES = [DIRECTIVE_DEFAULT_EXP_RE, DIRECTIVE_EXPORT_RE]"
]
},
{
@@ -591,8 +526,7 @@
"\n",
"**Algorithm:**\n",
"1. Always check if there is a `default_exp` directive. There must be one (and only one) `default_exp` directive.\n",
- "2. Check if an import is present. If present, check if a noexport directive is present.\n",
- "3. If no import is present, check for an explicit export directive."
+ "3. Check for an explicit export directive."
]
},
{
@@ -617,7 +551,6 @@
"source": [
"assert has_directive('#| default_exp: bootstrap')\n",
"assert has_directive('#| export')\n",
- "assert has_directive('#| noexport')\n",
"assert has_directive('max(1,20)\\n#| export')\n",
"assert not has_directive('# comment\\nmax(1, 20)\\n')"
]
@@ -644,7 +577,6 @@
"outputs": [],
"source": [
"assert strip_directives('#| export\\na=5') == 'a=5'\n",
- "assert strip_directives('#| noexport\\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'"
]
@@ -659,11 +591,7 @@
"#| export\n",
"def is_cell_exported(cell:dict)->bool:\n",
" \"Check whether the code cell should be exported.\"\n",
- " src = cell['source']\n",
- " lines = src.splitlines()\n",
- " if any(DIRECTIVE_NOEXPORT_RE.match(l) for l in lines): return False\n",
- " if any(DIRECTIVE_EXPORT_RE.match(l) for l in lines): return True\n",
- " return has_import(src)"
+ " return any(DIRECTIVE_EXPORT_RE.match(l) for l in cell['source'].splitlines())"
]
},
{
@@ -674,12 +602,10 @@
"outputs": [],
"source": [
"mk_cell = lambda s,id=1: dict(id=id,source=s,cell_type=CellType.CODE)\n",
- "assert is_cell_exported(mk_cell('import re'))\n",
- "assert is_cell_exported(mk_cell('from re import Pattern'))\n",
"assert is_cell_exported(mk_cell('#| export'))\n",
- "assert not is_cell_exported(mk_cell('#| noexport'))\n",
- "assert not is_cell_exported(mk_cell('#| noexport\\n#| export'))\n",
- "assert not is_cell_exported(mk_cell('#| export\\n#| noexport'))"
+ "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'))"
]
},
{
@@ -801,18 +727,6 @@
{
"cell_type": "code",
"execution_count": null,
- "id": "c044d6d4",
- "metadata": {},
- "outputs": [],
- "source": [
- "#| noexport\n",
- "import os\n",
- "import tempfile"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
"id": "e19b3289",
"metadata": {},
"outputs": [],
@@ -874,17 +788,6 @@
{
"cell_type": "code",
"execution_count": null,
- "id": "c85bc348",
- "metadata": {},
- "outputs": [],
- "source": [
- "#| noexport\n",
- "from io import StringIO"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
"id": "55f57bee",
"metadata": {},
"outputs": [],
@@ -906,9 +809,9 @@
"source": [
"cells = [\n",
" nbformat.v4.new_code_cell('#| default_exp: testmod'),\n",
- " nbformat.v4.new_code_cell('import os'),\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('#| noexport\\nimport pathlib'),\n",
+ " nbformat.v4.new_code_cell('import pathlib'),\n",
"]\n",
"nb = mk_nb(cells)\n",
"src = f\"\"\"# AUTOGENERATED! DO NOT EDIT!\n",
diff --git a/nbs/init.ipynb b/nbs/init.ipynb
index a5240f5..b6aed2d 100644
--- a/nbs/init.ipynb
+++ b/nbs/init.ipynb
@@ -26,7 +26,8 @@
"outputs": [],
"source": [
"#| export\n",
- "from nb.export import export_nb"
+ "#| export\n",
+ "from nb.export import export_nb\n"
]
},
{
diff --git a/nbs/test.ipynb b/nbs/test.ipynb
index d446272..2302a27 100644
--- a/nbs/test.ipynb
+++ b/nbs/test.ipynb
@@ -28,6 +28,7 @@
"metadata": {},
"outputs": [],
"source": [
+ "#| export\n",
"import ast,copy,contextlib,inspect,textwrap,traceback\n",
"from typing import Any,Generator\n",
"from dataclasses import dataclass"
@@ -38,32 +39,8 @@
"execution_count": null,
"id": "2cf5d527",
"metadata": {},
- "outputs": [
- {
- "ename": "NameError",
- "evalue": "name '_T' is not defined",
- "output_type": "error",
- "traceback": [
- "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
- "\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
- "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[9]\u001b[39m\u001b[32m, line 2\u001b[39m",
- "\u001b[32m 1\u001b[39m \u001b[38;5;66;03m#| noexport\u001b[39;00m",
- "\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m nb.core \u001b[38;5;28;01mimport\u001b[39;00m pubattr",
- "",
- "\u001b[36mFile \u001b[39m\u001b[32m~/nb/nb/core.py:28\u001b[39m",
- "\u001b[32m 25\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m inner",
- "\u001b[32m 27\u001b[39m \u001b[38;5;66;03m# cell: 42580f8b\u001b[39;00m",
- "\u001b[32m---> \u001b[39m\u001b[32m28\u001b[39m \u001b[38;5;129m@patch_to\u001b[39m(\u001b[30;43m_T\u001b[39;49m)",
- "\u001b[32m 29\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34msay_hello\u001b[39m(\u001b[38;5;28mself\u001b[39m: _T, name: \u001b[38;5;28mstr\u001b[39m) -> \u001b[38;5;28mstr\u001b[39m:",
- "\u001b[32m 30\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[33mf\u001b[39m\u001b[33m'\u001b[39m\u001b[33mHello, \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m'\u001b[39m",
- "\u001b[32m 32\u001b[39m \u001b[38;5;66;03m# cell: 4eb650ab\u001b[39;00m",
- "",
- "\u001b[31mNameError\u001b[39m: name '_T' is not defined"
- ]
- }
- ],
+ "outputs": [],
"source": [
- "#| noexport\n",
"from nb.core import pubattr"
]
},
@@ -359,7 +336,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "ExceptionInfo(type_=<class 'Exception'>, value=Exception('abc'), traceback=[' File \"/tmp/ipykernel_1287/933907959.py\", line 11, in raises\\n yield info\\n', ' File \"/tmp/ipykernel_1287/3922684010.py\", line 2, in <module>\\n raise Exception(\"abc\")\\n'])\n"
+ "ExceptionInfo(type_=<class 'Exception'>, value=Exception('abc'), traceback=[' File \"/tmp/ipykernel_572/3259713399.py\", line 10, in raises\\n yield info\\n', ' File \"/tmp/ipykernel_572/3922684010.py\", line 2, in <module>\\n raise Exception(\"abc\")\\n'])\n"
]
}
],
@@ -407,8 +384,7 @@
"metadata": {},
"outputs": [],
"source": [
- "#| noexport\n",
- "import nb.export; nb.export.export_nb('test.ipynb', src_dir='../nb/')"
+ "import nb; nb.export_nb('test.ipynb', src_dir='../nb/')"
]
}
],