Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make nb_export a cli [Enhancement] #1450

Merged
merged 2 commits into from
Sep 20, 2024
Merged
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
1 change: 1 addition & 0 deletions nbdev/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'nbdev.cli._update_repo_meta': ('api/cli.html#_update_repo_meta', 'nbdev/cli.py'),
'nbdev.cli.chelp': ('api/cli.html#chelp', 'nbdev/cli.py'),
'nbdev.cli.extract_tgz': ('api/cli.html#extract_tgz', 'nbdev/cli.py'),
'nbdev.cli.nb_export_cli': ('api/cli.html#nb_export_cli', 'nbdev/cli.py'),
'nbdev.cli.nbdev_filter': ('api/cli.html#nbdev_filter', 'nbdev/cli.py'),
'nbdev.cli.nbdev_new': ('api/cli.html#nbdev_new', 'nbdev/cli.py'),
'nbdev.cli.nbdev_update_license': ('api/cli.html#nbdev_update_license', 'nbdev/cli.py')},
Expand Down
12 changes: 11 additions & 1 deletion nbdev/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .test import *
from .clean import *
from .quarto import nbdev_readme, refresh_quarto_yml
from .export import nb_export
from .frontmatter import FrontmatterProc

from execnb.nbio import *
Expand All @@ -27,7 +28,7 @@
import os, tarfile, sys

# %% auto 0
__all__ = ['mapping', 'nbdev_filter', 'extract_tgz', 'nbdev_new', 'nbdev_update_license', 'chelp']
__all__ = ['mapping', 'nbdev_filter', 'extract_tgz', 'nbdev_new', 'nbdev_update_license', 'nb_export_cli', 'chelp']

