Skip to content

Commit 32afdcf

Browse files
committed
fixes #688
1 parent 3d48fbb commit 32afdcf

File tree

8 files changed

+79
-35
lines changed

8 files changed

+79
-35
lines changed

nbdev/_modidx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'nbdev_conda=nbdev.shortcuts:conda\n'
3030
'nbdev_release=nbdev.shortcuts:release\n'
3131
'nbdev_prepare=nbdev.shortcuts:prepare\n'
32+
'nbdev_readme=nbdev.cli:nbdev_readme\n'
3233
'nbdev_help=nbdev.shortcuts:chelp',
3334
'copyright': '2020 onwards, Jeremy Howard',
3435
'custom_sidebar': 'True',
@@ -75,6 +76,7 @@
7576
'nbdev.cli.nbdev_ghp_deploy': 'https://nbdev.fast.ai/cli.html#nbdev_ghp_deploy',
7677
'nbdev.cli.nbdev_new': 'https://nbdev.fast.ai/cli.html#nbdev_new',
7778
'nbdev.cli.nbdev_quarto': 'https://nbdev.fast.ai/cli.html#nbdev_quarto',
79+
'nbdev.cli.nbdev_readme': 'https://nbdev.fast.ai/cli.html#nbdev_readme',
7880
'nbdev.cli.nbdev_sidebar': 'https://nbdev.fast.ai/cli.html#nbdev_sidebar',
7981
'nbdev.cli.prompt_user': 'https://nbdev.fast.ai/cli.html#prompt_user',
8082
'nbdev.cli.refresh_quarto_yml': 'https://nbdev.fast.ai/cli.html#refresh_quarto_yml',

nbdev/cli.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
# %% auto 0
2323
__all__ = ['nbdev_ghp_deploy', 'nbdev_sidebar', 'FilterDefaults', 'nbdev_filter', 'update_version', 'bump_version',
24-
'nbdev_bump_version', 'extract_tgz', 'prompt_user', 'refresh_quarto_yml', 'nbdev_new', 'nbdev_quarto']
24+
'nbdev_bump_version', 'extract_tgz', 'prompt_user', 'refresh_quarto_yml', 'nbdev_new', 'nbdev_readme',
25+
'nbdev_quarto']
2526

2627
# %% ../nbs/10_cli.ipynb 5
2728
@call_parse
@@ -59,7 +60,7 @@ def nbdev_sidebar(
5960
):
6061
"Create sidebar.yml"
6162
if not force and str2bool(config_key('custom_sidebar', path=False)): return
62-
path = config_key("nbs_path") if not path else Path(path)
63+
path = config_key("nbs_path", ".") if not path else Path(path)
6364
files = nbglob(path, func=_f, symlinks=symlinks, file_re=file_re, folder_re=folder_re, file_glob=file_glob,
6465
skip_file_glob=skip_file_glob, skip_file_re=skip_file_re, skip_folder_re=skip_folder_re).sorted(key=_sort)
6566
lastd,res = Path(),[]
@@ -301,6 +302,30 @@ def _sprun(cmd):
301302
except subprocess.CalledProcessError as cpe: sys.exit(cpe.returncode)
302303

