Esempio n. 1
0
File: main.py Progetto: hoh/runipy
def main():
    log_format = "%(asctime)s %(message)s"
    log_datefmt = "%m/%d/%Y %I:%M:%S %p"

    parser = argparse.ArgumentParser()
    parser.add_argument("input_file", help=".ipynb file to run")
    parser.add_argument("output_file", nargs="?", help=".ipynb file to save cell output to")
    parser.add_argument("--quiet", "-q", action="store_true", help="don't print anything unless things go wrong")
    parser.add_argument(
        "--overwrite", "-o", action="store_true", help="write notebook output back to original notebook"
    )
    parser.add_argument("--html", nargs="?", default=False, help="output an HTML snapshot of the notebook")
    parser.add_argument("--pylab", action="store_true", help="start notebook with pylab enabled")
    parser.add_argument(
        "--skip-exceptions",
        "-s",
        action="store_true",
        help="if an exception occurs in a cell, continue running the subsequent cells",
    )
    args = parser.parse_args()

    if args.overwrite:
        if args.output_file is not None:
            print("Error: output_filename must not be provided if " "--overwrite (-o) given", file=stderr)
            exit(1)
        else:
            args.output_file = args.input_file

    if not args.quiet:
        logging.basicConfig(level=logging.DEBUG, format=log_format, datefmt=log_datefmt)

    nb_runner = NotebookRunner(args.input_file, args.pylab)

    exit_status = 0
    try:
        nb_runner.run_notebook(skip_exceptions=args.skip_exceptions)
    except NotebookError:
        exit_status = 1

    if args.output_file:
        nb_runner.save_notebook(args.output_file)

    if args.html is not False:
        if args.html is None:
            # if --html is given but no filename is provided,
            # come up with a sane output name based on the
            # input filename
            if args.input_file.endswith(".ipynb"):
                args.html = args.input_file[:-6] + ".html"
            else:
                args.html = args.input_file + ".ipynb"

        logging.info("Saving HTML snapshot to %s" % args.html)
        exporter = HTMLExporter()
        output, resources = exporter.from_notebook_node(nb_runner.nb)
        codecs.open(args.html, "w", encoding="utf-8").write(output)

    if exit_status != 0:
        logging.warning("Exiting with nonzero exit status")
    exit(exit_status)
Esempio n. 2
0
def evaluate_notebook(nb_path, dest_path=None):
    # Create evaluated version and save it to the dest path.
    nb_runner = NotebookRunner(nb_in=nb_path)
    nb_runner.run_notebook()
    if dest_path is None:
        dest_path = 'temp_evaluated.ipynb'
    nb_runner.save_notebook(dest_path)
    ret = nb_to_html(dest_path)
    if dest_path is 'temp_evaluated.ipynb':
        os.remove(dest_path)
    return ret
def evaluate_notebook(nb_path, dest_path=None):
    # Create evaluated version and save it to the dest path.
    nb_runner = NotebookRunner(nb_in=nb_path)
    nb_runner.run_notebook()
    if dest_path is None:
        dest_path = 'temp_evaluated.ipynb'
    nb_runner.save_notebook(dest_path)
    ret = nb_to_html(dest_path)
    if dest_path is 'temp_evaluated.ipynb':
        os.remove(dest_path)
    return ret
Esempio n. 4
0
def evaluate_notebook(nb_path, dest_path=None, skip_exceptions=False):
    # Create evaluated version and save it to the dest path.
    # Always use --pylab so figures appear inline
    # perhaps this is questionable?
    nb_runner = NotebookRunner(nb_path, pylab=True)
    nb_runner.run_notebook(skip_exceptions=skip_exceptions)
    if dest_path is None:
        dest_path = 'temp_evaluated.ipynb'
    nb_runner.save_notebook(dest_path)
    ret = nb_to_html(dest_path)
    if dest_path is 'temp_evaluated.ipynb':
        os.remove(dest_path)
    return ret
def evaluate_notebook(nb_path, dest_path=None, skip_exceptions=False):
    # Create evaluated version and save it to the dest path.
    # Always use --pylab so figures appear inline
    # perhaps this is questionable?
    nb_runner = NotebookRunner(nb_in=nb_path, pylab=True)
    nb_runner.run_notebook(skip_exceptions=skip_exceptions)
    if dest_path is None:
        dest_path = 'temp_evaluated.ipynb'
    nb_runner.save_notebook(dest_path)
    ret = nb_to_html(dest_path)
    if dest_path is 'temp_evaluated.ipynb':
        os.remove(dest_path)
    return ret
