-
Notifications
You must be signed in to change notification settings - Fork 512
exported modules can scrub_magics #1250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9366c7f
f3af3fe
3f7dea8
ca1d792
0f4d622
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/04_export.ipynb. | ||
|
||
# %% auto 0 | ||
__all__ = ['ExportModuleProc', 'black_format', 'nb_export'] | ||
__all__ = ['ExportModuleProc', 'black_format', 'scrub_magics', 'optional_procs', 'nb_export'] | ||
|
||
# %% ../nbs/api/04_export.ipynb 2 | ||
from .config import * | ||
|
@@ -26,7 +26,7 @@ def _export_(self, cell, exp_to=None): | |
self.in_all[ifnone(exp_to, '#')].append(cell) | ||
_exports_=_export_ | ||
|
||
# %% ../nbs/api/04_export.ipynb 7 | ||
# %% ../nbs/api/04_export.ipynb 8 | ||
def black_format(cell, # Cell to format | ||
force=False): # Turn black formatting on regardless of settings.ini | ||
"Processor to format code with `black`" | ||
|
@@ -40,8 +40,27 @@ def black_format(cell, # Cell to format | |
try: cell.source = _format_str(cell.source).strip() | ||
except: pass | ||
|
||
# %% ../nbs/api/04_export.ipynb 9 | ||
def nb_export(nbname, lib_path=None, procs=black_format, debug=False, mod_maker=ModuleMaker, name=None): | ||
# %% ../nbs/api/04_export.ipynb 10 | ||
# includes the newline, because calling .strip() would affect all cells. | ||
_magics_pattern = re.compile(r'^\s*(%%|%).*\n?', re.MULTILINE) | ||
|
||
def scrub_magics(cell): # Cell to format | ||
"Processor to remove cell magics from exported code" | ||
try: cfg = get_config() | ||
except FileNotFoundError: return | ||
if cell.cell_type != 'code': return | ||
try: cell.source = _magics_pattern.sub('', cell.source) | ||
except: pass | ||
|
||
# %% ../nbs/api/04_export.ipynb 13 | ||
import nbdev.export | ||
def optional_procs(): | ||
"An explicit list of processors that could be used by `nb_export`" | ||
return L([p for p in nbdev.export.__all__ | ||
if p not in ["nb_export", "ExportModuleProc", "optional_procs"]]) | ||
|
||
# %% ../nbs/api/04_export.ipynb 16 | ||
def nb_export(nbname, lib_path=None, procs=None, debug=False, mod_maker=ModuleMaker, name=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that I changed |
||
"Create module(s) from notebook" | ||
if lib_path is None: lib_path = get_config().lib_path | ||
exp = ExportModuleProc() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic pattern in
clean_magics
(in theprocessors
notebook) is a little different. It doesn't include the newline and relies on calling.strip()
. That's a much easier to follow, but seems to impact the whole cell instead of just the spark line.When I used
r'^\s*(%%|%).*'
to testnbdev_export --procs scrub_magics
, an unrelated notebook (with no magics!) was stripped of their trailing whitespace. Probably not a big deal, but I wanted to minimize the disruption here.