Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion nbdev/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from collections import defaultdict

import jupytext, nbformat, tempfile

# %% ../nbs/api/03_process.ipynb 6
# from https://github.com/quarto-dev/quarto-cli/blob/main/src/resources/jupyter/notebook.py
langs = defaultdict(
Expand Down Expand Up @@ -90,7 +92,18 @@ def _is_direc(f): return getattr(f, '__name__', '-')[-1]=='_'
class NBProcessor:
"Process cells and nbdev comments in a notebook"
def __init__(self, path=None, procs=None, nb=None, debug=False, rm_directives=True, process=False):
self.nb = read_nb(path) if nb is None else nb

if nb is None:
if str(path).endswith(".py"):
nb_converted = jupytext.read(path)
with tempfile.NamedTemporaryFile(delete=True, suffix=".ipynb") as temp_file:
nbformat.write(nb_converted, temp_file.name)
self.nb = read_nb(temp_file.name) if nb is None else nb
else:
self.nb = read_nb(path)
else:
self.nb = nb

self.lang = nb_lang(self.nb)
for cell in self.nb.cells: cell.directives_ = extract_directives(cell, remove=rm_directives, lang=self.lang)
self.procs = _mk_procs(procs, nb=self.nb)
Expand Down
25 changes: 15 additions & 10 deletions nbs/api/03_process.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"from fastcore.script import *\n",
"from fastcore.imports import *\n",
"\n",
"from collections import defaultdict"
"from collections import defaultdict\n",
"\n",
"import jupytext, nbformat, tempfile"
]
},
{
Expand Down Expand Up @@ -364,7 +366,18 @@
"class NBProcessor:\n",
" \"Process cells and nbdev comments in a notebook\"\n",
" def __init__(self, path=None, procs=None, nb=None, debug=False, rm_directives=True, process=False):\n",
" self.nb = read_nb(path) if nb is None else nb\n",
" \n",
" if nb is None:\n",
" if str(path).endswith(\".py\"):\n",
" nb_converted = jupytext.read(path)\n",
" with tempfile.NamedTemporaryFile(delete=True, suffix=\".ipynb\") as temp_file:\n",
" nbformat.write(nb_converted, temp_file.name)\n",
" self.nb = read_nb(temp_file.name) if nb is None else nb\n",
" else:\n",
" self.nb = read_nb(path)\n",
" else:\n",
" self.nb = nb\n",
" \n",
" self.lang = nb_lang(self.nb)\n",
" for cell in self.nb.cells: cell.directives_ = extract_directives(cell, remove=rm_directives, lang=self.lang)\n",
" self.procs = _mk_procs(procs, nb=self.nb)\n",
Expand Down Expand Up @@ -641,14 +654,6 @@
"g = exec_new('import nbdev.process')\n",
"assert hasattr(g['nbdev'].process, 'NBProcessor')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af6db102-2447-49ba-94d9-bfebfb48c0f0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
45 changes: 38 additions & 7 deletions nbs/api/04_export.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export -"
"Let's check if it is possible to export a `.py` file using the [`percent`](https://jupytext.readthedocs.io/en/latest/formats-scripts.html) format"
]
},
{
Expand All @@ -329,20 +329,51 @@
"metadata": {},
"outputs": [],
"source": [
"#|eval: false\n",
"Path('../nbdev/export.py').unlink(missing_ok=True)\n",
"nb_export('04_export.ipynb')\n",
"import tempfile"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"perc_py = \"\"\"\n",
"#%%\n",
"#|default_exp perc\n",
"\n",
"g = exec_new('import nbdev.export')\n",
"assert hasattr(g['nbdev'].export, 'nb_export')"
"#%%\n",
"#|export\n",
"print(\"Hello world\")\n",
"\"\"\"\n",
"\n",
"with tempfile.TemporaryDirectory() as temp_dir:\n",
" with open(f\"{temp_dir}/perc.py\", \"w\") as f: f.write(perc_py)\n",
" nb_export(f\"{temp_dir}/perc.py\", f\"{temp_dir}/perc_test\")\n",
" \n",
" !cat {temp_dir}/perc_test/perc.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export -"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"#|eval: false\n",
"Path('../nbdev/export.py').unlink(missing_ok=True)\n",
"nb_export('04_export.ipynb')\n",
"\n",
"g = exec_new('import nbdev.export')\n",
"assert hasattr(g['nbdev'].export, 'nb_export')"
]
}
],
"metadata": {
Expand Down
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ language = English
custom_sidebar = True
license = apache2
status = 5
requirements = fastcore>=1.5.27 execnb>=0.1.4 astunparse ghapi>=1.0.3 watchdog asttokens
requirements = fastcore>=1.5.27 execnb>=0.1.4 astunparse ghapi>=1.0.3 watchdog asttokens jupytext nbformat
pip_requirements = PyYAML
conda_requirements = pyyaml
conda_user = fastai
Expand Down
Loading