Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastai
GitHub Repository: fastai/course22
Path: blob/master/tools/clean.py
807 views
1
#!/usr/bin/env python
2
3
import nbformat
4
from nbdev.export import *
5
from nbdev.clean import *
6
from fastcore.all import *
7
from execnb.nbio import *
8
9
_re_header = re.compile(r'^#+\s+\S+')
10
_re_clean = re.compile(r'^\s*#\s*clean\s*')
11
12
def is_header_cell(cell): return _re_header.search(cell['source']) is not None
13
def is_clean_cell(cell): return _re_clean.search(cell['source']) is not None
14
15
def clean_tags(cell):
16
if is_header_cell(cell): return cell
17
for attr in ["id", "caption", "alt", "width", "hide_input", "hide_output", "clean"]:
18
cell["source"] = re.sub(r'#\s*' + attr + r'.*?($|\n)', '', cell["source"])
19
return cell
20
21
def proc_nb(fname, dest):
22
nb = read_nb(fname)
23
nb['cells'] = [clean_tags(c) for j,c in enumerate(nb['cells']) if
24
c['cell_type']=='code' or is_header_cell(c) or is_clean_cell(c)]
25
clean_nb(nb, clear_all=True)
26
with open(dest/fname.name, 'w') as f: nbformat.write(nb, f, version=4)
27
28
@call_parse
29
def proc_all(
30
path:str='.', # Path for source NBs
31
dest_path:str='clean'): # Path for dest NBs
32
path,dest_path = Path(path),Path(dest_path)
33
fns = [f for f in path.iterdir() if f.suffix == '.ipynb' and not f.name.startswith('_')]
34
for fn in fns: proc_nb(fn, dest=dest_path)
35
36
37