303304
# %% ../nbs/10_cli.ipynb 26
305+
def _doc_paths(path:str=None, doc_path:str=None):
306+
cfg = get_config()
307+
cfg_path = cfg.config_path
308+
path = config_key("nbs_path", ".") if not path else Path(path)
309+
doc_path = config_key("doc_path") if not doc_path else Path(doc_path)
310+
tmp_doc_path = path/f"{cfg['doc_path']}"
311+
return cfg,cfg_path,path,doc_path,tmp_doc_path
312+
313+
# %% ../nbs/10_cli.ipynb 27
314+
@call_parse
315+
def nbdev_readme(
316+
path:str=None, # Path to notebooks
317+
doc_path:str=None): # Path to output docs
318+
"Render README.md from index.ipynb"
319+
cfg,cfg_path,path,doc_path,tmp_doc_path = _doc_paths(path, doc_path)
320+
idx_path = path/config_key('readme_nb', 'index.ipynb', path=False)
321+
if idx_path.exists():
322+
_sprun(f'cd {path} && quarto render {idx_path} -o README.md -t gfm --no-execute')
323+
if (tmp_doc_path/'README.md').exists():
324+
_rdm = cfg_path/'README.md'
325+
if _rdm.exists(): _rdm.unlink() # py37 doesn't have arg missing_ok so have to check first
326+
shutil.move(str(tmp_doc_path/'README.md'), cfg_path) # README.md is temporarily in nbs/_docs
327+
328+
# %% ../nbs/10_cli.ipynb 28
304329
@call_parse
305330
def nbdev_quarto(
306331
path:str=None, # Path to notebooks
@@ -313,27 +338,15 @@ def nbdev_quarto(
313338
preview:bool=False # Preview the site instead of building it
314339
):
315340
"Create Quarto docs and README.md"
316-
cfg = get_config()
317-
cfg_path = cfg.config_path
341+
cfg,cfg_path,path,doc_path,tmp_doc_path = _doc_paths(path, doc_path)
318342
refresh_quarto_yml()
319-
path = config_key("nbs_path") if not path else Path(path)
320-
idx_path = path/config_key('readme_nb', 'index.ipynb', path=False)
321343
files = nbdev_sidebar.__wrapped__(path, symlinks=symlinks, file_re=file_re, folder_re=folder_re,
322344
skip_file_glob=skip_file_glob, skip_file_re=skip_file_re, returnit=True)
323-
doc_path = config_key("doc_path") if not doc_path else Path(doc_path)
324-
tmp_doc_path = config_key('nbs_path')/f"{cfg['doc_path']}"
325345
shutil.rmtree(doc_path, ignore_errors=True)
326346
cmd = 'preview' if preview else 'render'
327347
_sprun(f'cd {path} && quarto {cmd} --no-execute')
328348
if not preview:
329-
if idx_path.exists():
330-
_sprun(f'cd {path} && quarto render {idx_path} -o README.md -t gfm --no-execute')
331-
332-
if (tmp_doc_path/'README.md').exists():
333-
_rdm = cfg_path/'README.md'
334-
if _rdm.exists(): _rdm.unlink() #py 3.7 doesn't have arg missing_ok so have to check first
335-
shutil.move(str(tmp_doc_path/'README.md'), cfg_path) # README.md is temporarily in nbs/_docs
336-
349+
nbdev_readme.__wrapped__(path, doc_path)
337350
if tmp_doc_path.parent != cfg_path: # move docs folder to root of repo if it doesn't exist there
338351
shutil.rmtree(doc_path, ignore_errors=True)
339352
shutil.move(tmp_doc_path, cfg_path)

nbdev/doclinks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def build_modidx():
117117
dest = config_key('lib_path')
118118
if os.environ.get('IN_TEST',0): return
119119
_fn = dest/'_modidx.py'
120-
nbs_path = config_key('nbs_path')
120+
nbs_path = config_key('nbs_path', '.')
121121
files = globtastic(nbs_path)
122122
with contextlib.suppress(FileNotFoundError): _fn.unlink()
123123
cfg = get_config()

nbdev/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def nbdev_test(
8282
print('No files were eligible for testing')
8383
return
8484
if n_workers is None: n_workers = 0 if len(files)==1 else min(num_cpus(), 8)
85-
os.chdir(config_key("nbs_path"))
85+
os.chdir(config_key("nbs_path", "."))
8686
results = parallel(test_nb, files, skip_flags=skip_flags, force_flags=force_flags, n_workers=n_workers, pause=pause, do_print=do_print)
8787
passed,times = zip(*results)
8888
if all(passed): print("Success.")

nbs/04b_doclinks.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@
394394
" dest = config_key('lib_path')\n",
395395
" if os.environ.get('IN_TEST',0): return\n",
396396
" _fn = dest/'_modidx.py'\n",
397-
" nbs_path = config_key('nbs_path')\n",
397+
" nbs_path = config_key('nbs_path', '.')\n",
398398
" files = globtastic(nbs_path)\n",
399399
" with contextlib.suppress(FileNotFoundError): _fn.unlink()\n",
400400
" cfg = get_config()\n",

nbs/10_cli.ipynb

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"):\n",
117117
" \"Create sidebar.yml\"\n",
118118
" if not force and str2bool(config_key('custom_sidebar', path=False)): return\n",
119-
" path = config_key(\"nbs_path\") if not path else Path(path)\n",
119+
" path = config_key(\"nbs_path\", \".\") if not path else Path(path)\n",
120120
" files = nbglob(path, func=_f, symlinks=symlinks, file_re=file_re, folder_re=folder_re, file_glob=file_glob,\n",
121121
" skip_file_glob=skip_file_glob, skip_file_re=skip_file_re, skip_folder_re=skip_folder_re).sorted(key=_sort)\n",
122122
" lastd,res = Path(),[]\n",
@@ -517,6 +517,46 @@
517517
" except subprocess.CalledProcessError as cpe: sys.exit(cpe.returncode)"
518518
]
519519
},
520+
{
521+
"cell_type": "code",
522+
"execution_count": null,
523+
"id": "5192dfae",
524+
"metadata": {},
525+
"outputs": [],
526+
"source": [
527+
"#|export\n",
528+
"def _doc_paths(path:str=None, doc_path:str=None):\n",
529+
" cfg = get_config()\n",
530+
" cfg_path = cfg.config_path\n",
531+
" path = config_key(\"nbs_path\", \".\") if not path else Path(path)\n",
532+
" doc_path = config_key(\"doc_path\") if not doc_path else Path(doc_path)\n",
533+
" tmp_doc_path = path/f\"{cfg['doc_path']}\"\n",
534+
" return cfg,cfg_path,path,doc_path,tmp_doc_path"
535+
]
536+
},
537+
{
538+
"cell_type": "code",
539+
"execution_count": null,
540+
"id": "4e995496",
541+
"metadata": {},
542+
"outputs": [],
543+
"source": [
544+
"#|export\n",
545+
"@call_parse\n",
546+
"def nbdev_readme(\n",
547+
" path:str=None, # Path to notebooks\n",
548+
" doc_path:str=None): # Path to output docs\n",
549+
" \"Render README.md from index.ipynb\"\n",
550+
" cfg,cfg_path,path,doc_path,tmp_doc_path = _doc_paths(path, doc_path)\n",
551+
" idx_path = path/config_key('readme_nb', 'index.ipynb', path=False)\n",
552+
" if idx_path.exists(): \n",
553+
" _sprun(f'cd {path} && quarto render {idx_path} -o README.md -t gfm --no-execute')\n",
554+
" if (tmp_doc_path/'README.md').exists():\n",
555+
" _rdm = cfg_path/'README.md'\n",
556+
" if _rdm.exists(): _rdm.unlink() # py37 doesn't have arg missing_ok so have to check first\n",
557+
" shutil.move(str(tmp_doc_path/'README.md'), cfg_path) # README.md is temporarily in nbs/_docs"
558+
]
559+
},
520560
{
521561
"cell_type": "code",
522562
"execution_count": null,
@@ -537,27 +577,15 @@
537577
" preview:bool=False # Preview the site instead of building it\n",
538578
"):\n",
539579
" \"Create Quarto docs and README.md\"\n",
540-
" cfg = get_config()\n",
541-
" cfg_path = cfg.config_path\n",
580+
" cfg,cfg_path,path,doc_path,tmp_doc_path = _doc_paths(path, doc_path)\n",
542581
" refresh_quarto_yml()\n",
543-
" path = config_key(\"nbs_path\") if not path else Path(path)\n",
544-
" idx_path = path/config_key('readme_nb', 'index.ipynb', path=False)\n",
545582
" files = nbdev_sidebar.__wrapped__(path, symlinks=symlinks, file_re=file_re, folder_re=folder_re,\n",
546583
" skip_file_glob=skip_file_glob, skip_file_re=skip_file_re, returnit=True)\n",
547-
" doc_path = config_key(\"doc_path\") if not doc_path else Path(doc_path)\n",
548-
" tmp_doc_path = config_key('nbs_path')/f\"{cfg['doc_path']}\"\n",
549584
" shutil.rmtree(doc_path, ignore_errors=True)\n",
550585
" cmd = 'preview' if preview else 'render'\n",
551586
" _sprun(f'cd {path} && quarto {cmd} --no-execute')\n",
552587
" if not preview:\n",
553-
" if idx_path.exists(): \n",
554-
" _sprun(f'cd {path} && quarto render {idx_path} -o README.md -t gfm --no-execute')\n",
555-
"\n",
556-
" if (tmp_doc_path/'README.md').exists():\n",
557-
" _rdm = cfg_path/'README.md'\n",
558-
" if _rdm.exists(): _rdm.unlink() #py 3.7 doesn't have arg missing_ok so have to check first\n",
559-
" shutil.move(str(tmp_doc_path/'README.md'), cfg_path) # README.md is temporarily in nbs/_docs\n",
560-
"\n",
588+
" nbdev_readme.__wrapped__(path, doc_path)\n",
561589
" if tmp_doc_path.parent != cfg_path: # move docs folder to root of repo if it doesn't exist there\n",
562590
" shutil.rmtree(doc_path, ignore_errors=True)\n",
563591
" shutil.move(tmp_doc_path, cfg_path)"

nbs/14_test.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
" print('No files were eligible for testing')\n",
190190
" return\n",
191191
" if n_workers is None: n_workers = 0 if len(files)==1 else min(num_cpus(), 8)\n",
192-
" os.chdir(config_key(\"nbs_path\"))\n",
192+
" os.chdir(config_key(\"nbs_path\", \".\"))\n",
193193
" results = parallel(test_nb, files, skip_flags=skip_flags, force_flags=force_flags, n_workers=n_workers, pause=pause, do_print=do_print)\n",
194194
" passed,times = zip(*results)\n",
195195
" if all(passed): print(\"Success.\")\n",

settings.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ console_scripts = nbdev_create_config=nbdev.read:nbdev_create_config
4848
nbdev_conda=nbdev.shortcuts:conda
4949
nbdev_release=nbdev.shortcuts:release
5050
nbdev_prepare=nbdev.shortcuts:prepare
51+
nbdev_readme=nbdev.cli:nbdev_readme
5152
nbdev_help=nbdev.shortcuts:chelp
5253
tst_flags = notest
5354
nbs_path = nbs

0 commit comments

Comments
 (0)