Ejemplo n.º 1
0
def notebook_run(path):
    """Execute a notebook via nbconvert and collect output.
       :returns (parsed nb object, execution errors)
    """
    import nbformat
    from nbconvert import nbconvertapp

    # file can't be open multiple times on windows
    # https://docs.python.org/2/library/tempfile.html
    with tempfile.NamedTemporaryFile(suffix=".ipynb", delete=False) as fout:
        tmp_file_name = fout.name
    dirname, _ = os.path.split(path)

    nbconvertapp.main([
        "nbconvert",
        "--to",
        "notebook",
        "--execute",
        "--ExecutePreprocessor.timeout=60",
        "--output",
        tmp_file_name,
        path,
    ])

    nb = nbformat.read(tmp_file_name, nbformat.current_nbformat)

    if os.path.exists(tmp_file_name):
        os.remove(tmp_file_name)

    errors = [
        output for cell in nb.cells if "outputs" in cell
        for output in cell["outputs"] if output.output_type == "error"
    ]

    return nb, errors
Ejemplo n.º 2
0
def convert_ipynb(_):
    import nbconvert.nbconvertapp as nba
    cwd = os.getcwd()
    for fdir, dirs, fns in os.walk(cwd):
        if '.ipynb_checkpoints' in dirs:
            dirs.remove('.ipynb_checkpoints')
        for fn in fns:
            if fn.endswith('.ipynb'):
                fp = os.path.join(fdir, fn)
                args = ['--execute', '--template', './vertex.tpl', '--to', 'rst', fp]
                nba.main(args)
Ejemplo n.º 3
0
def convert_ipynb(_):
    import nbconvert.nbconvertapp as nba
    cwd = os.getcwd()
    for fdir, dirs, fns in os.walk(cwd):
        if '.ipynb_checkpoints' in dirs:
            dirs.remove('.ipynb_checkpoints')
        for fn in fns:
            if fn.endswith('.ipynb'):
                fp = os.path.join(fdir, fn)
                args = [
                    '--execute', '--template', './vertex.tpl', '--to', 'rst',
                    fp
                ]
                nba.main(args)
Ejemplo n.º 4
0
def convert_ipynb(_):
    import synapse.common as s_common
    import nbconvert.nbconvertapp as nba
    cwd = os.getcwd()
    for fdir, dirs, fns in os.walk(cwd):
        if '.ipynb_checkpoints' in dirs:
            dirs.remove('.ipynb_checkpoints')
        for fn in fns:
            if fn.endswith('.ipynb'):
                # if 'httpapi' not in fn:
                #     continue
                tick = s_common.now()
                fp = os.path.join(fdir, fn)
                args = ['--execute', '--template', './vertex.tpl', '--to', 'rst', fp]
                nba.main(args)
                tock = s_common.now()
                took = (tock - tick) / 1000
                print(f'convert_ipynb: Notebook {fn} execution took {took} seconds.')
Ejemplo n.º 5
0
# -*- coding: utf-8 -*-
import re
import sys

from nbconvert.nbconvertapp import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(main())
Ejemplo n.º 6
0
    def _convert(self, args):
        log.info("nbconvert")
        from nbconvert import nbconvertapp as app

        sys.argv = [""] + args
        app.main()
from nbconvert.nbconvertapp import main

if __name__ == '__main__':
    main()