Skip to content

Commit b2c687b

Browse files
committed
[roottest] Use nbconvert as a library in nbdiff.py
1 parent e2d16df commit b2c687b

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

roottest/python/JupyROOT/nbdiff.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import tempfile
77

88
nbExtension = ".ipynb"
9-
pythonInterpName = sys.executable
109

1110
rootKernelFileContent = (
1211
"""{
@@ -21,7 +20,7 @@
2120
]
2221
}
2322
"""
24-
% pythonInterpName
23+
% sys.executable
2524
)
2625

2726

@@ -148,24 +147,39 @@ def getKernelName(inNBName):
148147

149148

150149
def canReproduceNotebook(inNBName, needsCompare):
150+
from nbconvert import NotebookExporter
151+
from nbconvert.preprocessors import ExecutePreprocessor
152+
import nbformat
153+
151154
tmpDir = addEtcToEnvironment(os.path.dirname(inNBName))
152155
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,
162167
)
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
165177
if needsCompare:
166178
return compareNotebooks(inNBName, outNBName)
167179
else:
168-
return exitStatus
180+
return 0 # success
181+
182+
shutil.rmtree(tmpDir)
169183

170184

171185
def isInputNotebookFileName(filename):

0 commit comments

Comments
 (0)