Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
update unit test to convert notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Nov 20, 2021
1 parent 48bd3a6 commit 26a044d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 12 deletions.
73 changes: 67 additions & 6 deletions _unittests/ut_helpgen/test_notebooks_bug.py
Expand Up @@ -2,11 +2,9 @@
@brief test log(time=65s)
@author Xavier Dupre
"""
import sys
import os
import unittest
import re

from pyquickhelper.loghelper import fLOG
from pyquickhelper.helpgen.sphinx_main import process_notebooks
from pyquickhelper.pycode import is_travis_or_appveyor, get_temp_folder, ExtTestCase
Expand Down Expand Up @@ -49,10 +47,73 @@ def test_notebook(self):
fold = os.path.normpath(os.path.join(path, "notebooks"))
nbs = [os.path.join(fold, _)
for _ in os.listdir(fold) if ".ipynb" in _]
formats = ["slides", "ipynb", "html",
"python", "rst", "pdf"]
if sys.platform.startswith("win"):
formats.append("docx")
formats = ["ipynb", "python", "rst", "pdf"]

temp = get_temp_folder(__file__, "temp_nb_bug")

if is_travis_or_appveyor() in ('travis', 'appveyor'):
return

res = process_notebooks(nbs, temp, temp, formats=formats)
fLOG("*****", len(res))
for _ in res:
if not os.path.exists(_[0]):
raise Exception(_[0])

check = os.path.join(temp, "td1a_correction_session4.tex")
with open(check, "r", encoding="utf8") as f:
content = f.read()
if "\\section{" not in content:
raise Exception(content)
checks = [os.path.join(temp, "reveal.js"),
os.path.join(temp, "require.js")]
for check in checks:
if not os.path.exists(check):
raise Exception(check)

def test_notebook_html(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
path = os.path.abspath(os.path.split(__file__)[0])
fold = os.path.normpath(os.path.join(path, "notebooks"))
nbs = [os.path.join(fold, _)
for _ in os.listdir(fold) if ".ipynb" in _]
formats = ["html"]

temp = get_temp_folder(__file__, "temp_nb_bug")

if is_travis_or_appveyor() in ('travis', 'appveyor'):
return

res = process_notebooks(nbs, temp, temp, formats=formats)
fLOG("*****", len(res))
for _ in res:
if not os.path.exists(_[0]):
raise Exception(_[0])

check = os.path.join(temp, "td1a_correction_session4.tex")
with open(check, "r", encoding="utf8") as f:
content = f.read()
if "\\section{" not in content:
raise Exception(content)
checks = [os.path.join(temp, "reveal.js"),
os.path.join(temp, "require.js")]
for check in checks:
if not os.path.exists(check):
raise Exception(check)

def test_notebook_slides(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
path = os.path.abspath(os.path.split(__file__)[0])
fold = os.path.normpath(os.path.join(path, "notebooks"))
nbs = [os.path.join(fold, _)
for _ in os.listdir(fold) if ".ipynb" in _]
formats = ["slides"]

temp = get_temp_folder(__file__, "temp_nb_bug")

Expand Down
2 changes: 1 addition & 1 deletion src/pyquickhelper/helpgen/process_notebooks.py
Expand Up @@ -569,7 +569,7 @@ def _process_notebooks_in(notebooks, outfold, build, latex_path=None, pandoc_pat
tex, custom_latex_processing=None, nblinks=nblinks,
remove_unicode=remove_unicode_latex, fLOG=fLOG)
except FileNotFoundError as e: # pragma: no cover
mes = ("[_process_notebooks_in-ERROR] Unable to to convert into latex"
mes = ("[_process_notebooks_in-ERROR] Unable to convert into latex"
"notebook %r due to %r.") % (tex, e)
warnings.warn(mes, RuntimeWarning)
fLOG(mes)
Expand Down
6 changes: 4 additions & 2 deletions src/pyquickhelper/helpgen/process_notebooks_cmd.py
Expand Up @@ -4,6 +4,7 @@
"""
import sys
import warnings
import traceback

try:
from nbconvert.nbconvertapp import main as nbconvert_main
Expand All @@ -16,8 +17,9 @@ def run_nbconvert(argv):
nbconvert_main(argv=argv)
except Exception as ee:
warnings.warn(
"[run_nbconvert-ERROR] Unable to to convert a notebook with "
"args=%r due to %r." % (argv, ee), RuntimeWarning)
"[run_nbconvert-ERROR] Unable to convert a notebook with "
"args=%r due to %r\n--CALL-STACK--\n%s." % (
argv, ee, traceback.format_exc()), RuntimeWarning)


def main():
Expand Down
15 changes: 12 additions & 3 deletions src/pyquickhelper/ipythonhelper/notebook_runner.py
Expand Up @@ -745,14 +745,22 @@ def cell_image(self, cell, image_from_text=False):
if image_from_text:
b = self.create_picture_from(v, "latex")
results.append(b)
elif k in "application/vnd.jupyter.widget-view+json":
elif k == "application/vnd.jupyter.widget-view+json":
# see http://ipywidgets.readthedocs.io/en/latest/embedding.html
if "model_id" not in v:
raise KeyError( # pragma: no cover
"model_id is missing from {0}".format(v))
model_id = v["model_id"]
self.fLOG(
"[application/vnd.jupyter.widget-view+json] not rendered", model_id)
elif k == "application/vnd.jupyter.widget-state+json":
# see http://ipywidgets.readthedocs.io/en/latest/embedding.html
if "model_id" not in v:
raise KeyError( # pragma: no cover
"model_id is missing from {0}".format(v))
model_id = v["model_id"]
self.fLOG(
"[application/vnd.jupyter.widget-state+json] not rendered", model_id)
elif k in {"image/png", "image/jpg", "image/jpeg", "image/gif"}:
if not isinstance(v, bytes):
v = base64.b64decode(v)
Expand All @@ -763,8 +771,7 @@ def cell_image(self, cell, image_from_text=False):
elif k in ("text/vnd.plotly.v1+html",
"application/vnd.plotly.v1+json",
"application/vnd.bokehjs_exec.v0+json",
"application/vnd.bokehjs_load.v0+json",
"application/vnd.jupyter.widget-state+json"):
"application/vnd.bokehjs_load.v0+json"):
results.append((v, k.split("/")[-1]))
else:
raise NotImplementedError( # pragma: no cover
Expand Down Expand Up @@ -843,6 +850,8 @@ def cell_height(self, cell):
nbl += len(v) // 50
elif k == "application/vnd.jupyter.widget-view+json":
nbl += 5
elif k == "application/vnd.jupyter.widget-state+json":
nbl += 5
elif k in ("text/vnd.plotly.v1+html",
"application/vnd.plotly.v1+json",
"application/vnd.bokehjs_load.v0+json",
Expand Down

0 comments on commit 26a044d

Please sign in to comment.