Skip to content

Commit 77b27d3

Browse files
committed
make nb_export a cli [Enhancement]
1 parent cb87e4b commit 77b27d3

File tree

6 files changed

+79
-36
lines changed

6 files changed

+79
-36
lines changed

nbdev/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
'nbdev.cli._update_repo_meta': ('api/cli.html#_update_repo_meta', 'nbdev/cli.py'),
2424
'nbdev.cli.chelp': ('api/cli.html#chelp', 'nbdev/cli.py'),
2525
'nbdev.cli.extract_tgz': ('api/cli.html#extract_tgz', 'nbdev/cli.py'),
26+
'nbdev.cli.nb_export_cli': ('api/cli.html#nb_export_cli', 'nbdev/cli.py'),
2627
'nbdev.cli.nbdev_filter': ('api/cli.html#nbdev_filter', 'nbdev/cli.py'),
2728
'nbdev.cli.nbdev_new': ('api/cli.html#nbdev_new', 'nbdev/cli.py'),
2829
'nbdev.cli.nbdev_update_license': ('api/cli.html#nbdev_update_license', 'nbdev/cli.py')},

nbdev/cli.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .test import *
1414
from .clean import *
1515
from .quarto import nbdev_readme, refresh_quarto_yml
16+
from .export import nb_export
1617
from .frontmatter import FrontmatterProc
1718

1819
from execnb.nbio import *
@@ -27,7 +28,7 @@
2728
import os, tarfile, sys
2829

2930
# %% auto 0
30-
__all__ = ['mapping', 'nbdev_filter', 'extract_tgz', 'nbdev_new', 'nbdev_update_license', 'chelp']
31+
__all__ = ['mapping', 'nbdev_filter', 'extract_tgz', 'nbdev_new', 'nbdev_update_license', 'nb_export_cli', 'chelp']
3132

3233
# %% ../nbs/api/13_cli.ipynb
3334
@call_parse
@@ -158,6 +159,13 @@ def nbdev_update_license(
158159
lic.write(body)
159160
print(f"License updated from {curr_lic} to {to}")
160161

162+
# %% ../nbs/api/13_cli.ipynb
163+
@call_parse
164+
@delegates(nb_export, but=['procs', 'mod_maker'])
165+
def nb_export_cli(nbname, lib_path, name, debug:Param("debug flag", store_true), **kwargs):
166+
"Export a single nbdev notebook to a python script."
167+
return nb_export(nbname=nbname, lib_path=lib_path, name=name, debug=debug, **kwargs)
168+
161169
# %% ../nbs/api/13_cli.ipynb
162170
@call_parse
163171
def chelp():

nbdev/export.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from fastcore.script import *
1515
from fastcore.basics import *
1616
from fastcore.imports import *
17+
from fastcore.meta import *
1718

1819
from collections import defaultdict
1920

@@ -63,12 +64,18 @@ def scrub_magics(cell): # Cell to format
6364
def optional_procs():
6465
"An explicit list of processors that could be used by `nb_export`"
6566
return L([p for p in nbdev.export.__all__
66-
if p not in ["nb_export", "ExportModuleProc", "optional_procs"]])
67+
if p not in ["nb_export", "nb_export_cli", "ExportModuleProc", "optional_procs"]])
6768

6869
# %% ../nbs/api/04_export.ipynb
69-
def nb_export(nbname, lib_path=None, procs=None, debug=False, mod_maker=ModuleMaker, name=None):
70+
def nb_export(nbname:str, # Filename of notebook
71+
lib_path:str=None, # Path to destination library. If not in a nbdev project, defaults to current directory.
72+
procs=None, # Processors to use
73+
name:str=None, # Name of python script {name}.py to create.
74+
mod_maker=ModuleMaker,
75+
debug:bool=False, # Debug mode
76+
):
7077
"Create module(s) from notebook"
71-
if lib_path is None: lib_path = get_config().lib_path
78+
if lib_path is None: lib_path = get_config().lib_path if is_nbdev() else '.'
7279
exp = ExportModuleProc()
7380
nb = NBProcessor(nbname, [exp]+L(procs), debug=debug)
7481
nb.process()

