|
18 | 18 | """
|
19 | 19 | from __future__ import print_function
|
20 | 20 |
|
21 |
| -import glob |
| 21 | +import io |
| 22 | +import glob # noqa |
22 | 23 | import os
|
23 | 24 | import shutil
|
24 | 25 | import sys
|
25 |
| -import sphinx |
| 26 | +from contextlib import contextmanager |
| 27 | + |
| 28 | +import sphinx # noqa |
26 | 29 | import argparse
|
27 |
| -import jinja2 |
| 30 | +import jinja2 # noqa |
28 | 31 |
|
29 | 32 | os.environ['PYTHONPATH'] = '..'
|
30 | 33 |
|
@@ -102,10 +105,90 @@ def clean():
|
102 | 105 | shutil.rmtree('source/generated')
|
103 | 106 |
|
104 | 107 |
|
| 108 | +@contextmanager |
| 109 | +def cleanup_nb(nb): |
| 110 | + try: |
| 111 | + yield |
| 112 | + finally: |
| 113 | + try: |
| 114 | + os.remove(nb + '.executed') |
| 115 | + except OSError: |
| 116 | + pass |
| 117 | + |
| 118 | + |
| 119 | +def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=''): |
| 120 | + """ |
| 121 | + Execute notebook in `src` and write the output to `dst` |
| 122 | +
|
| 123 | + Parameters |
| 124 | + ---------- |
| 125 | + src, dst: str |
| 126 | + path to notebook |
| 127 | + allow_errors: bool |
| 128 | + timeout: int |
| 129 | + kernel_name: str |
| 130 | + defualts to value set in notebook metadata |
| 131 | +
|
| 132 | + Returns |
| 133 | + ------- |
| 134 | + dst: str |
| 135 | + """ |
| 136 | + import nbformat |
| 137 | + from nbconvert.preprocessors import ExecutePreprocessor |
| 138 | + |
| 139 | + with io.open(src, encoding='utf-8') as f: |
| 140 | + nb = nbformat.read(f, as_version=4) |
| 141 | + |
| 142 | + ep = ExecutePreprocessor(allow_errors=allow_errors, |
| 143 | + timeout=timeout, |
| 144 | + kernel_name=kernel_name) |
| 145 | + ep.preprocess(nb, resources={}) |
| 146 | + |
| 147 | + with io.open(dst, 'wt', encoding='utf-8') as f: |
| 148 | + nbformat.write(nb, f) |
| 149 | + return dst |
| 150 | + |
| 151 | + |
| 152 | +def convert_nb(src, dst, to='html', template_file='basic'): |
| 153 | + """ |
| 154 | + Convert a notebook `src`. |
| 155 | +
|
| 156 | + Parameters |
| 157 | + ---------- |
| 158 | + src, dst: str |
| 159 | + filepaths |
| 160 | + to: {'rst', 'html'} |
| 161 | + format to export to |
| 162 | + template_file: str |
| 163 | + name of template file to use. Default 'basic' |
| 164 | + """ |
| 165 | + from nbconvert import HTMLExporter, RSTExporter |
| 166 | + |
| 167 | + dispatch = {'rst': RSTExporter, 'html': HTMLExporter} |
| 168 | + exporter = dispatch[to.lower()](template_file=template_file) |
| 169 | + |
| 170 | + (body, resources) = exporter.from_filename(src) |
| 171 | + with io.open(dst, 'wt', encoding='utf-8') as f: |
| 172 | + f.write(body) |
| 173 | + return dst |
| 174 | + |
| 175 | + |
105 | 176 | def html():
|
106 | 177 | check_build()
|
107 |
| - os.system('jupyter nbconvert --to=html --template=basic ' |
108 |
| - '--output=source/html-styling.html source/html-styling.ipynb') |
| 178 | + |
| 179 | + notebooks = [ |
| 180 | + 'source/html-styling.ipynb', |
| 181 | + ] |
| 182 | + |
| 183 | + for nb in notebooks: |
| 184 | + with cleanup_nb(nb): |
| 185 | + try: |
| 186 | + print("Converting %s" % nb) |
| 187 | + executed = execute_nb(nb, nb + '.executed', allow_errors=True) |
| 188 | + convert_nb(executed, nb.rstrip('.ipynb') + '.html') |
| 189 | + except ImportError: |
| 190 | + pass |
| 191 | + |
109 | 192 | if os.system('sphinx-build -P -b html -d build/doctrees '
|
110 | 193 | 'source build/html'):
|
111 | 194 | raise SystemExit("Building HTML failed.")
|
|
0 commit comments