Skip to content

Commit d93caae

Browse files
committed
fixes #1578
1 parent 89c0f3a commit d93caae

File tree

5 files changed

+35
-28
lines changed

5 files changed

+35
-28
lines changed

nbdev/_modidx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@
113113
'nbdev.frontmatter._dict2fm': ('api/frontmatter.html#_dict2fm', 'nbdev/frontmatter.py'),
114114
'nbdev.frontmatter._fm2dict': ('api/frontmatter.html#_fm2dict', 'nbdev/frontmatter.py'),
115115
'nbdev.frontmatter._insertfm': ('api/frontmatter.html#_insertfm', 'nbdev/frontmatter.py'),
116-
'nbdev.frontmatter._md2dict': ('api/frontmatter.html#_md2dict', 'nbdev/frontmatter.py')},
116+
'nbdev.frontmatter._md2dict': ('api/frontmatter.html#_md2dict', 'nbdev/frontmatter.py'),
117+
'nbdev.frontmatter.nb_frontmatter': ('api/frontmatter.html#nb_frontmatter', 'nbdev/frontmatter.py')},
117118
'nbdev.imports': {},
118119
'nbdev.maker': { 'nbdev.maker.ModuleMaker': ('api/maker.html#modulemaker', 'nbdev/maker.py'),
119120
'nbdev.maker.ModuleMaker.__init__': ('api/maker.html#modulemaker.__init__', 'nbdev/maker.py'),

nbdev/frontmatter.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/09_frontmatter.ipynb.
44

55
# %% auto #0
6-
__all__ = ['FrontmatterProc']
6+
__all__ = ['nb_frontmatter', 'FrontmatterProc']
77

88
# %% ../nbs/api/09_frontmatter.ipynb #2398f5ef-06d3-4890-8a54-7cf4f81f3894
99
from .imports import *
@@ -42,6 +42,13 @@ def _md2dict(s:str):
4242
except Exception as e: warn(f'Failed to create YAML dict for:\n{r}\n\n{e}\n')
4343
return res
4444

45+
def nb_frontmatter(nb):
46+
"Get frontmatter dict from `nb` without modifying cells"
47+
for c in nb.cells:
48+
if c.cell_type=='raw': return _fm2dict(c.source)
49+
if c.cell_type=='markdown': return _md2dict(c.source)
50+
return {}
51+
4552
# %% ../nbs/api/09_frontmatter.ipynb #1b5d9d32
4653
def _dict2fm(d): return f'---\n{yaml.dump(d)}\n---\n\n'
4754
def _insertfm(nb, fm): nb.cells.insert(0, mk_cell(_dict2fm(fm), 'raw'))

nbdev/test.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .config import *
1818
from .doclinks import *
1919
from .process import NBProcessor, nb_lang
20-
from .frontmatter import FrontmatterProc
20+
from .frontmatter import nb_frontmatter
2121

2222
from execnb.nbio import *
2323
from execnb.shell import *
@@ -29,13 +29,14 @@ def test_nb(fn, # file name of notebook to test
2929
do_print=False, # print completion?
3030
showerr=True, # print errors to stderr?
3131
basepath=None, # path to add to sys.path
32-
verbose=False): # stream stdout/stderr from cells to console?
32+
verbose=False, # stream stdout/stderr from cells to console?
33+
save=False): # write outputs back to notebook on success?
3334
"Execute tests in notebook in `fn` except those with `skip_flags`"
3435
if basepath: sys.path.insert(0, str(basepath))
3536
if not IN_NOTEBOOK: os.environ["IN_TEST"] = '1'
3637
flags=set(L(skip_flags)) - set(L(force_flags))
37-
nb = NBProcessor(fn, procs=FrontmatterProc, process=True).nb
38-
fm = getattr(nb, 'frontmatter_', {})
38+
nb = NBProcessor(fn, rm_directives=False, process=True).nb
39+
fm = nb_frontmatter(nb)
3940
if str2bool(fm.get('skip_exec', False)) or nb_lang(nb) != 'python': return True, 0
4041

4142
def _no_eval(cell):
@@ -51,6 +52,7 @@ def _no_eval(cell):
5152
try:
5253
with working_directory(fn.parent):
5354
k.run_all(nb, exc_stop=True, preproc=_no_eval, verbose=verbose)
55+
if save: write_nb(nb, fn)
5456
res = True
5557
except:
5658
if showerr: sys.stderr.write(k.prettytb(fname=fn)+'\n')
@@ -78,6 +80,7 @@ def nbdev_test(
7880
pause:float=0.01, # Pause time (in seconds) between notebooks to avoid race conditions
7981
ignore_fname:str='.notest', # Filename that will result in siblings being ignored
8082
verbose:bool=False, # Print stdout/stderr from notebook cells?
83+
save:bool=False, # Write outputs back to notebooks on success?
8184
**kwargs):
8285
"Test in parallel notebooks matching `path`, passing along `flags`"
8386
skip_flags = get_config().tst_flags
@@ -92,7 +95,7 @@ def nbdev_test(
9295
wd_pth = get_config().nbs_path
9396
with working_directory(wd_pth if (wd_pth and wd_pth.exists()) else os.getcwd()):
9497
results = parallel(test_nb, files, skip_flags=skip_flags, force_flags=force_flags, n_workers=n_workers,
95-
basepath=get_config().config_path, pause=pause, do_print=do_print, verbose=verbose, **kw)
98+
basepath=get_config().config_path, pause=pause, do_print=do_print, verbose=verbose, save=save, **kw)
9699
passed,times = zip(*results)
97100
if all(passed): print("Success.")
98101
else:

