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

Commit

Permalink
Fixes #294, rename command line into convert_notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Feb 20, 2020
1 parent 5332bad commit 483c793
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 38 deletions.
42 changes: 22 additions & 20 deletions _unittests/ut_cli/test_cli_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class TestProcessNotebook(ExtTestCase):

@skipif_travis("No latex installed.")
@skipif_appveyor("No latex installed.")
def test_process_notebook(self):
def test_convert_notebook(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

temp = get_temp_folder(__file__, "temp_process_notebook")
temp = get_temp_folder(__file__, "temp_convert_notebook")
source = os.path.join(temp, "..", "data", "td1a_unit_test_ci.ipynb")

fold = 'static'
Expand All @@ -39,12 +39,12 @@ def test_process_notebook(self):
if not os.path.exists('%s.tpl' % tpl):
shutil.copy(sr, '.')

with self.subTest(cmd="process_notebooks"):
with self.subTest(cmd="convert_notebook"):
st = BufferedPrint()
main(args=["process_notebooks", "-n", source, "-o",
temp, "-b", temp, '-f', 'rst,html'], fLOG=st.fprint)
main(args=["convert_notebook", "-f", source, "-o",
temp, "-b", temp, '-fo', 'rst,html'], fLOG=st.fprint)
res = str(st)
self.assertIn("convert into rst", res)
self.assertIn("convert into 'rst'", res)

with self.subTest(cmd="run_notebook"):
outname = os.path.join(temp, "out_nb.ipynb")
Expand All @@ -55,12 +55,13 @@ def test_process_notebook(self):
self.assertExists(outname)
source = outname

temp2 = get_temp_folder(__file__, "temp_process_notebook_next")
temp2 = get_temp_folder(__file__, "temp_convert_notebook_next")
st = BufferedPrint()
main(args=["process_notebooks", "-n", source, "-o",
temp2, "-b", temp2, '-f', 'rst,html'], fLOG=st.fprint)
main(args=["convert_notebook", "-f", source, "-o",
temp2, "-b", temp2, '-fo', 'rst,html'], fLOG=st.fprint)
res = str(st)
self.assertIn("convert into rst", res)
self.assertIn("convert into 'rst'", res)
self.assertExists(os.path.join(temp2, "out_nb2html.html"))

def test_run_notebook_help(self):
st = BufferedPrint()
Expand All @@ -70,22 +71,22 @@ def test_run_notebook_help(self):

@skipif_travis("No latex installed.")
@skipif_appveyor("No latex installed.")
def test_process_notebook2(self):
def test_convert_notebook2(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

temp = get_temp_folder(__file__, "temp_process_notebook2")
temp = get_temp_folder(__file__, "temp_convert_notebook2")
source = os.path.join(temp, "..", "data",
"onnx_tree_ensemble_parallel.ipynb")

with self.subTest(cmd="process_notebooks"):
with self.subTest(cmd="convert_notebook"):
st = BufferedPrint()
main(args=["process_notebooks", "-n", source, "-o",
temp, "-b", temp, '-f', 'rst,html'], fLOG=st.fprint)
main(args=["convert_notebook", "-f", source, "-o",
temp, "-b", temp, '-fo', 'rst,html'], fLOG=st.fprint)
res = str(st)
self.assertIn("convert into rst", res)
self.assertIn("convert into 'rst'", res)

with self.subTest(cmd="run_notebook"):
outname = os.path.join(temp, "out_nb.ipynb")
Expand All @@ -96,12 +97,13 @@ def test_process_notebook2(self):
self.assertExists(outname)
source = outname

temp2 = get_temp_folder(__file__, "temp_process_notebook2_next")
temp2 = get_temp_folder(__file__, "temp_convert_notebook2_next")
st = BufferedPrint()
main(args=["process_notebooks", "-n", source, "-o",
temp2, "-b", temp2, '-f', 'rst,html'], fLOG=st.fprint)
main(args=["convert_notebook", "-f", source, "-o",
temp2, "-b", temp2, '-fo', 'rst,html'], fLOG=st.fprint)
res = str(st)
self.assertIn("convert into rst", res)
self.assertIn("convert into 'rst'", res)
self.assertExists(os.path.join(temp2, "out_nb2html.html"))


if __name__ == "__main__":
Expand Down
8 changes: 3 additions & 5 deletions src/pyquickhelper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,34 @@ def main(args, fLOG=print):
from .pandashelper import df2rst
from .pycode import clean_files, run_test_function
from .cli import cli_main_helper
from .helpgen.process_notebooks import process_notebooks
from .filehelper import create_visual_diff_through_html_files
from .filehelper import explore_folder
from .cli.simplified_fct import sphinx_rst
from .imghelper.img_helper import zoom_img
from .imghelper.img_export import images2pdf
from .script_exec import repeat_script
from .ftp_cli import ftp_upload
from .cli.notebook import run_notebook
from .cli.notebook import run_notebook, convert_notebook
except ImportError: # pragma: no cover
from pyquickhelper.cli.pyq_sync_cli import pyq_sync
from pyquickhelper.cli.encryption_file_cli import encrypt_file, decrypt_file
from pyquickhelper.cli.encryption_cli import encrypt, decrypt
from pyquickhelper.pandashelper import df2rst
from pyquickhelper.pycode import clean_files, run_test_function
from pyquickhelper.cli import cli_main_helper
from pyquickhelper.helpgen.process_notebooks import process_notebooks
from pyquickhelper.filehelper import create_visual_diff_through_html_files
from pyquickhelper.filehelper import explore_folder
from pyquickhelper.cli.simplified_fct import sphinx_rst
from pyquickhelper.imghelper.img_helper import zoom_img
from pyquickhelper.imghelper.img_export import images2pdf
from pyquickhelper.cli.script_exec import repeat_script
from pyquickhelper.cli.ftp_cli import ftp_upload
from pyquickhelper.cli.notebook import run_notebook
from pyquickhelper.cli.notebook import run_notebook, convert_notebook

fcts = dict(synchronize_folder=pyq_sync, encrypt_file=encrypt_file,
decrypt_file=decrypt_file, encrypt=encrypt,
decrypt=decrypt, df2rst=df2rst, clean_files=clean_files,
process_notebooks=process_notebooks,
convert_notebook=convert_notebook,
visual_diff=create_visual_diff_through_html_files,
ls=explore_folder, run_test_function=run_test_function,
sphinx_rst=sphinx_rst, run_notebook=run_notebook,
Expand Down
51 changes: 51 additions & 0 deletions src/pyquickhelper/cli/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
@file
@brief Command line about noteboooks.
"""
import os
from ..ipythonhelper import run_notebook as _run_notebook
from ..helpgen.process_notebooks import process_notebooks


def run_notebook(filename, profile_dir='', working_dir='',
Expand Down Expand Up @@ -56,3 +58,52 @@ def run_notebook(filename, profile_dir='', working_dir='',
log_level=log_level, startup_timeout=int(
startup_timeout),
fLOG=fLOG, detailed_log=detailed_log)


def convert_notebook(filename, outfold=None, build=None,
latex_path=None, pandoc_path=None,
formats="html,python", exc=True, nblinks=None,
remove_unicode_latex=False,
fLOG=print):
"""
Converts a notebook into a specific format.
:param filename: notebook name
:param outfold: notebook is first copied into this directory
to make some preprocessing. This directory must exist,
directory ``_convertnb`` will be created otherwise.
:param build: can be the current one
:param latex_path: if format includes latex
:param pandoc_path: for word format
:param formats: list of formats to use (comma separated),
full list is ``ipynb,html,python,rst,slides,pdf,github``
:param exc: raises an exception of be silent
:param nblinks: to add some link
:param remove_unicode_latex: should not be necessary
.. cmdref::
:title: Convert a notebook into a different format
:cmd: -m pyquickhelper convert_notebook --help
The command line converts notebook into HTML, RST, PDF, slides...
It calls :epkg:`nbconvert` but adds some preprocessing before calling
it.
"""
if not os.path.exists(filename):
raise FileNotFoundError("Unable to find '{}'.".format(filename))
if outfold in ('.', '', None):
outfold = os.path.abspath(os.path.dirname(filename))
if not os.path.exists(outfold):
raise FileNotFoundError("Unable to find '{}'.".format(outfold))
if build in ('.', '', None):
build = os.path.join(outfold, "_convertnb")
if not os.path.exists(build):
os.mkdir(build)
if not os.path.exists(build):
raise FileNotFoundError("Unable to find '{}'.".format(build))
return process_notebooks(
notebooks=filename, outfold=outfold, build=build,
latex_path=latex_path, pandoc_path=pandoc_path,
formats=formats, exc=exc, nblinks=nblinks,
remove_unicode_latex=remove_unicode_latex,
fLOG=fLOG)
26 changes: 13 additions & 13 deletions src/pyquickhelper/helpgen/process_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ def process_notebooks(notebooks, outfold, build, latex_path=None, pandoc_path=No
from pyquickhelper.ipythonhelper import process_notebooks
process_notebooks("td1a_correction_session7.ipynb",
"dest_folder", "dest_folder",
formats=("ipynb", "html", "python", "rst", "slides", "pdf",
"docx", "github")])
"dest_folder", "dest_folder",
formats=("ipynb", "html", "python", "rst", "slides", "pdf",
"docx", "github")])
For latex and pdf, a custom processor was added to handle raw data
and add ``\\begin{verbatim}`` and ``\\end{verbatim}``.
Expand Down Expand Up @@ -196,7 +196,8 @@ def _process_notebooks_in_private(fnbcexe, list_args, options_args):
except SystemExit as e:
exc = e
except IndentationError as e:
# This is change in IPython 6.0.0. The conversion fails on IndentationError.
# This is change in IPython 6.0.0.
# The conversion fails on IndentationError.
# We switch to another one.
i = list_args.index("--template")
format = list_args[i + 1]
Expand All @@ -214,7 +215,7 @@ def _process_notebooks_in_private(fnbcexe, list_args, options_args):
else:
# We do nothing in this case.
exc = e
except AttributeError as e:
except (AttributeError, FileNotFoundError) as e:
exc = e
sys.stdout = memo_out
sys.stderr = memo_err
Expand Down Expand Up @@ -288,9 +289,7 @@ def _process_notebooks_in(notebooks, outfold, build, latex_path=None, pandoc_pat
a cell outputting *svg*
(see `PR 910 <https://github.com/jupyter/nbconvert/pull/910>`_).
.. versionchanged:: 1.7
Change default value of *remove_unicode_latex* to False.
Use `xelatex <https://doc.ubuntu-fr.org/xelatex>`_ if possible.
Use `xelatex <https://doc.ubuntu-fr.org/xelatex>`_ if possible.
"""
from nbconvert.nbconvertapp import main as nbconvert_main
if pandoc_path is None:
Expand Down Expand Up @@ -341,7 +340,7 @@ def _process_notebooks_in(notebooks, outfold, build, latex_path=None, pandoc_pat

# we copy available images (only notebook folder)
# in case they are used in latex
currentdir = os.path.dirname(notebook_in)
currentdir = os.path.abspath(os.path.dirname(notebook_in))
for curfile in os.listdir(currentdir):
ext = os.path.splitext(curfile)[1]
if ext in {'.png', '.jpg', '.bmp', '.gif', '.jpeg', '.svg', '.mp4'}:
Expand Down Expand Up @@ -456,8 +455,8 @@ def _process_notebooks_in(notebooks, outfold, build, latex_path=None, pandoc_pat
# output
templ = {'html': 'full', 'latex': 'article',
'elatex': 'article'}.get(format, format)
fLOG("[_process_notebooks_in] ### convert into ", format_, " NB: ", notebook,
" ### ", os.path.exists(outputfile), ":", outputfile)
fLOG("[_process_notebooks_in] ### convert into '{}' (done: {}): '{}' -> '{}'".format(
format_, os.path.exists(outputfile), notebook, outputfile))

list_args.extend(["--output", outputfile_noext_fixed])
if templ is not None and format != "slides":
Expand All @@ -479,8 +478,9 @@ def _process_notebooks_in(notebooks, outfold, build, latex_path=None, pandoc_pat
else:
list_args.extend(["--to", format,
notebook if nb_slide is None else nb_slide])
fLOG("[_process_notebooks_in] NBc*:", format, list_args)
fLOG("[_process_notebooks_in]", os.getcwd())
fLOG(
"[_process_notebooks_in] NBc* format='{}' args={}".format(format, list_args))
fLOG("[_process_notebooks_in] cwd='{}'".format(os.getcwd()))

c = " ".join(list_args)
out, err = _process_notebooks_in_private(
Expand Down

0 comments on commit 483c793

Please sign in to comment.