generated from fastai/nbdev-template
/
strip.py
38 lines (35 loc) · 1.18 KB
/
strip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/04_strip.ipynb.
# %% auto 0
__all__ = ['strip_nbs']
# %% ../nbs/04_strip.ipynb 2
from nbdev.config import get_config
from nbdev.processors import read_nb
from nbdev.export import nb_export
from nbdev.doclinks import nbglob
from nbdev.sync import write_nb
from fastcore.basics import AttrDict
from fastcore.script import call_parse
from fastcore.xtras import Path
# %% ../nbs/04_strip.ipynb 5
@call_parse
def strip_nbs(
notebook_path:str,
output_folder:str
):
"Removes all non-code cells from `notebook_path` and saves it in `output_folder`"
if not Path(output_folder).exists():
Path(output_folder).mkdir(parents=True, exist_ok=True)
for path in Path(notebook_path).ls(file_exts=".ipynb"):
nb = read_nb(path)
cells = []
for cell in nb["cells"]:
if cell["cell_type"] == "code":
cell.source = ''.join([line.split(" #")[0] for line in cell.source])
cells.append(cell)
new_nb = AttrDict(
cells=cells,
metadata={},
nbformat=4,
nbformat_minor=5
)
write_nb(new_nb, Path(output_folder)/path.name)