Skip to content

Commit a9c7ae3

Browse files
authored
Merge pull request #1454 from Isaac-Flath/watch_export
Watch export
2 parents f9fc9ad + 2d61b50 commit a9c7ae3

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

nbdev/_modidx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
'nbdev.cli.nb_export_cli': ('api/cli.html#nb_export_cli', 'nbdev/cli.py'),
2727
'nbdev.cli.nbdev_filter': ('api/cli.html#nbdev_filter', 'nbdev/cli.py'),
2828
'nbdev.cli.nbdev_new': ('api/cli.html#nbdev_new', 'nbdev/cli.py'),
29-
'nbdev.cli.nbdev_update_license': ('api/cli.html#nbdev_update_license', 'nbdev/cli.py')},
29+
'nbdev.cli.nbdev_update_license': ('api/cli.html#nbdev_update_license', 'nbdev/cli.py'),
30+
'nbdev.cli.watch_export': ('api/cli.html#watch_export', 'nbdev/cli.py')},
3031
'nbdev.config': { 'nbdev.config._apply_defaults': ('api/config.html#_apply_defaults', 'nbdev/config.py'),
3132
'nbdev.config._basic_export_nb': ('api/config.html#_basic_export_nb', 'nbdev/config.py'),
3233
'nbdev.config._cfg2txt': ('api/config.html#_cfg2txt', 'nbdev/config.py'),

nbdev/cli.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
# %% ../nbs/api/13_cli.ipynb 2
66
from __future__ import annotations
77
import warnings
8+
import time
89

910
from .config import *
1011
from .process import *
1112
from .processors import *
1213
from .doclinks import *
1314
from .test import *
1415
from .clean import *
15-
from .quarto import nbdev_readme, refresh_quarto_yml
16+
from .quarto import nbdev_readme, refresh_quarto_yml, fs_watchdog
1617
from .export import nb_export
1718
from .frontmatter import FrontmatterProc
1819

20+
from fastcore.xtras import run
1921
from execnb.nbio import *
2022
from fastcore.meta import *
2123
from fastcore.utils import *
@@ -28,7 +30,8 @@
2830
import os, tarfile, sys
2931

3032
# %% auto 0
31-
__all__ = ['mapping', 'nbdev_filter', 'extract_tgz', 'nbdev_new', 'nbdev_update_license', 'nb_export_cli', 'chelp']
33+
__all__ = ['mapping', 'nbdev_filter', 'extract_tgz', 'nbdev_new', 'nbdev_update_license', 'nb_export_cli', 'watch_export',
34+
'chelp']
3235

3336
# %% ../nbs/api/13_cli.ipynb
3437
@call_parse
@@ -168,6 +171,25 @@ def nb_export_cli(nbname,
168171
"Export a single nbdev notebook to a python script."
169172
return nb_export(nbname=nbname, debug=debug, **kwargs)
170173

174+
# %% ../nbs/api/13_cli.ipynb
175+
@call_parse
176+
def watch_export(nbs:str=None, # Nb directory to watch for changes
177+
lib:str=None, # Export directory to write py files to
178+
force:bool=False # Ignore nbdev config if in nbdev project
179+
):
180+
'''Use `nb_export` on ipynb files in `nbs` directory on changes using nbdev config if available'''
181+
cfg = get_config() if is_nbdev() else None
182+
nbs = nbs or (cfg.nbs_path if cfg else '.')
183+
lib = lib or (cfg.lib_path if cfg else '.')
184+
if cfg and (nbs != cfg.nbs_path or lib != cfg.lib_path) and not force:
185+
raise ValueError("In nbdev project. Use --force to override config.")
186+
def _export(e,lib=lib):
187+
p = e.src_path
188+
if (not '.ipynb_checkpoints' in p and p.endswith('.ipynb') and not Path(p).name.startswith('.~')):
189+
run(f'nb_export --lib_path {lib} "{p}"')
190+
with fs_watchdog(_export, nbs):
191+
while True: time.sleep(1)
192+
171193
# %% ../nbs/api/13_cli.ipynb
172194
@call_parse
173195
def chelp():

nbs/api/13_cli.ipynb

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@
3030
"#|export\n",
3131
"from __future__ import annotations\n",
3232
"import warnings\n",
33+
"import time\n",
3334
"\n",
3435
"from nbdev.config import *\n",
3536
"from nbdev.process import *\n",
3637
"from nbdev.processors import *\n",
3738
"from nbdev.doclinks import *\n",
3839
"from nbdev.test import *\n",
3940
"from nbdev.clean import *\n",
40-
"from nbdev.quarto import nbdev_readme, refresh_quarto_yml\n",
41+
"from nbdev.quarto import nbdev_readme, refresh_quarto_yml, fs_watchdog\n",
4142
"from nbdev.export import nb_export\n",
4243
"from nbdev.frontmatter import FrontmatterProc\n",
4344
"\n",
45+
"from fastcore.xtras import run\n",
4446
"from execnb.nbio import *\n",
4547
"from fastcore.meta import *\n",
4648
"from fastcore.utils import *\n",
@@ -304,6 +306,33 @@
304306
" return nb_export(nbname=nbname, debug=debug, **kwargs)"
305307
]
306308
},
309+
{
310+
"cell_type": "code",
311+
"execution_count": null,
312+
"id": "aaa472e7",
313+
"metadata": {},
314+
"outputs": [],
315+
"source": [
316+
"#|export\n",
317+
"@call_parse\n",
318+
"def watch_export(nbs:str=None, # Nb directory to watch for changes\n",
319+
" lib:str=None, # Export directory to write py files to\n",
320+
" force:bool=False # Ignore nbdev config if in nbdev project\n",
321+
" ):\n",
322+
" '''Use `nb_export` on ipynb files in `nbs` directory on changes using nbdev config if available'''\n",
323+
" cfg = get_config() if is_nbdev() else None\n",
324+
" nbs = nbs or (cfg.nbs_path if cfg else '.')\n",
325+
" lib = lib or (cfg.lib_path if cfg else '.')\n",
326+
" if cfg and (nbs != cfg.nbs_path or lib != cfg.lib_path) and not force:\n",
327+
" raise ValueError(\"In nbdev project. Use --force to override config.\")\n",
328+
" def _export(e,lib=lib):\n",
329+
" p = e.src_path\n",
330+
" if (not '.ipynb_checkpoints' in p and p.endswith('.ipynb') and not Path(p).name.startswith('.~')):\n",
331+
" run(f'nb_export --lib_path {lib} \"{p}\"')\n",
332+
" with fs_watchdog(_export, nbs):\n",
333+
" while True: time.sleep(1)"
334+
]
335+
},
307336
{
308337
"cell_type": "code",
309338
"execution_count": null,
@@ -380,7 +409,8 @@
380409
"\u001b[1m\u001b[94mnbdev_test\u001b[22m\u001b[39m Test in parallel notebooks matching `path`, passing along `flags`\n",
381410
"\u001b[1m\u001b[94mnbdev_trust\u001b[22m\u001b[39m Trust notebooks matching `fname`\n",
382411
"\u001b[1m\u001b[94mnbdev_update\u001b[22m\u001b[39m Propagate change in modules matching `fname` to notebooks that created them\n",
383-
"\u001b[1m\u001b[94mnbdev_update_license\u001b[22m\u001b[39m Allows you to update the license of your project.\n"
412+
"\u001b[1m\u001b[94mnbdev_update_license\u001b[22m\u001b[39m Allows you to update the license of your project.\n",
413+
"\u001b[1m\u001b[94mwatch_export\u001b[22m\u001b[39m None\n"
384414
]
385415
}
386416
],

settings.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ console_scripts = nbdev_create_config=nbdev.config:nbdev_create_config
5151
nbdev_proc_nbs=nbdev.quarto:nbdev_proc_nbs
5252
nbdev_help=nbdev.cli:chelp
5353
nb_export=nbdev.cli:nb_export_cli
54+
watch_export=nbdev.cli:watch_export
5455
tst_flags = notest
5556
nbs_path = nbs
5657
doc_path = _docs

0 commit comments

Comments
 (0)