find_var
def find_var(
lines, varname
):Find the line numbers where varname is defined in lines
These functions let us find and modify the definitions of variables in Python modules.
Find the line numbers where varname is defined in lines
Update the definition of varname in file fn, by calling func with the current definition
Helper class to create exported library from notebook source cells
In order to export a notebook, we need an way to create a Python file. ModuleMaker fills that role. Pass in the directory where you want to module created, the name of the module, the path of the notebook source, and set is_new to True if this is a new file being created (rather than an existing file being added to). The location of the saved module will be in fname. Finally, if the source in the notebooks should not be parsed by Python (such as partial class declarations in cells), parse should be set to False.
Note: If doing so, then the
__all__generation will be turned off as well.
Path('tmp/test/testing.py')
id attr of decorator, regardless of whether called as function or bare
We want to add an __all__ to the top of the exported module. This methods autogenerates it from all code in cells.
Convert a module name to a name relative to fname
test_eq(relative_import('nbdev.core', "xyz"), 'nbdev.core')
test_eq(relative_import('nbdev.core', 'nbdev'), '.core')
_p = Path('fastai')
test_eq(relative_import('fastai.core', _p/'vision'), '..core')
test_eq(relative_import('fastai.core', _p/'vision/transform'), '...core')
test_eq(relative_import('fastai.vision.transform', _p/'vision'), '.transform')
test_eq(relative_import('fastai.notebook.core', _p/'data'), '..notebook.core')
test_eq(relative_import('fastai.vision', _p/'vision'), '.')
test_eq(relative_import('fastai', _p), '.')
test_eq(relative_import('fastai', _p/'vision'), '..')
test_eq(relative_import('fastai', _p/'vision/transform'), '...')ss = "from nbdev.export import *\nfrom nbdev.a.b import *"
cell = make_code_cells([ss])[0]
cell.import2relative('nbdev')
test_eq(cell.source, 'from .export import *\nfrom .a.b import *')
cell = make_code_cells([ss])[0]
cell.import2relative('nbdev/a')
test_eq(cell.source, 'from ..export import *\nfrom .b import *')Write module containing cells with __all__ generated from all_cells
Pass all_cells=[] or parse=False if you don’t want any __all__ added.
Passing parse=False is also handy for when writing broken up functions or classes that ast.parse might not like but still want it to be exported, such as having a cell with:
Note that by doing so we cannot properly generate a __all__, so we assume that it is unwanted.
Path('tmp/test/testing_noall.py')
If is_new=False then the additional definitions are added to the bottom, and any existing __all__ is updated with the newly-added symbols.
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../04_export.ipynb.
# %% ../../04_export.ipynb #bc0f5e48
from __future__ import print_function
# %% auto #0
__all__ = ['b', 'c', 'd']
# %% ../../04_export.ipynb #bacd036d
#| export
def a(): ...
# %% ../../04_export.ipynb #7812454e
def b(): ...
# %% ../../04_export.ipynb #b6972b0c
def c(): ...
# %% ../../04_export.ipynb #0de045a9
def d(): ...