= '../../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
export
ExportModuleProc
ExportModuleProc ()
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
.
Optional export processors
black_format
black_format (cell, force=False)
Processor to format code with black
Type | Default | Details | |
---|---|---|---|
cell | Cell to format | ||
force | bool | False | Turn black formatting on regardless of 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)
Processor to remove cell magics from exported code
Details | |
---|---|
cell | Cell to format |
scrub_magics
is a processor that scrubs the jupyter “magics” lines out of exported cells. This can be helpful when using tools like sparkmagic or just Jupyter’s builtin magics in an nbdev project.
Usage:
This behavior can be enabled by passing scrub_magics
into the --procs
flag of the nbdev_export
command. - nbdev_export --procs scrub_magics
- nbdev_export --procs 'scrub_magics black_format'
Example:
A cell like below could export the line "hello nbdev"
into the bar
module. And the %%spark
magic line would be omitted.
%%spark
#|export bar
"hello nbdev"
It will export as something similar to this:
# %% ../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 ()
An explicit list of processors that could be used by 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)
Create module(s) from notebook
Type | Default | Details | |
---|---|---|---|
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 |
Let’s check we can import a test file:
'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[
We’ll also check that our ‘everything’ file exports correctly:
'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
That notebook should also export one extra function to 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')