diff --git a/nbdev/export.py b/nbdev/export.py index 5bf813d75..7a6bf785e 100644 --- a/nbdev/export.py +++ b/nbdev/export.py @@ -41,7 +41,7 @@ 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() @@ -49,11 +49,11 @@ def nb_export(nbname, lib_path=None, procs=black_format, debug=False, mod_maker= 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) diff --git a/nbs/api/export.ipynb b/nbs/api/export.ipynb index a579ad13b..a681147c0 100644 --- a/nbs/api/export.ipynb +++ b/nbs/api/export.ipynb @@ -134,7 +134,7 @@ "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", @@ -142,13 +142,13 @@ " 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)" ] },