Coverage for pyquickhelper/cli/notebook.py: 71%
28 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
1"""
2@file
3@brief Command line about noteboooks.
4"""
5import os
8def run_notebook(filename, profile_dir='', working_dir='',
9 skip_exceptions=False,
10 outfilename='', additional_path='',
11 kernel_name="python", log_level=30,
12 startup_timeout=300,
13 verbose=0, raise_exception=True, fLOG=print):
14 """
15 Runs a notebook end to end,
16 it is inspired from module `runipy <https://github.com/paulgb/runipy/>`_.
18 :param filename: notebook filename
19 :param profile_dir: profile directory
20 :param working_dir: working directory
21 :param skip_exceptions: skip exceptions
22 :param outfilename: if not None, saves the output in this notebook
23 :param additional_path: additional paths for import (comma separated)
24 :param kernel_name: kernel name, it can be None
25 :param log_level: Choices: (0, 10, 20, 30=default, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL')
26 :param detailed_log: a second function to log more information when executing the notebook,
27 this should be a function with the same signature as ``print`` or None
28 :param startup_timeout: wait for this long for the kernel to be ready,
29 see `wait_for_ready
30 <https://github.com/jupyter/jupyter_client/blob/master/jupyter_client/blocking/client.py#L84>`_
31 :param verbose: 0 for standard logging, 1 for more
32 :param raise_exception: raise an exception if a cell raises one
33 :param fLOG: logging function
34 :return: tuple (statistics, output)
36 .. cmdref::
37 :title: Run a notebook
38 :cmd: -m pyquickhelper run_notebook --help
40 The command line runs a notebook and stores the modified
41 notebook.
42 """
43 from ..ipythonhelper import run_notebook as _run_notebook
44 detailed_log = fLOG if verbose else None
45 if profile_dir == '':
46 profile_dir = None
47 if working_dir == '':
48 working_dir = None
49 if outfilename == '':
50 outfilename = None
51 if additional_path in ('', None):
52 additional_path = None
53 else:
54 additional_path = additional_path.split(',')
55 log_level = int(log_level)
56 raise_exception = raise_exception in ('True', True, 1, '1', 'true')
57 return _run_notebook(
58 filename, profile_dir=profile_dir, working_dir=working_dir,
59 skip_exceptions=skip_exceptions, outfilename=outfilename,
60 additional_path=additional_path, kernel_name=kernel_name,
61 log_level=log_level, startup_timeout=int(startup_timeout),
62 fLOG=fLOG, detailed_log=detailed_log,
63 raise_exception=raise_exception)
66def convert_notebook(filename, outfold=None, build=None,
67 latex_path=None, pandoc_path=None,
68 formats="html,python", exc=True, nblinks=None,
69 remove_unicode_latex=False,
70 fLOG=print):
71 """
72 Converts a notebook into a specific format.
74 :param filename: notebook name
75 :param outfold: notebook is first copied into this directory
76 to make some preprocessing. This directory must exist,
77 directory ``_convertnb`` will be created otherwise.
78 :param build: can be the current one
79 :param latex_path: if format includes latex
80 :param pandoc_path: for word format
81 :param formats: list of formats to use (comma separated),
82 full list is ``ipynb,html,python,rst,slides,pdf,github``
83 :param exc: raises an exception of be silent
84 :param nblinks: to add some link
85 :param remove_unicode_latex: should not be necessary
87 .. cmdref::
88 :title: Convert a notebook into a different format
89 :cmd: -m pyquickhelper convert_notebook --help
91 The command line converts notebook into HTML, RST, PDF, slides...
92 It calls :epkg:`nbconvert` but adds some preprocessing before calling
93 it.
94 """
95 from ..helpgen.process_notebooks import process_notebooks
96 if not os.path.exists(filename):
97 raise FileNotFoundError( # pragma: no cover
98 f"Unable to find '{filename}'.")
99 if outfold in ('.', '', None):
100 outfold = os.path.abspath(os.path.dirname(filename))
101 if not os.path.exists(outfold):
102 raise FileNotFoundError( # pragma: no cover
103 f"Unable to find '{outfold}'.")
104 if build in ('.', '', None):
105 build = os.path.join(outfold, "_convertnb")
106 if not os.path.exists(build):
107 os.mkdir(build)
108 if not os.path.exists(build):
109 raise FileNotFoundError( # pragma: no cover
110 f"Unable to find '{build}'.")
111 return process_notebooks(
112 notebooks=filename, outfold=outfold, build=build,
113 latex_path=latex_path, pandoc_path=pandoc_path,
114 formats=formats, exc=exc, nblinks=nblinks,
115 remove_unicode_latex=remove_unicode_latex,
116 fLOG=fLOG)