Esempio n. 6
0
    def run(self):

        # Now convert the lecture notes, problem sets, and practice problems to
        # HTML notebooks.

        from runipy.notebook_runner import NotebookRunner

        start_dir = os.path.abspath('.')

        for notebook in (glob.glob('notebooks/*.ipynb')):
            os.chdir(os.path.dirname(notebook))
            r = NotebookRunner(os.path.basename(notebook), pylab=True)
            r.run_notebook(skip_exceptions=True)
            r.save_notebook(os.path.basename(notebook))
            os.chdir(start_dir)
Esempio n. 7
0
    def run(self):

        # Now convert the lecture notes, problem sets, and practice problems to
        # HTML notebooks.

        from runipy.notebook_runner import NotebookRunner

        start_dir = os.path.abspath('.')

        for notebook in glob.glob('*.ipynb'):
            if os.path.dirname(notebook):
                os.chdir(os.path.dirname(notebook))
            r = NotebookRunner("./" + os.path.basename(notebook))
            r.run_notebook(skip_exceptions=True)
            r.save_notebook("./" + os.path.basename(notebook))
            os.chdir(start_dir)
Esempio n. 8
0
    def run(self):
        """ Run the tutorial notebooks so the line numbers make sense. """
        from runipy.notebook_runner import NotebookRunner

        current_directory = os.getcwd()

        # walk through each directory in tutorials/ to find all .ipynb file
        notebook_files = []
        for root, dirs, files in os.walk(current_directory):
            for filename in files:
                base,ext = os.path.splitext(filename)
                if ext.lower() == ".ipynb" and "checkpoint" not in base:
                    os.chdir(root)
                    r = NotebookRunner(filename, pylab=True)
                    r.run_notebook(skip_exceptions=True)
                    r.save_notebook(filename)
Esempio n. 9
0
    def run(self):

        # Now convert the lecture notes, problem sets, and practice problems to
        # HTML notebooks.

        from runipy.notebook_runner import NotebookRunner

        start_dir = os.path.abspath('.')

        for notebook in (glob.glob('lectures/*.ipynb')
                        + glob.glob('problems/*.ipynb')
                        + glob.glob('practice/*.ipynb')):
            if "Understanding" in notebook:
                continue
            os.chdir(os.path.dirname(notebook))
            r = NotebookRunner(os.path.basename(notebook), pylab=True)
            r.run_notebook(skip_exceptions=True)
            r.save_notebook(os.path.basename(notebook))
            os.chdir(start_dir)
Esempio n. 10
0
def go(source_dir):
    print os.listdir(source_dir)
    if not os.path.exists(source_dir):
        raise ValueError('source_dir doesnt exist')

    print '----- starting NB Evaluation and Conversion'
    logging.basicConfig(level=logging.DEBUG,
                        format=log_format,
                        datefmt=log_datefmt)
    nb_files = recursive_find_by_filter(source_dir, '*.ipynb')
    print 'source dir is  %s' % source_dir
    for nb_file in nb_files:
        print nb_file
        basename = os.path.basename(nb_file)
        notebook_name = basename[:basename.rfind('.')]
        build_directory = os.path.dirname(nb_file)

        r = NotebookRunner(nb_file, pylab=True)
        r.run_notebook()
        r.save_notebook(nb_file)

        exporter = RSTExporter()
        writer = FilesWriter()

        resources = {}
        resources['output_files_dir'] = '%s_files' % notebook_name

        output, resources = exporter.from_notebook_node(r.nb,
                                                        resources=resources)

        writer.build_directory = build_directory
        writer.write(output, resources, notebook_name=notebook_name)
        rst_file = nb_file[:nb_file.rfind('.')] + '.' + exporter.file_extension

        # this could be improved to only change the double quotes from
        # cross references (ie  :.*:``.*`` -> :.*:`.*`)
        find_replace(rst_file, r'``', '`')
