-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtasks.py
104 lines (84 loc) · 2.65 KB
/
tasks.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import glob
import re
import sys
from invoke import task
def run(ctx, *args, **kwargs):
if 'pty' not in kwargs:
kwargs['pty'] = True
if 'echo' not in kwargs:
kwargs['echo'] = True
return ctx.run(*args, **kwargs)
@task
def execute_notebooks(ctx):
"""Execute notebooks"""
root = os.path.abspath(os.getcwd())
print("Executing notebooks...")
for filename in sorted(glob.glob('*.ipynb')):
if 'demo' in filename.lower():
continue
run(ctx, ' '.join([
sys.executable, '-m', 'jupyter', 'nbconvert',
'--inplace',
'--execute',
'--ExecutePreprocessor.allow_errors=True',
filename
]))
@task
def convert_notebooks(ctx):
"""Convert notebooks to rst and html"""
root = os.path.abspath(os.getcwd())
print("Converting notebooks...")
for filename in sorted(glob.glob('*.ipynb')):
if 'demo' in filename.lower():
continue
run(ctx, ' '.join([
sys.executable, '-m', 'jupyter', 'nbconvert',
'--to', 'rst',
'--FilesWriter.build_directory=sphinx/source',
filename
]))
# hack to convert links to ipynb files to html
for filename in sorted(glob.glob('sphinx/source/*.rst')):
with open(filename, 'r') as fh:
source = fh.read()
source = re.sub(r"<([^><]*)\.ipynb>", r"<\1.html>", source)
with open(filename, 'w') as fh:
fh.write(source)
@task
def clean_sphinx(ctx):
print("Removing .rst files in 'sphinx'...")
run(ctx, 'git clean -fdx sphinx')
@task
def clear_cells_output(ctx, what='all'):
"""Clear output from notebook cells"""
root = os.path.abspath(os.getcwd())
print("Clearing output from notebook cells...")
if what == 'all':
for filename in sorted(glob.glob('*.ipynb')):
run(ctx, ' '.join([
sys.executable, '-m', 'jupyter', 'nbconvert',
'--inplace',
'--to', 'ipynb',
'--ClearOutputPreprocessor.enabled=True',
filename
]))
else:
run(ctx, ' '.join([
sys.executable, '-m', 'jupyter', 'nbconvert',
'--inplace',
'--to', 'ipynb',
'--ClearOutputPreprocessor.enabled=True',
what
]))
@task
def check_notebooks(ctx, execute=False):
print("Running setup...")
clean_sphinx(ctx)
clear_cells_output(ctx)
if execute:
execute_notebooks(ctx)
convert_notebooks(ctx)
print("Checking notebooks...")
run(ctx, 'make -C sphinx spelling')
run(ctx, 'make -C sphinx linkcheck')