# %% ../nbs/api/13_cli.ipynb
@call_parse
Expand Down Expand Up @@ -158,6 +159,15 @@ def nbdev_update_license(
lic.write(body)
print(f"License updated from {curr_lic} to {to}")

# %% ../nbs/api/13_cli.ipynb
@call_parse
@delegates(nb_export, but=['procs', 'mod_maker'])
def nb_export_cli(nbname,
debug:store_true=False, # Debug flag
**kwargs):
"Export a single nbdev notebook to a python script."
return nb_export(nbname=nbname, debug=debug, **kwargs)

# %% ../nbs/api/13_cli.ipynb
@call_parse
def chelp():
Expand Down
13 changes: 10 additions & 3 deletions nbdev/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from fastcore.script import *
from fastcore.basics import *
from fastcore.imports import *
from fastcore.meta import *

from collections import defaultdict

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

# %% ../nbs/api/04_export.ipynb
def nb_export(nbname, lib_path=None, procs=None, debug=False, mod_maker=ModuleMaker, name=None):
def nb_export(nbname:str, # Filename of notebook
lib_path:str=None, # Path to destination library. If not in a nbdev project, defaults to current directory.
procs=None, # Processors to use
name:str=None, # Name of python script {name}.py to create.
mod_maker=ModuleMaker,
debug:bool=False, # Debug mode
):
"Create module(s) from notebook"
if lib_path is None: lib_path = get_config().lib_path
if lib_path is None: lib_path = get_config().lib_path if is_nbdev() else '.'
exp = ExportModuleProc()
nb = NBProcessor(nbname, [exp]+L(procs), debug=debug)
nb.process()
Expand Down
15 changes: 11 additions & 4 deletions nbs/api/04_export.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"from fastcore.script import *\n",
"from fastcore.basics import *\n",
"from fastcore.imports import *\n",
"from fastcore.meta import *\n",
"\n",
"from collections import defaultdict"
]
Expand Down Expand Up @@ -210,7 +211,7 @@
"def optional_procs():\n",
" \"An explicit list of processors that could be used by `nb_export`\"\n",
" return L([p for p in nbdev.export.__all__\n",
" if p not in [\"nb_export\", \"ExportModuleProc\", \"optional_procs\"]])"
" if p not in [\"nb_export\", \"nb_export_cli\", \"ExportModuleProc\", \"optional_procs\"]])"
]
},
{
Expand All @@ -237,9 +238,15 @@
"outputs": [],
"source": [
"#|export\n",
"def nb_export(nbname, lib_path=None, procs=None, debug=False, mod_maker=ModuleMaker, name=None):\n",
"def nb_export(nbname:str, # Filename of notebook \n",
" lib_path:str=None, # Path to destination library. If not in a nbdev project, defaults to current directory.\n",
" procs=None, # Processors to use\n",
" name:str=None, # Name of python script {name}.py to create.\n",
" mod_maker=ModuleMaker,\n",
" debug:bool=False, # Debug mode\n",
" ):\n",
" \"Create module(s) from notebook\"\n",
" if lib_path is None: lib_path = get_config().lib_path\n",
" if lib_path is None: lib_path = get_config().lib_path if is_nbdev() else '.'\n",
" exp = ExportModuleProc()\n",
" nb = NBProcessor(nbname, [exp]+L(procs), debug=debug)\n",
" nb.process()\n",
Expand Down Expand Up @@ -269,7 +276,7 @@
"metadata": {},
"outputs": [],
"source": [
" #|eval: false\n",
"#|eval: false\n",
"shutil.rmtree('tmp', ignore_errors=True)\n",
"nb_export('../../tests/00_some.thing.ipynb', 'tmp')\n",
"\n",
Expand Down
77 changes: 49 additions & 28 deletions nbs/api/13_cli.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"from nbdev.test import *\n",
"from nbdev.clean import *\n",
"from nbdev.quarto import nbdev_readme, refresh_quarto_yml\n",
"from nbdev.export import nb_export\n",
"from nbdev.frontmatter import FrontmatterProc\n",
"\n",
"from execnb.nbio import *\n",
Expand Down Expand Up @@ -286,6 +287,23 @@
" print(f\"License updated from {curr_lic} to {to}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "412b4cd2",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"@call_parse\n",
"@delegates(nb_export, but=['procs', 'mod_maker'])\n",
"def nb_export_cli(nbname, \n",
" debug:store_true=False, # Debug flag \n",
" **kwargs): \n",
" \"Export a single nbdev notebook to a python script.\"\n",
" return nb_export(nbname=nbname, debug=debug, **kwargs)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -332,34 +350,37 @@
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m\u001b[94mnbdev_bump_version\u001b[0m Increment version in settings.ini by one\n",
"\u001b[1m\u001b[94mnbdev_changelog\u001b[0m Create a CHANGELOG.md file from closed and labeled GitHub issues\n",
"\u001b[1m\u001b[94mnbdev_clean\u001b[0m Clean all notebooks in `fname` to avoid merge conflicts\n",
"\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",
"\u001b[1m\u001b[94mnbdev_create_config\u001b[0m Create a config file.\n",
"\u001b[1m\u001b[94mnbdev_docs\u001b[0m Create Quarto docs and README.md\n",
"\u001b[1m\u001b[94mnbdev_export\u001b[0m Export notebooks in `path` to Python modules\n",
"\u001b[1m\u001b[94mnbdev_filter\u001b[0m A notebook filter for Quarto\n",
"\u001b[1m\u001b[94mnbdev_fix\u001b[0m Create working notebook from conflicted notebook `nbname`\n",
"\u001b[1m\u001b[94mnbdev_help\u001b[0m Show help for all console scripts\n",
"\u001b[1m\u001b[94mnbdev_install\u001b[0m Install Quarto and the current library\n",
"\u001b[1m\u001b[94mnbdev_install_hooks\u001b[0m Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks\n",
"\u001b[1m\u001b[94mnbdev_install_quarto\u001b[0m Install latest Quarto on macOS or Linux, prints instructions for Windows\n",
"\u001b[1m\u001b[94mnbdev_merge\u001b[0m Git merge driver for notebooks\n",
"\u001b[1m\u001b[94mnbdev_migrate\u001b[0m Convert all markdown and notebook files in `path` from v1 to v2\n",
"\u001b[1m\u001b[94mnbdev_new\u001b[0m Create an nbdev project.\n",
"\u001b[1m\u001b[94mnbdev_prepare\u001b[0m Export, test, and clean notebooks, and render README if needed\n",
"\u001b[1m\u001b[94mnbdev_preview\u001b[0m Preview docs locally\n",
"\u001b[1m\u001b[94mnbdev_proc_nbs\u001b[0m Process notebooks in `path` for docs rendering\n",
"\u001b[1m\u001b[94mnbdev_pypi\u001b[0m Create and upload Python package to PyPI\n",
"\u001b[1m\u001b[94mnbdev_readme\u001b[0m None\n",
"\u001b[1m\u001b[94mnbdev_release_both\u001b[0m Release both conda and PyPI packages\n",
"\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",
"\u001b[1m\u001b[94mnbdev_release_git\u001b[0m Tag and create a release in GitHub for the current version\n",
"\u001b[1m\u001b[94mnbdev_sidebar\u001b[0m Create sidebar.yml\n",
"\u001b[1m\u001b[94mnbdev_test\u001b[0m Test in parallel notebooks matching `path`, passing along `flags`\n",
"\u001b[1m\u001b[94mnbdev_trust\u001b[0m Trust notebooks matching `fname`\n",
"\u001b[1m\u001b[94mnbdev_update\u001b[0m Propagate change in modules matching `fname` to notebooks that created them\n"
"\u001b[1m\u001b[94mnb_export\u001b[22m\u001b[39m Export a single nbdev notebook to a python script.\n",
"\u001b[1m\u001b[94mnbdev_bump_version\u001b[22m\u001b[39m Increment version in settings.ini by one\n",
"\u001b[1m\u001b[94mnbdev_changelog\u001b[22m\u001b[39m Create a CHANGELOG.md file from closed and labeled GitHub issues\n",
"\u001b[1m\u001b[94mnbdev_clean\u001b[22m\u001b[39m Clean all notebooks in `fname` to avoid merge conflicts\n",
"\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",
"\u001b[1m\u001b[94mnbdev_create_config\u001b[22m\u001b[39m Create a config file.\n",
"\u001b[1m\u001b[94mnbdev_docs\u001b[22m\u001b[39m Create Quarto docs and README.md\n",
"\u001b[1m\u001b[94mnbdev_export\u001b[22m\u001b[39m Export notebooks in `path` to Python modules\n",
"\u001b[1m\u001b[94mnbdev_filter\u001b[22m\u001b[39m A notebook filter for Quarto\n",
"\u001b[1m\u001b[94mnbdev_fix\u001b[22m\u001b[39m Create working notebook from conflicted notebook `nbname`\n",
"\u001b[1m\u001b[94mnbdev_help\u001b[22m\u001b[39m Show help for all console scripts\n",
"\u001b[1m\u001b[94mnbdev_install\u001b[22m\u001b[39m Install Quarto and the current library\n",
"\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",
"\u001b[1m\u001b[94mnbdev_install_quarto\u001b[22m\u001b[39m Install latest Quarto on macOS or Linux, prints instructions for Windows\n",
"\u001b[1m\u001b[94mnbdev_merge\u001b[22m\u001b[39m Git merge driver for notebooks\n",
"\u001b[1m\u001b[94mnbdev_migrate\u001b[22m\u001b[39m Convert all markdown and notebook files in `path` from v1 to v2\n",
"\u001b[1m\u001b[94mnbdev_new\u001b[22m\u001b[39m Create an nbdev project.\n",
"\u001b[1m\u001b[94mnbdev_prepare\u001b[22m\u001b[39m Export, test, and clean notebooks, and render README if needed\n",
"\u001b[1m\u001b[94mnbdev_preview\u001b[22m\u001b[39m Preview docs locally\n",
"\u001b[1m\u001b[94mnbdev_proc_nbs\u001b[22m\u001b[39m Process notebooks in `path` for docs rendering\n",
"\u001b[1m\u001b[94mnbdev_pypi\u001b[22m\u001b[39m Create and upload Python package to PyPI\n",
"\u001b[1m\u001b[94mnbdev_readme\u001b[22m\u001b[39m Create README.md from readme_nb (index.ipynb by default)\n",
"\u001b[1m\u001b[94mnbdev_release_both\u001b[22m\u001b[39m Release both conda and PyPI packages\n",
"\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",
"\u001b[1m\u001b[94mnbdev_release_git\u001b[22m\u001b[39m Tag and create a release in GitHub for the current version\n",
"\u001b[1m\u001b[94mnbdev_requirements\u001b[22m\u001b[39m Writes a `requirements.txt` file to `directory` based on settings.ini.\n",
"\u001b[1m\u001b[94mnbdev_sidebar\u001b[22m\u001b[39m Create sidebar.yml\n",
"\u001b[1m\u001b[94mnbdev_test\u001b[22m\u001b[39m Test in parallel notebooks matching `path`, passing along `flags`\n",
"\u001b[1m\u001b[94mnbdev_trust\u001b[22m\u001b[39m Trust notebooks matching `fname`\n",
"\u001b[1m\u001b[94mnbdev_update\u001b[22m\u001b[39m Propagate change in modules matching `fname` to notebooks that created them\n",
"\u001b[1m\u001b[94mnbdev_update_license\u001b[22m\u001b[39m Allows you to update the license of your project.\n"
]
}
],
Expand Down
1 change: 1 addition & 0 deletions settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ console_scripts = nbdev_create_config=nbdev.config:nbdev_create_config
nbdev_requirements=nbdev.release:write_requirements
nbdev_proc_nbs=nbdev.quarto:nbdev_proc_nbs
nbdev_help=nbdev.cli:chelp
nb_export=nbdev.cli:nb_export_cli
tst_flags = notest
nbs_path = nbs
doc_path = _docs
Expand Down
Loading