Esempio n. 11
0
def go(source_dir):
    print(os.listdir(source_dir))
    if not os.path.exists(source_dir):
        raise ValueError ('source_dir doesnt exist')
        
    print('----- starting NB Evaluation and Conversion')
    logging.basicConfig(level=logging.DEBUG, format=log_format, datefmt=log_datefmt)
    nb_files = recursive_find_by_filter(source_dir, '*.ipynb')
    print('source dir is  %s'%source_dir)
    for nb_file in nb_files:
        print(nb_file)
        basename = os.path.basename(nb_file)
        notebook_name = basename[:basename.rfind('.')]
        build_directory = os.path.dirname(nb_file)
        
        r = NotebookRunner(nb_file, pylab=True)
        r.run_notebook()
        r.save_notebook(nb_file)
        
        
        exporter = RSTExporter()
        writer =  FilesWriter()
        
        resources={}
        resources['output_files_dir'] = '%s_files' % notebook_name
        
        output, resources = exporter.from_notebook_node(r.nb, 
                                                resources = resources)
        
        
        writer.build_directory = build_directory
        writer.write(output, resources, notebook_name=notebook_name)
        rst_file = nb_file[:nb_file.rfind('.')]+'.'+exporter.file_extension
        
        # this could be improved to only change the double quotes from
        # cross references (ie  :.*:``.*`` -> :.*:`.*`)
        find_replace(rst_file,r'``','`')
Esempio n. 12
0
def main():
    log_format = '%(asctime)s %(message)s'
    log_datefmt = '%m/%d/%Y %I:%M:%S %p'

    parser = argparse.ArgumentParser()
    parser.add_argument('input_file',
            help='.ipynb file to run')
    parser.add_argument('output_file', nargs='?',
            help='.ipynb file to save cell output to')
    parser.add_argument('--quiet', '-q', action='store_true',
            help='don\'t print anything unless things go wrong')
    parser.add_argument('--overwrite', '-o', action='store_true',
            help='write notebook output back to original notebook')
    parser.add_argument('--html', nargs='?', default=False,
            help='output an HTML snapshot of the notebook')
    parser.add_argument('--template', nargs='?', default=False,
            help='template to use for HTML output')
    parser.add_argument('--pylab', action='store_true',
            help='start notebook with pylab enabled')
    parser.add_argument('--matplotlib', action='store_true',
            help='start notebook with matplotlib inlined')
    parser.add_argument('--skip-exceptions', '-s', action='store_true',
            help='if an exception occurs in a cell, continue running the subsequent cells')
    args = parser.parse_args()


    if args.overwrite:
        if args.output_file is not None:
            print('Error: output_filename must not be provided if '
                    '--overwrite (-o) given', file=stderr)
            exit(1)
        else:
            args.output_file = args.input_file

    if not args.quiet:
        logging.basicConfig(level=logging.DEBUG, format=log_format, datefmt=log_datefmt)


    nb_runner = NotebookRunner(args.input_file, args.pylab, args.matplotlib)

    exit_status = 0
    try:
        nb_runner.run_notebook(skip_exceptions=args.skip_exceptions)
    except NotebookError:
        exit_status = 1

    if args.output_file:
        nb_runner.save_notebook(args.output_file)

    if args.html is not False:
        if args.html is None:
            # if --html is given but no filename is provided,
            # come up with a sane output name based on the
            # input filename
            if args.input_file.endswith('.ipynb'):
                args.html = args.input_file[:-6] + '.html'
            else:
                args.html = args.input_file + '.ipynb'

        if args.template is False:
            exporter = HTMLExporter()
        else:
            exporter = HTMLExporter(config=Config({'HTMLExporter':{'default_template':args.template}}))

        logging.info('Saving HTML snapshot to %s' % args.html)
        output, resources = exporter.from_notebook_node(nb_runner.nb)
        codecs.open(args.html, 'w', encoding='utf-8').write(output)

    if exit_status != 0:
        logging.warning('Exiting with nonzero exit status')
    exit(exit_status)
