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].sourceexport
Exporting a notebook to a library
ExportModuleProc
def ExportModuleProc(
args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):
A processor which exports code to a module
Specify dest where the module(s) will be exported to, and optionally a class to use to create the module (ModuleMaker, by default).
Exported cells are stored in a dict called modules, where the keys are the modules exported to. Those without an explicit module are stored in the '#' key, which will be exported to default_exp.
nb_export
nb_export
def nb_export(
nbname:str, # Filename of notebook
lib_path:str=None, # Path to destination library. If not in a nbdev project, defaults to current directory.
procs:NoneType=None, # Processors to use
name:str=None, # Name of python script {name}.py to create.
mod_maker:type=ModuleMaker, debug:bool=False, # Debug mode
solo_nb:bool=False, # Export single notebook outside of an nbdev project.
):
Create module(s) from notebook
Let’s check we can import a test file:
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)We’ll also check that our ‘everything’ file exports correctly:
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), sThat notebook should also export one extra function to 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')