|
6 | 6 | import tempfile |
7 | 7 |
|
8 | 8 | nbExtension = ".ipynb" |
9 | | -pythonInterpName = sys.executable |
10 | 9 |
|
11 | 10 | rootKernelFileContent = ( |
12 | 11 | """{ |
|
21 | 20 | ] |
22 | 21 | } |
23 | 22 | """ |
24 | | - % pythonInterpName |
| 23 | + % sys.executable |
25 | 24 | ) |
26 | 25 |
|
27 | 26 |
|
@@ -148,24 +147,39 @@ def getKernelName(inNBName): |
148 | 147 |
|
149 | 148 |
|
150 | 149 | def canReproduceNotebook(inNBName, needsCompare): |
| 150 | + from nbconvert import NotebookExporter |
| 151 | + from nbconvert.preprocessors import ExecutePreprocessor |
| 152 | + import nbformat |
| 153 | + |
151 | 154 | tmpDir = addEtcToEnvironment(os.path.dirname(inNBName)) |
152 | 155 | outNBName = inNBName.replace(nbExtension, "_out" + nbExtension) |
153 | | - convCmd = ( |
154 | | - "%s -m jupyter nbconvert " |
155 | | - "--to notebook " |
156 | | - "--ExecutePreprocessor.kernel_name=%s " |
157 | | - "--ExecutePreprocessor.enabled=True " |
158 | | - "--ExecutePreprocessor.timeout=3600 " |
159 | | - "--ExecutePreprocessor.startup_timeout=180 " |
160 | | - "%s " |
161 | | - "--output %s" % (pythonInterpName, getKernelName(inNBName), inNBName, outNBName) |
| 156 | + |
| 157 | + # Load input notebook |
| 158 | + with open(inNBName, "r", encoding="utf-8") as f: |
| 159 | + nb = nbformat.read(f, as_version=4) |
| 160 | + |
| 161 | + # Configure execution |
| 162 | + ep = ExecutePreprocessor( |
| 163 | + kernel_name=getKernelName(inNBName), |
| 164 | + timeout=3600, |
| 165 | + startup_timeout=180, |
| 166 | + allow_errors=False, |
162 | 167 | ) |
163 | | - exitStatus = os.system(convCmd) # we use system to inherit the environment in os.environ |
164 | | - shutil.rmtree(tmpDir) |
| 168 | + |
| 169 | + # Run the notebook |
| 170 | + ep.preprocess(nb, {"metadata": {"path": os.path.dirname(inNBName)}}) |
| 171 | + |
| 172 | + # Export executed notebook |
| 173 | + with open(outNBName, "w", encoding="utf-8") as f: |
| 174 | + nbformat.write(nb, f) |
| 175 | + |
| 176 | + # Compare or return success |
165 | 177 | if needsCompare: |
166 | 178 | return compareNotebooks(inNBName, outNBName) |
167 | 179 | else: |
168 | | - return exitStatus |
| 180 | + return 0 # success |
| 181 | + |
| 182 | + shutil.rmtree(tmpDir) |
169 | 183 |
|
170 | 184 |
|
171 | 185 | def isInputNotebookFileName(filename): |
|
0 commit comments