Esempio n. 13
0
def main():
    # TODO: options:
    # - output:
    #   - save HTML report (nbconvert)

    print 'ko'
    log_format = '%(asctime)s %(message)s'
    log_datefmt = '%m/%d/%Y %I:%M:%S %p'

    parser = argparse.ArgumentParser()
    parser.add_argument('input_file',
            help='.ipynb file to run')
    parser.add_argument('output_file', nargs='?',
            help='.ipynb file to save cell output to')
    parser.add_argument('--quiet', '-q', action='store_true',
            help='don\'t print anything unless things go wrong')
    parser.add_argument('--overwrite', '-o', action='store_true',
            help='write notebook output back to original notebook')
    parser.add_argument('--html', nargs='?', default=False,
            help='output an HTML snapshot of the notebook')
    parser.add_argument('--pylab', action='store_true',
            help='start notebook with pylab enabled')
    args = parser.parse_args()


    if args.overwrite:
        if args.output_file is not None:
            print >> stderr, 'Error: output_filename must not be provided if '\
                    '--overwrite (-o) given'
            exit(1)
        else:
            args.output_file = args.input_file

    if not args.quiet:
        logging.basicConfig(level=logging.DEBUG, format=log_format, datefmt=log_datefmt)


    nb_runner = NotebookRunner(args.input_file, args.pylab)

    exit_status = 0
    try:
        nb_runner.run_notebook()
    except NotebookError:
        exit_status = 1

    if args.output_file:
        nb_runner.save_notebook(args.output_file)

    if args.html is not False:
        if args.html is None:
            # if --html is given but no filename is provided,
            # come up with a sane output name based on the
            # input filename
            if args.input_file.endswith('.ipynb'):
                args.html = args.input_file[:-6] + '.html'
            else:
                args.html = args.input_file + '.ipynb'

        logging.info('Saving HTML snapshot to %s' % args.html)
        exporter = HTMLExporter()
        output, resources = exporter.from_notebook_node(nb_runner.nb)
        codecs.open(args.html, 'w', encoding='utf-8').write(output)

    if exit_status != 0:
        logging.warning('Exiting with nonzero exit status')
    exit(exit_status)
Esempio n. 14
0
File: main.py Progetto: zonca/runipy
def main():
    log_format = '%(asctime)s %(message)s'
    log_datefmt = '%m/%d/%Y %I:%M:%S %p'

    parser = argparse.ArgumentParser()
    parser.add_argument('input_file', help='.ipynb file to run')
    parser.add_argument('output_file',
                        nargs='?',
                        help='.ipynb file to save cell output to')
    parser.add_argument('--quiet',
                        '-q',
                        action='store_true',
                        help='don\'t print anything unless things go wrong')
    parser.add_argument('--overwrite',
                        '-o',
                        action='store_true',
                        help='write notebook output back to original notebook')
    parser.add_argument('--html',
                        nargs='?',
                        default=False,
                        help='output an HTML snapshot of the notebook')
    parser.add_argument('--template',
                        nargs='?',
                        default=False,
                        help='template to use for HTML output')
    parser.add_argument('--pylab',
                        action='store_true',
                        help='start notebook with pylab enabled')
    parser.add_argument('--matplotlib',
                        action='store_true',
                        help='start notebook with matplotlib inlined')
    parser.add_argument(
        '--skip-exceptions',
        '-s',
        action='store_true',
        help=
        'if an exception occurs in a cell, continue running the subsequent cells'
    )
    args = parser.parse_args()

    if args.overwrite:
        if args.output_file is not None:
            print(
                'Error: output_filename must not be provided if '
                '--overwrite (-o) given',
                file=stderr)
            exit(1)
        else:
            args.output_file = args.input_file

    if not args.quiet:
        logging.basicConfig(level=logging.DEBUG,
                            format=log_format,
                            datefmt=log_datefmt)

    nb_runner = NotebookRunner(args.input_file, args.pylab, args.matplotlib)

    exit_status = 0
    try:
        nb_runner.run_notebook(skip_exceptions=args.skip_exceptions)
    except NotebookError:
        exit_status = 1

    if args.output_file:
        nb_runner.save_notebook(args.output_file)

    if args.html is not False:
        if args.html is None:
            # if --html is given but no filename is provided,
            # come up with a sane output name based on the
            # input filename
            if args.input_file.endswith('.ipynb'):
                args.html = args.input_file[:-6] + '.html'
            else:
                args.html = args.input_file + '.ipynb'

        if args.template is False:
            exporter = HTMLExporter()
        else:
            exporter = HTMLExporter(config=Config(
                {'HTMLExporter': {
                    'default_template': args.template
                }}))

        logging.info('Saving HTML snapshot to %s' % args.html)
        output, resources = exporter.from_notebook_node(nb_runner.nb)
        codecs.open(args.html, 'w', encoding='utf-8').write(output)

    if exit_status != 0:
        logging.warning('Exiting with nonzero exit status')
    exit(exit_status)