Skip to content

Commit 63fe2b2

Browse files
authored
Merge pull request #1489 from radekosmulski/add_exporting_nb_outside_nbdev_proj
Add exporting single notebook outside nbdev project
2 parents 857d911 + 172cafc commit 63fe2b2

File tree

6 files changed

+14
-12
lines changed

6 files changed

+14
-12
lines changed

nbdev/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ def add_init(path=None):
249249
if get_config().get('put_version_in_init', True): update_version(path)
250250

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

259259
# %% ../nbs/api/01_config.ipynb
260260
def _basic_export_nb(fname, name, dest=None):

nbdev/export.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def nb_export(nbname:str, # Filename of notebook
7373
name:str=None, # Name of python script {name}.py to create.
7474
mod_maker=ModuleMaker,
7575
debug:bool=False, # Debug mode
76+
solo_nb:bool=False # Export single notebook outside of an nbdev project.
7677
):
7778
"Create module(s) from notebook"
7879
if lib_path is None: lib_path = get_config().lib_path if is_nbdev() else '.'
@@ -88,5 +89,5 @@ def nb_export(nbname:str, # Filename of notebook
8889
"Note nbdev2 no longer supports nbdev1 syntax. Run `nbdev_migrate` to upgrade.\n"
8990
"See https://nbdev.fast.ai/getting_started.html for more information.")
9091
return
91-
mm = mod_maker(dest=lib_path, name=nm, nb_path=nbname, is_new=bool(name) or mod=='#')
92+
mm = mod_maker(dest=lib_path, name=nm, nb_path=nbname, is_new=bool(name) or mod=='#', solo_nb=solo_nb)
9293
mm.make(cells, all_cells, lib_path=lib_path)

nbdev/maker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def update_var(varname, func, fn=None, code=None):
6363
# %% ../nbs/api/02_maker.ipynb
6464
class ModuleMaker:
6565
"Helper class to create exported library from notebook source cells"
66-
def __init__(self, dest, name, nb_path, is_new=True, parse=True):
66+
def __init__(self, dest, name, nb_path, is_new=True, parse=True, solo_nb=False):
6767
dest,nb_path = Path(dest),Path(nb_path)
6868
store_attr()
6969
self.fname = dest/(name.replace('.','/') + ".py")
@@ -208,8 +208,8 @@ def make(self:ModuleMaker, cells, all_cells=None, lib_path=None):
208208
f.write(_retr_mdoc(cells))
209209
f.write(f"# AUTOGENERATED! DO NOT EDIT! File to edit: {self.dest2nb}.")
210210
if last_future > 0: write_cells(cells[:last_future], self.hdr, f)
211-
if self.parse: f.write(f"\n\n# %% auto 0\n__all__ = {all_str}")
212-
write_cells(cells[last_future:], self.hdr, f, cell_number=get_config().cell_number)
211+
if self.parse and not self.solo_nb: f.write(f"\n\n# %% auto 0\n__all__ = {all_str}")
212+
write_cells(cells[last_future:], self.hdr, f, cell_number=get_config().cell_number, solo_nb=self.solo_nb)
213213
f.write('\n')
214214

215215
# %% ../nbs/api/02_maker.ipynb

nbs/api/01_config.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,12 +735,12 @@
735735
"outputs": [],
736736
"source": [
737737
"#|export\n",
738-
"def write_cells(cells, hdr, file, offset=0, cell_number=True):\n",
738+
"def write_cells(cells, hdr, file, offset=0, cell_number=True, solo_nb=False):\n",
739739
" \"Write `cells` to `file` along with header `hdr` starting at index `offset` (mainly for nbdev internal use).\"\n",
740740
" for cell in cells:\n",
741741
" if cell.cell_type=='code' and cell.source.strip():\n",
742742
" idx = f\" {cell.idx_+offset}\" if cell_number else \"\"\n",
743-
" file.write(f'\\n\\n{hdr}{idx}\\n{cell.source}')"
743+
" file.write(f'\\n\\n{hdr}{idx}\\n{cell.source}') if not solo_nb else file.write(f'\\n\\n{cell.source}')"
744744
]
745745
},
746746
{

nbs/api/02_maker.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
"#|export\n",
202202
"class ModuleMaker:\n",
203203
" \"Helper class to create exported library from notebook source cells\"\n",
204-
" def __init__(self, dest, name, nb_path, is_new=True, parse=True):\n",
204+
" def __init__(self, dest, name, nb_path, is_new=True, parse=True, solo_nb=False):\n",
205205
" dest,nb_path = Path(dest),Path(nb_path)\n",
206206
" store_attr()\n",
207207
" self.fname = dest/(name.replace('.','/') + \".py\")\n",
@@ -518,8 +518,8 @@
518518
" f.write(_retr_mdoc(cells))\n",
519519
" f.write(f\"# AUTOGENERATED! DO NOT EDIT! File to edit: {self.dest2nb}.\")\n",
520520
" if last_future > 0: write_cells(cells[:last_future], self.hdr, f)\n",
521-
" if self.parse: f.write(f\"\\n\\n# %% auto 0\\n__all__ = {all_str}\")\n",
522-
" write_cells(cells[last_future:], self.hdr, f, cell_number=get_config().cell_number)\n",
521+
" if self.parse and not self.solo_nb: f.write(f\"\\n\\n# %% auto 0\\n__all__ = {all_str}\")\n",
522+
" write_cells(cells[last_future:], self.hdr, f, cell_number=get_config().cell_number, solo_nb=self.solo_nb)\n",
523523
" f.write('\\n')"
524524
]
525525
},

nbs/api/04_export.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
" name:str=None, # Name of python script {name}.py to create.\n",
245245
" mod_maker=ModuleMaker,\n",
246246
" debug:bool=False, # Debug mode\n",
247+
" solo_nb:bool=False # Export single notebook outside of an nbdev project.\n",
247248
" ):\n",
248249
" \"Create module(s) from notebook\"\n",
249250
" if lib_path is None: lib_path = get_config().lib_path if is_nbdev() else '.'\n",
@@ -259,7 +260,7 @@
259260
" \"Note nbdev2 no longer supports nbdev1 syntax. Run `nbdev_migrate` to upgrade.\\n\"\n",
260261
" \"See https://nbdev.fast.ai/getting_started.html for more information.\")\n",
261262
" return\n",
262-
" mm = mod_maker(dest=lib_path, name=nm, nb_path=nbname, is_new=bool(name) or mod=='#')\n",
263+
" mm = mod_maker(dest=lib_path, name=nm, nb_path=nbname, is_new=bool(name) or mod=='#', solo_nb=solo_nb)\n",
263264
" mm.make(cells, all_cells, lib_path=lib_path)"
264265
]
265266
},

0 commit comments

Comments
 (0)