Skip to content

Commit

Permalink
Merge pull request #1204 from fastai/allow-export-to-script
Browse files Browse the repository at this point in the history
add parameter for name to nb_export
  • Loading branch information
jph00 authored Nov 7, 2022
2 parents f7dd526 + b7d36ef commit 8d9bbea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions nbdev/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ def black_format(cell, # Cell to format
except: pass

# %% ../nbs/api/export.ipynb 9
def nb_export(nbname, lib_path=None, procs=black_format, debug=False, mod_maker=ModuleMaker):
def nb_export(nbname, lib_path=None, procs=black_format, debug=False, mod_maker=ModuleMaker, name=None):
"Create module(s) from notebook"
if lib_path is None: lib_path = get_config().lib_path
exp = ExportModuleProc()
nb = NBProcessor(nbname, [exp]+L(procs), debug=debug)
nb.process()
for mod,cells in exp.modules.items():
all_cells = exp.in_all[mod]
name = getattr(exp, 'default_exp', None) if mod=='#' else mod
if not name:
nm = ifnone(name, getattr(exp, 'default_exp', None) if mod=='#' else mod)
if not nm:
warn(f"Notebook '{nbname}' uses `#|export` without `#|default_exp` cell.\n"
"Note nbdev2 no longer supports nbdev1 syntax. Run `nbdev_migrate` to upgrade.\n"
"See https://nbdev.fast.ai/getting_started.html for more information.")
return
mm = mod_maker(dest=lib_path, name=name, nb_path=nbname, is_new=mod=='#')
mm = mod_maker(dest=lib_path, name=nm, nb_path=nbname, is_new=bool(name) or mod=='#')
mm.make(cells, all_cells, lib_path=lib_path)
8 changes: 4 additions & 4 deletions nbs/api/export.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,21 @@
"outputs": [],
"source": [
"#|export\n",
"def nb_export(nbname, lib_path=None, procs=black_format, debug=False, mod_maker=ModuleMaker):\n",
"def nb_export(nbname, lib_path=None, procs=black_format, debug=False, mod_maker=ModuleMaker, name=None):\n",
" \"Create module(s) from notebook\"\n",
" if lib_path is None: lib_path = get_config().lib_path\n",
" exp = ExportModuleProc()\n",
" nb = NBProcessor(nbname, [exp]+L(procs), debug=debug)\n",
" nb.process()\n",
" for mod,cells in exp.modules.items():\n",
" all_cells = exp.in_all[mod]\n",
" name = getattr(exp, 'default_exp', None) if mod=='#' else mod\n",
" if not name:\n",
" nm = ifnone(name, getattr(exp, 'default_exp', None) if mod=='#' else mod)\n",
" if not nm:\n",
" warn(f\"Notebook '{nbname}' uses `#|export` without `#|default_exp` cell.\\n\"\n",
" \"Note nbdev2 no longer supports nbdev1 syntax. Run `nbdev_migrate` to upgrade.\\n\"\n",
" \"See https://nbdev.fast.ai/getting_started.html for more information.\")\n",
" return\n",
" mm = mod_maker(dest=lib_path, name=name, nb_path=nbname, is_new=mod=='#')\n",
" mm = mod_maker(dest=lib_path, name=nm, nb_path=nbname, is_new=bool(name) or mod=='#')\n",
" mm.make(cells, all_cells, lib_path=lib_path)"
]
},
Expand Down

0 comments on commit 8d9bbea

Please sign in to comment.