nbs/api/09_frontmatter.ipynb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@
9393
" if r:\n",
9494
" try: res.update(yaml.safe_load('\\n'.join(r)))\n",
9595
" except Exception as e: warn(f'Failed to create YAML dict for:\\n{r}\\n\\n{e}\\n')\n",
96-
" return res"
96+
" return res\n",
97+
"\n",
98+
"def nb_frontmatter(nb):\n",
99+
" \"Get frontmatter dict from `nb` without modifying cells\"\n",
100+
" for c in nb.cells:\n",
101+
" if c.cell_type=='raw': return _fm2dict(c.source)\n",
102+
" if c.cell_type=='markdown': return _md2dict(c.source)\n",
103+
" return {}"
97104
]
98105
},
99106
{
@@ -278,13 +285,7 @@
278285
"source": []
279286
}
280287
],
281-
"metadata": {
282-
"kernelspec": {
283-
"display_name": "python3",
284-
"language": "python",
285-
"name": "python3"
286-
}
287-
},
288+
"metadata": {},
288289
"nbformat": 4,
289290
"nbformat_minor": 5
290291
}

nbs/api/12_test.ipynb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"from nbdev.config import *\n",
4040
"from nbdev.doclinks import *\n",
4141
"from nbdev.process import NBProcessor, nb_lang\n",
42-
"from nbdev.frontmatter import FrontmatterProc\n",
42+
"from nbdev.frontmatter import nb_frontmatter\n",
4343
"\n",
4444
"from execnb.nbio import *\n",
4545
"from execnb.shell import *"
@@ -59,13 +59,14 @@
5959
" do_print=False, # print completion?\n",
6060
" showerr=True, # print errors to stderr?\n",
6161
" basepath=None, # path to add to sys.path\n",
62-
" verbose=False): # stream stdout/stderr from cells to console?\n",
62+
" verbose=False, # stream stdout/stderr from cells to console?\n",
63+
" save=False): # write outputs back to notebook on success?\n",
6364
" \"Execute tests in notebook in `fn` except those with `skip_flags`\"\n",
6465
" if basepath: sys.path.insert(0, str(basepath))\n",
6566
" if not IN_NOTEBOOK: os.environ[\"IN_TEST\"] = '1'\n",
6667
" flags=set(L(skip_flags)) - set(L(force_flags))\n",
67-
" nb = NBProcessor(fn, procs=FrontmatterProc, process=True).nb\n",
68-
" fm = getattr(nb, 'frontmatter_', {})\n",
68+
" nb = NBProcessor(fn, rm_directives=False, process=True).nb\n",
69+
" fm = nb_frontmatter(nb)\n",
6970
" if str2bool(fm.get('skip_exec', False)) or nb_lang(nb) != 'python': return True, 0\n",
7071
"\n",
7172
" def _no_eval(cell):\n",
@@ -81,6 +82,7 @@
8182
" try:\n",
8283
" with working_directory(fn.parent):\n",
8384
" k.run_all(nb, exc_stop=True, preproc=_no_eval, verbose=verbose)\n",
85+
" if save: write_nb(nb, fn)\n",
8486
" res = True\n",
8587
" except: \n",
8688
" if showerr: sys.stderr.write(k.prettytb(fname=fn)+'\\n')\n",
@@ -172,6 +174,7 @@
172174
" pause:float=0.01, # Pause time (in seconds) between notebooks to avoid race conditions\n",
173175
" ignore_fname:str='.notest', # Filename that will result in siblings being ignored\n",
174176
" verbose:bool=False, # Print stdout/stderr from notebook cells?\n",
177+
" save:bool=False, # Write outputs back to notebooks on success?\n",
175178
" **kwargs):\n",
176179
" \"Test in parallel notebooks matching `path`, passing along `flags`\"\n",
177180
" skip_flags = get_config().tst_flags\n",
@@ -186,7 +189,7 @@
186189
" wd_pth = get_config().nbs_path\n",
187190
" with working_directory(wd_pth if (wd_pth and wd_pth.exists()) else os.getcwd()):\n",
188191
" results = parallel(test_nb, files, skip_flags=skip_flags, force_flags=force_flags, n_workers=n_workers,\n",
189-
" basepath=get_config().config_path, pause=pause, do_print=do_print, verbose=verbose, **kw)\n",
192+
" basepath=get_config().config_path, pause=pause, do_print=do_print, verbose=verbose, save=save, **kw)\n",
190193
" passed,times = zip(*results)\n",
191194
" if all(passed): print(\"Success.\")\n",
192195
" else: \n",
@@ -253,14 +256,6 @@
253256
"#| hide\n",
254257
"import nbdev; nbdev.nbdev_export()"
255258
]
256-
},
257-
{
258-
"cell_type": "code",
259-
"execution_count": null,
260-
"id": "86f62c6e",
261-
"metadata": {},
262-
"outputs": [],
263-
"source": []
264259
}
265260
],
266261
"metadata": {},

0 commit comments

Comments
 (0)