= '../../tests/01_everything.ipynb'
everything_fn
= ExportModuleProc()
exp = NBProcessor(everything_fn, exp)
proc
proc.process()'everything')
test_eq(exp.default_exp, assert 'print_function' in exp.modules['#'][1].source
assert 'h_n' in exp.in_all['some.thing'][0].source
导出
将 notebook 导出为库
ExportModuleProc
ExportModuleProc ()
一个将代码导出到模块的处理器
指定模块将导出到的 dest
,以及可选用于创建模块的类(默认为 ModuleMaker
)。
导出的单元格存储在名为 modules
的 dict
中,其键是导出到的模块。没有显式指定模块的单元格存储在 '#'
键下,将被导出到 default_exp
。
可选的导出处理器
black_format
black_format (cell, force=False)
使用 black
格式化代码的处理器
类型 | 默认值 | 详情 | |
---|---|---|---|
cell | 要格式化的单元格 | ||
force | bool | False | 开启 black 格式化,忽略 settings.ini 的设置 |
= read_nb('../../tests/export_procs.ipynb')['cells'][0]
_cell =True)
black_format(_cell, force'j = [1, 2, 3]') test_eq(_cell.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"
= read_nb('../../tests/export_procs.ipynb')['cells'][2]
_cell
scrub_magics(_cell)'''#|export bar
test_eq(_cell.source, "hello nbdev"''')
optional_procs
optional_procs ()
一个可供 nb_export
使用的显式处理器列表
# every optional processor should be explicitly listed here
'black_format', 'scrub_magics']) test_eq(optional_procs(), [
nb_export
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。 |
让我们检查是否可以导入一个测试文件
'tmp', ignore_errors=True)
shutil.rmtree('../../tests/00_some.thing.ipynb', 'tmp')
nb_export(
= exec_new('import tmp.some.thing')
g 'tmp'].some.thing.__all__, ['a'])
test_eq(g['tmp'].some.thing.a, 1) test_eq(g[
我们还将检查我们的 'everything' 文件是否正确导出
'tmp')
nb_export(everything_fn,
= exec_new('import tmp.everything; from tmp.everything import *')
g = L("a b d e m n o p q".split())
_alls 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
= exec_new('import tmp.some.thing')
g 'tmp'].some.thing.__all__, ['a','h_n'])
test_eq(g['tmp'].some.thing.h_n(), None) test_eq(g[
'../nbdev/export.py').unlink(missing_ok=True)
Path('04_export.ipynb')
nb_export(
= exec_new('import nbdev.export')
g assert hasattr(g['nbdev'].export, 'nb_export')