Skip to content

Commit

Permalink
Merge pull request #1489 from radekosmulski/add_exporting_nb_outside_…
Browse files Browse the repository at this point in the history
…nbdev_proj

Add exporting single notebook outside nbdev project
  • Loading branch information
jph00 authored Jan 31, 2025
2 parents 857d911 + 172cafc commit 63fe2b2
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions nbdev/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ def add_init(path=None):
if get_config().get('put_version_in_init', True): update_version(path)

# %% ../nbs/api/01_config.ipynb
def write_cells(cells, hdr, file, offset=0, cell_number=True):
def write_cells(cells, hdr, file, offset=0, cell_number=True, solo_nb=False):
"Write `cells` to `file` along with header `hdr` starting at index `offset` (mainly for nbdev internal use)."
for cell in cells:
if cell.cell_type=='code' and cell.source.strip():
idx = f" {cell.idx_+offset}" if cell_number else ""
file.write(f'\n\n{hdr}{idx}\n{cell.source}')
file.write(f'\n\n{hdr}{idx}\n{cell.source}') if not solo_nb else file.write(f'\n\n{cell.source}')

# %% ../nbs/api/01_config.ipynb
def _basic_export_nb(fname, name, dest=None):
Expand Down
3 changes: 2 additions & 1 deletion nbdev/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def nb_export(nbname:str, # Filename of notebook
name:str=None, # Name of python script {name}.py to create.
mod_maker=ModuleMaker,
debug:bool=False, # Debug mode
solo_nb:bool=False # Export single notebook outside of an nbdev project.
):
"Create module(s) from notebook"
if lib_path is None: lib_path = get_config().lib_path if is_nbdev() else '.'
Expand All @@ -88,5 +89,5 @@ def nb_export(nbname:str, # Filename of notebook
"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=nm, nb_path=nbname, is_new=bool(name) or mod=='#')
mm = mod_maker(dest=lib_path, name=nm, nb_path=nbname, is_new=bool(name) or mod=='#', solo_nb=solo_nb)
mm.make(cells, all_cells, lib_path=lib_path)
6 changes: 3 additions & 3 deletions nbdev/maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def update_var(varname, func, fn=None, code=None):
# %% ../nbs/api/02_maker.ipynb
class ModuleMaker:
"Helper class to create exported library from notebook source cells"
def __init__(self, dest, name, nb_path, is_new=True, parse=True):
def __init__(self, dest, name, nb_path, is_new=True, parse=True, solo_nb=False):
dest,nb_path = Path(dest),Path(nb_path)
store_attr()
self.fname = dest/(name.replace('.','/') + ".py")
Expand Down Expand Up @@ -208,8 +208,8 @@ def make(self:ModuleMaker, cells, all_cells=None, lib_path=None):
f.write(_retr_mdoc(cells))
f.write(f"# AUTOGENERATED! DO NOT EDIT! File to edit: {self.dest2nb}.")
if last_future > 0: write_cells(cells[:last_future], self.hdr, f)
if self.parse: f.write(f"\n\n# %% auto 0\n__all__ = {all_str}")
write_cells(cells[last_future:], self.hdr, f, cell_number=get_config().cell_number)
if self.parse and not self.solo_nb: f.write(f"\n\n# %% auto 0\n__all__ = {all_str}")
write_cells(cells[last_future:], self.hdr, f, cell_number=get_config().cell_number, solo_nb=self.solo_nb)
f.write('\n')

# %% ../nbs/api/02_maker.ipynb
Expand Down
4 changes: 2 additions & 2 deletions nbs/api/01_config.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -735,12 +735,12 @@
"outputs": [],
"source": [
"#|export\n",
"def write_cells(cells, hdr, file, offset=0, cell_number=True):\n",
"def write_cells(cells, hdr, file, offset=0, cell_number=True, solo_nb=False):\n",
" \"Write `cells` to `file` along with header `hdr` starting at index `offset` (mainly for nbdev internal use).\"\n",
" for cell in cells:\n",
" if cell.cell_type=='code' and cell.source.strip():\n",
" idx = f\" {cell.idx_+offset}\" if cell_number else \"\"\n",
" file.write(f'\\n\\n{hdr}{idx}\\n{cell.source}')"
" file.write(f'\\n\\n{hdr}{idx}\\n{cell.source}') if not solo_nb else file.write(f'\\n\\n{cell.source}')"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions nbs/api/02_maker.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
"#|export\n",
"class ModuleMaker:\n",
" \"Helper class to create exported library from notebook source cells\"\n",
" def __init__(self, dest, name, nb_path, is_new=True, parse=True):\n",
" def __init__(self, dest, name, nb_path, is_new=True, parse=True, solo_nb=False):\n",
" dest,nb_path = Path(dest),Path(nb_path)\n",
" store_attr()\n",
" self.fname = dest/(name.replace('.','/') + \".py\")\n",
Expand Down Expand Up @@ -518,8 +518,8 @@
" f.write(_retr_mdoc(cells))\n",
" f.write(f\"# AUTOGENERATED! DO NOT EDIT! File to edit: {self.dest2nb}.\")\n",
" if last_future > 0: write_cells(cells[:last_future], self.hdr, f)\n",
" if self.parse: f.write(f\"\\n\\n# %% auto 0\\n__all__ = {all_str}\")\n",
" write_cells(cells[last_future:], self.hdr, f, cell_number=get_config().cell_number)\n",
" if self.parse and not self.solo_nb: f.write(f\"\\n\\n# %% auto 0\\n__all__ = {all_str}\")\n",
" write_cells(cells[last_future:], self.hdr, f, cell_number=get_config().cell_number, solo_nb=self.solo_nb)\n",
" f.write('\\n')"
]
},
Expand Down
3 changes: 2 additions & 1 deletion nbs/api/04_export.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
" name:str=None, # Name of python script {name}.py to create.\n",
" mod_maker=ModuleMaker,\n",
" debug:bool=False, # Debug mode\n",
" solo_nb:bool=False # Export single notebook outside of an nbdev project.\n",
" ):\n",
" \"Create module(s) from notebook\"\n",
" if lib_path is None: lib_path = get_config().lib_path if is_nbdev() else '.'\n",
Expand All @@ -259,7 +260,7 @@
" \"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=nm, nb_path=nbname, is_new=bool(name) or mod=='#')\n",
" mm = mod_maker(dest=lib_path, name=nm, nb_path=nbname, is_new=bool(name) or mod=='#', solo_nb=solo_nb)\n",
" mm.make(cells, all_cells, lib_path=lib_path)"
]
},
Expand Down

0 comments on commit 63fe2b2

Please sign in to comment.