wvpy.render_workbook

 1# run with:
 2#    python -m wvpy.render_workbook test.py
 3#    python -m wvpy.render_workbook test.ipynb
 4
 5from typing import Iterable
 6import argparse
 7import os
 8import sys
 9import traceback
10from wvpy.jtools import render_as_html
11
12
13def render_workbook(
14    infiles: Iterable[str],
15    *,
16    quiet: bool = False,
17    strip_input: bool = True,
18) -> int:
19    """
20    Render a list of Jupyter notebooks.
21
22    :param infiles: list of file names to process
23    :param quiet: if true do the work quietly
24    :param strip_input: if true strip input cells and cell numbering
25    :return: 0 if successful 
26    """
27    # checks
28    assert isinstance(quiet, bool)
29    assert isinstance(strip_input, bool)
30    assert len(infiles) > 0
31    assert len(set(infiles)) == len(infiles)
32    assert not isinstance(infiles, str)  # common error
33    infiles = list(infiles)
34    tasks = []
35    for input_file_name in infiles:
36        assert isinstance(input_file_name, str)
37        assert len(input_file_name) > 0
38        assert not input_file_name.endswith('.html')
39        assert not input_file_name.endswith('.pdf')
40        if not (input_file_name.endswith('.py') or input_file_name.endswith('.ipynb')):
41            py_exists = os.path.exists(input_file_name + '.py')
42            ipynb_exists = os.path.exists(input_file_name + '.ipynb')
43            if py_exists == ipynb_exists:
44                raise ValueError("if no suffix is specified, then exactly one of the .py or ipynb file forms must be present")
45            if py_exists:
46                input_file_name = input_file_name + '.py'
47            else:
48                input_file_name = input_file_name + '.ipynb'
49        assert input_file_name.endswith('.py') or input_file_name.endswith('.ipynb')
50        assert os.path.exists(input_file_name)
51        tasks.append(input_file_name)
52    # do the work
53    for input_file_name in tasks:
54        render_as_html(
55            input_file_name, 
56            exclude_input=strip_input, 
57            verbose=quiet == False)
58    return 0
59
60
61if __name__ == '__main__':
62    try:
63        parser = argparse.ArgumentParser(description="Render .py or .ipynb to .html by executing in Jupyter")
64        parser.add_argument(
65            'infile', 
66            metavar='infile', 
67            type=str, 
68            nargs='+',
69            help='name of input file(s)')
70        parser.add_argument('--strip_input', action='store_true', help="strip input cells and cell markers")
71        parser.add_argument('--quiet', action='store_true', help='quiet operation')
72        args = parser.parse_args()
73        # checks
74        assert isinstance(args.quiet, bool)
75        assert isinstance(args.strip_input, bool)
76        assert len(args.infile) > 0
77        assert len(set(args.infile)) == len(args.infile)
78        ret = render_workbook(
79            quiet=quiet,
80            strip_input=strip_input,
81            infiles=args.infile,
82        )
83        sys.exit(ret)
84    except AssertionError:
85        _, _, tb = sys.exc_info()
86        tb_info = traceback.extract_tb(tb)
87        filename, line, func, text = tb_info[-1]
88        print(f'Assertion failed {filename}:{line} (caller {func}) in statement {text}')
89    except Exception as ex:
90        print(ex)
91    sys.exit(-1)
def render_workbook( infiles: Iterable[str], *, quiet: bool = False, strip_input: bool = True) -> int:
15def render_workbook(
16    infiles: Iterable[str],
17    *,
18    quiet: bool = False,
19    strip_input: bool = True,
20) -> int:
21    """
22    Render a list of Jupyter notebooks.
23
24    :param infiles: list of file names to process
25    :param quiet: if true do the work quietly
26    :param strip_input: if true strip input cells and cell numbering
27    :return: 0 if successful 
28    """
29    # checks
30    assert isinstance(quiet, bool)
31    assert isinstance(strip_input, bool)
32    assert len(infiles) > 0
33    assert len(set(infiles)) == len(infiles)
34    assert not isinstance(infiles, str)  # common error
35    infiles = list(infiles)
36    tasks = []
37    for input_file_name in infiles:
38        assert isinstance(input_file_name, str)
39        assert len(input_file_name) > 0
40        assert not input_file_name.endswith('.html')
41        assert not input_file_name.endswith('.pdf')
42        if not (input_file_name.endswith('.py') or input_file_name.endswith('.ipynb')):
43            py_exists = os.path.exists(input_file_name + '.py')
44            ipynb_exists = os.path.exists(input_file_name + '.ipynb')
45            if py_exists == ipynb_exists:
46                raise ValueError("if no suffix is specified, then exactly one of the .py or ipynb file forms must be present")
47            if py_exists:
48                input_file_name = input_file_name + '.py'
49            else:
50                input_file_name = input_file_name + '.ipynb'
51        assert input_file_name.endswith('.py') or input_file_name.endswith('.ipynb')
52        assert os.path.exists(input_file_name)
53        tasks.append(input_file_name)
54    # do the work
55    for input_file_name in tasks:
56        render_as_html(
57            input_file_name, 
58            exclude_input=strip_input, 
59            verbose=quiet == False)
60    return 0

Render a list of Jupyter notebooks.

Parameters
  • infiles: list of file names to process
  • quiet: if true do the work quietly
  • strip_input: if true strip input cells and cell numbering
Returns

0 if successful