Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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, 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 fLOG: logging function
33 :return: tuple (statistics, output)
35 .. cmdref::
36 :title: Run a notebook
37 :cmd: -m pyquickhelper run_notebook --help
39 The command line runs a notebook and stores the modified
40 notebook.
41 """
42 from ..ipythonhelper import run_notebook as _run_notebook
43 detailed_log = fLOG if verbose else None
44 if profile_dir == '':
45 profile_dir = None
46 if working_dir == '':
47 working_dir = None
48 if outfilename == '':
49 outfilename = None
50 if additional_path in ('', None):
51 additional_path = None
52 else:
53 additional_path = additional_path.split(',')
54 return _run_notebook(filename, profile_dir=profile_dir, working_dir=working_dir,
55 skip_exceptions=skip_exceptions, outfilename=outfilename,
56 additional_path=additional_path, kernel_name=kernel_name,
57 log_level=log_level, startup_timeout=int(
58 startup_timeout),
59 fLOG=fLOG, detailed_log=detailed_log)
62def convert_notebook(filename, outfold=None, build=None,
63 latex_path=None, pandoc_path=None,
64 formats="html,python", exc=True, nblinks=None,
65 remove_unicode_latex=False,
66 fLOG=print):
67 """
68 Converts a notebook into a specific format.
70 :param filename: notebook name
71 :param outfold: notebook is first copied into this directory
72 to make some preprocessing. This directory must exist,
73 directory ``_convertnb`` will be created otherwise.
74 :param build: can be the current one
75 :param latex_path: if format includes latex
76 :param pandoc_path: for word format
77 :param formats: list of formats to use (comma separated),
78 full list is ``ipynb,html,python,rst,slides,pdf,github``
79 :param exc: raises an exception of be silent
80 :param nblinks: to add some link
81 :param remove_unicode_latex: should not be necessary
83 .. cmdref::
84 :title: Convert a notebook into a different format
85 :cmd: -m pyquickhelper convert_notebook --help
87 The command line converts notebook into HTML, RST, PDF, slides...
88 It calls :epkg:`nbconvert` but adds some preprocessing before calling
89 it.
90 """
91 from ..helpgen.process_notebooks import process_notebooks
92 if not os.path.exists(filename):
93 raise FileNotFoundError( # pragma: no cover
94 "Unable to find '{}'.".format(filename))
95 if outfold in ('.', '', None):
96 outfold = os.path.abspath(os.path.dirname(filename))
97 if not os.path.exists(outfold):
98 raise FileNotFoundError( # pragma: no cover
99 "Unable to find '{}'.".format(outfold))
100 if build in ('.', '', None):
101 build = os.path.join(outfold, "_convertnb")
102 if not os.path.exists(build):
103 os.mkdir(build)
104 if not os.path.exists(build):
105 raise FileNotFoundError( # pragma: no cover
106 "Unable to find '{}'.".format(build))
107 return process_notebooks(
108 notebooks=filename, outfold=outfold, build=build,
109 latex_path=latex_path, pandoc_path=pandoc_path,
110 formats=formats, exc=exc, nblinks=nblinks,
111 remove_unicode_latex=remove_unicode_latex,
112 fLOG=fLOG)