Skip to content

Commit

Permalink
attempt notebook test change to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrakenhoff committed Sep 27, 2024
1 parent 31e3ca2 commit f1bdd70
Showing 1 changed file with 75 additions and 45 deletions.
120 changes: 75 additions & 45 deletions tests/test_notebooks.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# %%
import os
import shutil
import subprocess
import tempfile
# import tempfile

import nbformat
import pytest
from nbconvert.preprocessors import ExecutePreprocessor

# import subprocess

# %%
nbdirs = [
# os.path.join("docs/00tutorials"),
os.path.join("docs/03examples"),
# os.path.join("notebooks"),
]


testdir = tempfile.mkdtemp()
# testdir = tempfile.mkdtemp()


def get_notebooks():
Expand All @@ -24,49 +28,75 @@ def get_notebooks():
return nblist


def get_jupyter_kernel():
try:
jklcmd = ("jupyter", "kernelspec", "list")
b = subprocess.Popen(
jklcmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
).communicate()[0]
if isinstance(b, bytes):
b = b.decode("utf-8")
print(b)
for line in b.splitlines():
if "python" in line:
kernel = line.split()[0]
except: # noqa
kernel = None
# def get_jupyter_kernel():
# try:
# jklcmd = ("jupyter", "kernelspec", "list")
# b = subprocess.Popen(
# jklcmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
# ).communicate()[0]
# if isinstance(b, bytes):
# b = b.decode("utf-8")
# print(b)
# for line in b.splitlines():
# if "python" in line:
# kernel = line.split()[0]
# except: # noqa
# kernel = None

# return kernel


# @pytest.mark.notebooks
# @pytest.mark.parametrize("pth", get_notebooks())
# def test_notebook(pth):
# kernel = get_jupyter_kernel()
# print("available jupyter kernel {}".format(kernel))

# fn = os.path.basename(pth)

# cmd = (
# "jupyter "
# + "nbconvert "
# + "--ExecutePreprocessor.timeout=600 "
# + "--to "
# + "notebook "
# + "--execute "
# + "{} ".format(pth)
# + "--output-dir "
# + "{} ".format(testdir)
# + "--output "
# + "{}".format(fn)
# )
# ival = os.system(cmd)
# assert ival == 0, "could not run {}".format(fn)


return kernel
# @pytest.mark.notebooks
# @pytest.mark.parametrize("pth", get_notebooks())
# def test_notebook_cmd(pth):
# cmd = (
# "jupyter "
# + "nbconvert "
# + "--ExecutePreprocessor.timeout=600 "
# + "--ExecutePreprocessor.kernel_name=python3 "
# + "--to "
# + "notebook "
# + "--execute "
# + "{} ".format(pth)
# )
# ival = os.system(cmd)
# assert ival == 0, f"could not run {os.path.basename(pth)}"


@pytest.mark.notebooks
@pytest.mark.parametrize("pth", get_notebooks())
def test_notebook(pth):
kernel = get_jupyter_kernel()
print("available jupyter kernel {}".format(kernel))

fn = os.path.basename(pth)

cmd = (
"jupyter "
+ "nbconvert "
+ "--ExecutePreprocessor.timeout=600 "
+ "--to "
+ "notebook "
+ "--execute "
+ "{} ".format(pth)
+ "--output-dir "
+ "{} ".format(testdir)
+ "--output "
+ "{}".format(fn)
)
ival = os.system(cmd)
assert ival == 0, "could not run {}".format(fn)


if __name__ == "__main__":
test_notebook()
shutil.rmtree(testdir)
def test_notebook_py(pth):
with open(pth, "r", encoding="utf-8") as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name="python3")
try:
assert (
ep.preprocess(nb, {"metadata": {"path": "docs/03examples"}}) is not None
), f"Got empty notebook for {os.path.basename(pth)}"
except Exception as e:
pytest.fail(reason=f"Failed executing {os.path.basename(pth)}: {e}")

0 comments on commit f1bdd70

Please sign in to comment.