导出

将 notebook 导出为库

source

ExportModuleProc

 ExportModuleProc ()

一个将代码导出到模块的处理器

指定模块将导出到的 dest,以及可选用于创建模块的类(默认为 ModuleMaker)。

导出的单元格存储在名为 modulesdict 中,其键是导出到的模块。没有显式指定模块的单元格存储在 '#' 键下,将被导出到 default_exp

everything_fn = '../../tests/01_everything.ipynb'

exp = ExportModuleProc()
proc = NBProcessor(everything_fn, exp)
proc.process()
test_eq(exp.default_exp, 'everything')
assert 'print_function'  in exp.modules['#'][1].source
assert 'h_n' in exp.in_all['some.thing'][0].source

可选的导出处理器


source

black_format

 black_format (cell, force=False)

使用 black 格式化代码的处理器

类型 默认值 详情
cell 要格式化的单元格
force bool False 开启 black 格式化,忽略 settings.ini 的设置
_cell = read_nb('../../tests/export_procs.ipynb')['cells'][0]
black_format(_cell, force=True)
test_eq(_cell.source, 'j = [1, 2, 3]')

source

scrub_magics

 scrub_magics (cell)

从导出的代码中移除单元格魔法命令的处理器

详情
cell 要格式化的单元格

scrub_magics 是一个处理器,用于清除导出单元格中的 jupyter “魔法命令”行。这在使用像 sparkmagic 或 Jupyter 内置魔法命令时非常有用。

用法
通过将 scrub_magics 传递给 nbdev_export 命令的 --procs 标志可以启用此行为。 - nbdev_export --procs scrub_magics - nbdev_export --procs 'scrub_magics black_format'

示例

如下所示的单元格可以将 "hello nbdev" 这一行导出到 bar 模块中。并且 %%spark 魔法命令行将被忽略。

%%spark
#|export bar
"hello nbdev"

它将导出为类似于此的内容

# %% ../path/to/01_bar.ipynb 1
"hello nbdev"
_cell = read_nb('../../tests/export_procs.ipynb')['cells'][2]
scrub_magics(_cell)
test_eq(_cell.source, '''#|export bar
"hello nbdev"''')

source

optional_procs

 optional_procs ()

一个可供 nb_export 使用的显式处理器列表

# every optional processor should be explicitly listed here
test_eq(optional_procs(), ['black_format', 'scrub_magics'])

nb_export


source

nb_export

 nb_export (nbname:str, lib_path:str=None, procs=None, name:str=None,
            mod_maker=<class 'nbdev.maker.ModuleMaker'>, debug:bool=False,
            solo_nb:bool=False)

从 notebook 创建模块

类型 默认值 详情
nbname str Notebook 文件名
lib_path str None 目标库路径。如果不在 nbdev 项目中,默认为当前目录。
procs NoneType None 要使用的处理器
name str None 要创建的 Python 脚本 {name}.py 的名称。
mod_maker type ModuleMaker
debug bool False 调试模式
solo_nb bool False 在 nbdev 项目之外导出单个 notebook。

让我们检查是否可以导入一个测试文件

shutil.rmtree('tmp', ignore_errors=True)
nb_export('../../tests/00_some.thing.ipynb', 'tmp')

g = exec_new('import tmp.some.thing')
test_eq(g['tmp'].some.thing.__all__, ['a'])
test_eq(g['tmp'].some.thing.a, 1)

我们还将检查我们的 'everything' 文件是否正确导出

nb_export(everything_fn, 'tmp')

g = exec_new('import tmp.everything; from tmp.everything import *')
_alls = L("a b d e m n o p q".split())
for s in _alls.map("{}_y"): assert s in g, s
for s in "c_y_nall _f_y_nall g_n h_n i_n j_n k_n l_n".split(): assert s not in g, s
for s in _alls.map("{}_y") + ["c_y_nall", "_f_y_nall"]: assert hasattr(g['tmp'].everything,s), s

该 notebook 还应将一个额外的函数导出到 tmp.some.thing

del(sys.modules['tmp.some.thing']) # remove from module cache
g = exec_new('import tmp.some.thing')
test_eq(g['tmp'].some.thing.__all__, ['a','h_n'])
test_eq(g['tmp'].some.thing.h_n(), None)
Path('../nbdev/export.py').unlink(missing_ok=True)
nb_export('04_export.ipynb')

g = exec_new('import nbdev.export')
assert hasattr(g['nbdev'].export, 'nb_export')