nbs/api/04_export.ipynb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"from fastcore.script import *\n",
3535
"from fastcore.basics import *\n",
3636
"from fastcore.imports import *\n",
37+
"from fastcore.meta import *\n",
3738
"\n",
3839
"from collections import defaultdict"
3940
]
@@ -210,7 +211,7 @@
210211
"def optional_procs():\n",
211212
" \"An explicit list of processors that could be used by `nb_export`\"\n",
212213
" return L([p for p in nbdev.export.__all__\n",
213-
" if p not in [\"nb_export\", \"ExportModuleProc\", \"optional_procs\"]])"
214+
" if p not in [\"nb_export\", \"nb_export_cli\", \"ExportModuleProc\", \"optional_procs\"]])"
214215
]
215216
},
216217
{
@@ -237,9 +238,15 @@
237238
"outputs": [],
238239
"source": [
239240
"#|export\n",
240-
"def nb_export(nbname, lib_path=None, procs=None, debug=False, mod_maker=ModuleMaker, name=None):\n",
241+
"def nb_export(nbname:str, # Filename of notebook \n",
242+
" lib_path:str=None, # Path to destination library. If not in a nbdev project, defaults to current directory.\n",
243+
" procs=None, # Processors to use\n",
244+
" name:str=None, # Name of python script {name}.py to create.\n",
245+
" mod_maker=ModuleMaker,\n",
246+
" debug:bool=False, # Debug mode\n",
247+
" ):\n",
241248
" \"Create module(s) from notebook\"\n",
242-
" if lib_path is None: lib_path = get_config().lib_path\n",
249+
" if lib_path is None: lib_path = get_config().lib_path if is_nbdev() else '.'\n",
243250
" exp = ExportModuleProc()\n",
244251
" nb = NBProcessor(nbname, [exp]+L(procs), debug=debug)\n",
245252
" nb.process()\n",
@@ -269,7 +276,7 @@
269276
"metadata": {},
270277
"outputs": [],
271278
"source": [
272-
" #|eval: false\n",
279+
"#|eval: false\n",
273280
"shutil.rmtree('tmp', ignore_errors=True)\n",
274281
"nb_export('../../tests/00_some.thing.ipynb', 'tmp')\n",
275282
"\n",

nbs/api/13_cli.ipynb

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"from nbdev.test import *\n",
3939
"from nbdev.clean import *\n",
4040
"from nbdev.quarto import nbdev_readme, refresh_quarto_yml\n",
41+
"from nbdev.export import nb_export\n",
4142
"from nbdev.frontmatter import FrontmatterProc\n",
4243
"\n",
4344
"from execnb.nbio import *\n",
@@ -286,6 +287,21 @@
286287
" print(f\"License updated from {curr_lic} to {to}\")"
287288
]
288289
},
290+
{
291+
"cell_type": "code",
292+
"execution_count": null,
293+
"id": "412b4cd2",
294+
"metadata": {},
295+
"outputs": [],
296+
"source": [
297+
"#|export\n",
298+
"@call_parse\n",
299+
"@delegates(nb_export, but=['procs', 'mod_maker'])\n",
300+
"def nb_export_cli(nbname, lib_path, name, debug:Param(\"debug flag\", store_true), **kwargs): \n",
301+
" \"Export a single nbdev notebook to a python script.\"\n",
302+
" return nb_export(nbname=nbname, lib_path=lib_path, name=name, debug=debug, **kwargs)"
303+
]
304+
},
289305
{
290306
"cell_type": "code",
291307
"execution_count": null,
@@ -332,34 +348,37 @@
332348
"name": "stdout",
333349
"output_type": "stream",
334350
"text": [
335-
"\u001b[1m\u001b[94mnbdev_bump_version\u001b[0m Increment version in settings.ini by one\n",
336-
"\u001b[1m\u001b[94mnbdev_changelog\u001b[0m Create a CHANGELOG.md file from closed and labeled GitHub issues\n",
337-
"\u001b[1m\u001b[94mnbdev_clean\u001b[0m Clean all notebooks in `fname` to avoid merge conflicts\n",
338-
"\u001b[1m\u001b[94mnbdev_conda\u001b[0m Create a `meta.yaml` file ready to be built into a package, and optionally build and upload it\n",
339-
"\u001b[1m\u001b[94mnbdev_create_config\u001b[0m Create a config file.\n",
340-
"\u001b[1m\u001b[94mnbdev_docs\u001b[0m Create Quarto docs and README.md\n",
341-
"\u001b[1m\u001b[94mnbdev_export\u001b[0m Export notebooks in `path` to Python modules\n",
342-
"\u001b[1m\u001b[94mnbdev_filter\u001b[0m A notebook filter for Quarto\n",
343-
"\u001b[1m\u001b[94mnbdev_fix\u001b[0m Create working notebook from conflicted notebook `nbname`\n",
344-
"\u001b[1m\u001b[94mnbdev_help\u001b[0m Show help for all console scripts\n",
345-
"\u001b[1m\u001b[94mnbdev_install\u001b[0m Install Quarto and the current library\n",
346-
"\u001b[1m\u001b[94mnbdev_install_hooks\u001b[0m Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks\n",
347-
"\u001b[1m\u001b[94mnbdev_install_quarto\u001b[0m Install latest Quarto on macOS or Linux, prints instructions for Windows\n",
348-
"\u001b[1m\u001b[94mnbdev_merge\u001b[0m Git merge driver for notebooks\n",
349-
"\u001b[1m\u001b[94mnbdev_migrate\u001b[0m Convert all markdown and notebook files in `path` from v1 to v2\n",
350-
"\u001b[1m\u001b[94mnbdev_new\u001b[0m Create an nbdev project.\n",
351-
"\u001b[1m\u001b[94mnbdev_prepare\u001b[0m Export, test, and clean notebooks, and render README if needed\n",
352-
"\u001b[1m\u001b[94mnbdev_preview\u001b[0m Preview docs locally\n",
353-
"\u001b[1m\u001b[94mnbdev_proc_nbs\u001b[0m Process notebooks in `path` for docs rendering\n",
354-
"\u001b[1m\u001b[94mnbdev_pypi\u001b[0m Create and upload Python package to PyPI\n",
355-
"\u001b[1m\u001b[94mnbdev_readme\u001b[0m None\n",
356-
"\u001b[1m\u001b[94mnbdev_release_both\u001b[0m Release both conda and PyPI packages\n",
357-
"\u001b[1m\u001b[94mnbdev_release_gh\u001b[0m Calls `nbdev_changelog`, lets you edit the result, then pushes to git and calls `nbdev_release_git`\n",
358-
"\u001b[1m\u001b[94mnbdev_release_git\u001b[0m Tag and create a release in GitHub for the current version\n",
359-
"\u001b[1m\u001b[94mnbdev_sidebar\u001b[0m Create sidebar.yml\n",
360-
"\u001b[1m\u001b[94mnbdev_test\u001b[0m Test in parallel notebooks matching `path`, passing along `flags`\n",
361-
"\u001b[1m\u001b[94mnbdev_trust\u001b[0m Trust notebooks matching `fname`\n",
362-
"\u001b[1m\u001b[94mnbdev_update\u001b[0m Propagate change in modules matching `fname` to notebooks that created them\n"
351+
"\u001b[1m\u001b[94mnb_export\u001b[22m\u001b[39m Export a single nbdev notebook to a python script.\n",
352+
"\u001b[1m\u001b[94mnbdev_bump_version\u001b[22m\u001b[39m Increment version in settings.ini by one\n",
353+
"\u001b[1m\u001b[94mnbdev_changelog\u001b[22m\u001b[39m Create a CHANGELOG.md file from closed and labeled GitHub issues\n",
354+
"\u001b[1m\u001b[94mnbdev_clean\u001b[22m\u001b[39m Clean all notebooks in `fname` to avoid merge conflicts\n",
355+
"\u001b[1m\u001b[94mnbdev_conda\u001b[22m\u001b[39m Create a `meta.yaml` file ready to be built into a package, and optionally build and upload it\n",
356+
"\u001b[1m\u001b[94mnbdev_create_config\u001b[22m\u001b[39m Create a config file.\n",
357+
"\u001b[1m\u001b[94mnbdev_docs\u001b[22m\u001b[39m Create Quarto docs and README.md\n",
358+
"\u001b[1m\u001b[94mnbdev_export\u001b[22m\u001b[39m Export notebooks in `path` to Python modules\n",
359+
"\u001b[1m\u001b[94mnbdev_filter\u001b[22m\u001b[39m A notebook filter for Quarto\n",
360+
"\u001b[1m\u001b[94mnbdev_fix\u001b[22m\u001b[39m Create working notebook from conflicted notebook `nbname`\n",
361+
"\u001b[1m\u001b[94mnbdev_help\u001b[22m\u001b[39m Show help for all console scripts\n",
362+
"\u001b[1m\u001b[94mnbdev_install\u001b[22m\u001b[39m Install Quarto and the current library\n",
363+
"\u001b[1m\u001b[94mnbdev_install_hooks\u001b[22m\u001b[39m Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks\n",
364+
"\u001b[1m\u001b[94mnbdev_install_quarto\u001b[22m\u001b[39m Install latest Quarto on macOS or Linux, prints instructions for Windows\n",
365+
"\u001b[1m\u001b[94mnbdev_merge\u001b[22m\u001b[39m Git merge driver for notebooks\n",
366+
"\u001b[1m\u001b[94mnbdev_migrate\u001b[22m\u001b[39m Convert all markdown and notebook files in `path` from v1 to v2\n",
367+
"\u001b[1m\u001b[94mnbdev_new\u001b[22m\u001b[39m Create an nbdev project.\n",
368+
"\u001b[1m\u001b[94mnbdev_prepare\u001b[22m\u001b[39m Export, test, and clean notebooks, and render README if needed\n",
369+
"\u001b[1m\u001b[94mnbdev_preview\u001b[22m\u001b[39m Preview docs locally\n",
370+
"\u001b[1m\u001b[94mnbdev_proc_nbs\u001b[22m\u001b[39m Process notebooks in `path` for docs rendering\n",
371+
"\u001b[1m\u001b[94mnbdev_pypi\u001b[22m\u001b[39m Create and upload Python package to PyPI\n",
372+
"\u001b[1m\u001b[94mnbdev_readme\u001b[22m\u001b[39m Create README.md from readme_nb (index.ipynb by default)\n",
373+
"\u001b[1m\u001b[94mnbdev_release_both\u001b[22m\u001b[39m Release both conda and PyPI packages\n",
374+
"\u001b[1m\u001b[94mnbdev_release_gh\u001b[22m\u001b[39m Calls `nbdev_changelog`, lets you edit the result, then pushes to git and calls `nbdev_release_git`\n",
375+
"\u001b[1m\u001b[94mnbdev_release_git\u001b[22m\u001b[39m Tag and create a release in GitHub for the current version\n",
376+
"\u001b[1m\u001b[94mnbdev_requirements\u001b[22m\u001b[39m Writes a `requirements.txt` file to `directory` based on settings.ini.\n",
377+
"\u001b[1m\u001b[94mnbdev_sidebar\u001b[22m\u001b[39m Create sidebar.yml\n",
378+
"\u001b[1m\u001b[94mnbdev_test\u001b[22m\u001b[39m Test in parallel notebooks matching `path`, passing along `flags`\n",
379+
"\u001b[1m\u001b[94mnbdev_trust\u001b[22m\u001b[39m Trust notebooks matching `fname`\n",
380+
"\u001b[1m\u001b[94mnbdev_update\u001b[22m\u001b[39m Propagate change in modules matching `fname` to notebooks that created them\n",
381+
"\u001b[1m\u001b[94mnbdev_update_license\u001b[22m\u001b[39m Allows you to update the license of your project.\n"
363382
]
364383
}
365384
],

settings.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ console_scripts = nbdev_create_config=nbdev.config:nbdev_create_config
5050
nbdev_requirements=nbdev.release:write_requirements
5151
nbdev_proc_nbs=nbdev.quarto:nbdev_proc_nbs
5252
nbdev_help=nbdev.cli:chelp
53+
nb_export=nbdev.cli:nb_export_cli
5354
tst_flags = notest
5455
nbs_path = nbs
5556
doc_path = _docs

0 commit comments

Comments
 (0)