Ejemplo n.º 1
0
def evaluate_notebook(nb_path, dest_path=None, skip_exceptions=True):
    # Create evaluated version and save it to the dest path.
    # Always use --pylab so figures appear inline
    # perhaps this is questionable?
    curr_dir = os.getcwd()
    os.chdir(os.path.dirname(nb_path))

    notebook = read(open(nb_path), "json")
    nb_runner = NotebookRunner(notebook, pylab=True)
    try:
        nb_runner.run_notebook(skip_exceptions=skip_exceptions)
    except NotebookError as e:
        print ""
        print e
        # Return the traceback, filtering out ANSI color codes.
        # http://stackoverflow.com/questions/13506033/filtering-out-ansi-escape-sequences
        return "Notebook conversion failed with the following traceback: \n%s" % re.sub(
            r"\\033[\[\]]([0-9]{1,2}([;@][0-9]{0,2})*)*[mKP]?", "", str(e)
        )
    if dest_path is None:
        dest_path = "temp_evaluated.ipynb"
    write(nb_runner.nb, open(dest_path, "w"), "json")
    ret = nb_to_html(dest_path)
    if dest_path is "temp_evaluated.ipynb":
        os.remove(dest_path)
    os.chdir(curr_dir)
    return ret
Ejemplo n.º 2
0
 def testRunNotebooks(self):
     notebook_dir = path.join('tests', 'input')
     for notebook_path in glob(path.join(notebook_dir, '*.ipynb')):
         notebook_file = path.basename(notebook_path)
         print(notebook_file)
         expected_file = path.join('tests', 'expected', notebook_file)
         notebook = ""
         with open(notebook_path) as notebook_file:
             notebook = notebook_file.read()
         try:
             # IPython 3
             notebook = reads(notebook, 3)
         except (TypeError, NBFormatError):
             # IPython 2
             notebook = reads(notebook, 'json')
         runner = NotebookRunner(notebook, working_dir=notebook_dir)
         runner.run_notebook(True)
         expected = ""
         with open(expected_file) as notebook_file:
             expected = notebook_file.read()
         try:
             # IPython 3
             expected = reads(expected, 3)
         except (TypeError, NBFormatError):
             # IPython 2
             expected = reads(expected, 'json')
         self.assert_notebooks_equal(expected, runner.nb)
Ejemplo n.º 3
0
def test_notebook(filename):
    nb = read(open(filename), 'json')

    expected = deepcopy(nb['worksheets'][0]['cells'])

    # TODO: figure out how to set the log-level for IPython here
    r = NotebookRunner(nb)
    r.run_notebook()



    actual = r.nb['worksheets'][0]['cells']

    failed = []

    for exp, act in zip(expected, actual):
        if exp['cell_type'] == 'code':
            #print "comparing outputs for ", exp['input']
            for eo,ao in zip(exp['outputs'], act['outputs']):
                #print "\t", eo['text'], ao['text'], eo['text']==ao['text']
                eotext = ['\n'.join(l for l in eo['text'].split('\n') if _filter_exceptions(l))]
                aotext = ['\n'.join(l for l in ao['text'].split('\n') if _filter_exceptions(l))]
                if eotext != aotext:
                    #print "\tFAILED"
                    failed.append({'prompt_number': exp['prompt_number'], 'input': exp['input'], 'expected_output': eotext, 'actual_output': aotext})


    return len(failed)==0, failed, r.nb
Ejemplo n.º 4
0
def evaluate_notebook(nb_path, dest_path=None, skip_exceptions=True):
    # Create evaluated version and save it to the dest path.
    # Always use --pylab so figures appear inline
    # perhaps this is questionable?
    curr_dir = os.getcwd()
    os.chdir(os.path.dirname(nb_path))

    notebook = read(open(nb_path), 'json')
    nb_runner = NotebookRunner(notebook, pylab=True)
    try:
        nb_runner.run_notebook(skip_exceptions=skip_exceptions)
    except NotebookError as e:
        print ''
        print e
        # Return the traceback, filtering out ANSI color codes.
        # http://stackoverflow.com/questions/13506033/filtering-out-ansi-escape-sequences
        return 'Notebook conversion failed with the following traceback: \n%s' % \
            re.sub(r'\\033[\[\]]([0-9]{1,2}([;@][0-9]{0,2})*)*[mKP]?', '', str(e))
    if dest_path is None:
        dest_path = 'temp_evaluated.ipynb'
    write(nb_runner.nb, open(dest_path, 'w'), 'json')
    ret = nb_to_html(dest_path)
    if dest_path is 'temp_evaluated.ipynb':
        os.remove(dest_path)
    os.chdir(curr_dir)
    return ret
Ejemplo n.º 5
0
 def testRunNotebooks(self):
     notebook_dir = path.join('tests', 'input')
     for notebook_path in glob(path.join(notebook_dir, '*.ipynb')):
         notebook_file = path.basename(notebook_path)
         print(notebook_file)
         expected_file = path.join('tests', 'expected', notebook_file)
         notebook = ""
         with open(notebook_path) as notebook_file:
             notebook = notebook_file.read()
         try:
             # IPython 3
             notebook = reads(notebook, 3)
         except (TypeError, NBFormatError):
             # IPython 2
             notebook = reads(notebook, 'json')
         runner = NotebookRunner(notebook, working_dir=notebook_dir)
         runner.run_notebook(True)
         expected = ""
         with open(expected_file) as notebook_file:
             expected = notebook_file.read()
         try:
             # IPython 3
             expected = reads(expected, 3)
         except (TypeError, NBFormatError):
             # IPython 2
             expected = reads(expected, 'json')
         self.assert_notebooks_equal(expected, runner.nb)
Ejemplo n.º 6
0
def test_nbs():
    path = os.path.join(pyfolio_root(), "examples", "*.ipynb")
    for ipynb in glob.glob(path):
        with open(ipynb) as f:
            nb = read_notebook(f, "json")
            nb_runner = NotebookRunner(nb)
            nb_runner.run_notebook(skip_exceptions=False)
Ejemplo n.º 7
0
def run_ipython_notebook(notebook_str):
    """
    References:
        https://github.com/paulgb/runipy
        >>> from utool.util_ipynb import *  # NOQA
    """
    from runipy.notebook_runner import NotebookRunner
    import nbformat
    import logging
    log_format = '%(asctime)s %(levelname)s: %(message)s'
    log_datefmt = '%m/%d/%Y %I:%M:%S %p'
    logging.basicConfig(
        level=logging.INFO, format=log_format, datefmt=log_datefmt
    )
    #fpath = 'tmp.ipynb'
    #notebook_str = ut.readfrom(fpath)
    #nb3 = IPython.nbformat.reads(notebook_str, 3)
    #cell = nb4.cells[1]
    #self = runner
    #runner = NotebookRunner(nb3, mpl_inline=True)
    print('Executing IPython notebook')
    nb4 = nbformat.reads(notebook_str, 4)
    runner = NotebookRunner(nb4)
    runner.run_notebook(skip_exceptions=False)
    run_nb = runner.nb
    return run_nb
Ejemplo n.º 8
0
def test_nbs():
    path = os.path.join(pyfolio_root(), 'examples', '*.ipynb')
    for ipynb in glob.glob(path):
        with open(ipynb) as f:
            nb = read(f, 'json')
            nb_runner = NotebookRunner(nb)
            nb_runner.run_notebook(skip_exceptions=False)
Ejemplo n.º 9
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?

    notebook = read(open(nb_path), 'json')
    cwd = os.getcwd()
    filedir, filename = os.path.split(nb_path)
    os.chdir(filedir)
    nb_runner = NotebookRunner(notebook, pylab=False)
    try:
        nb_runner.run_notebook(skip_exceptions=skip_exceptions)
    except NotebookError as e:
        print('')
        print(e)
        # Return the traceback, filtering out ANSI color codes.
        # http://stackoverflow.com/questions/13506033/filtering-out-ansi-escape-sequences
        return 'Notebook conversion failed with the following traceback: \n%s' % \
            re.sub(r'\\033[\[\]]([0-9]{1,2}([;@][0-9]{0,2})*)*[mKP]?', '', str(e))
    os.chdir(cwd)
    write(nb_runner.nb, open(dest_path, 'w'), 'json')

    ret = nb_to_html(dest_path)
    if dest_path.endswith('temp_evaluated.ipynb'):
        os.remove(dest_path)
    return ret
Ejemplo n.º 10
0
def run_notebooks(selected_nb_re=None):
    """ Run the tutorial notebooks. """
    from runipy.notebook_runner import NotebookRunner

    _orig_path = os.getcwd()

    # walk through each directory in tutorials/ to find all .ipynb file
    for tutorial_filename,nb in walk_through_tutorials(only_published=True,
                                selected_nb_re=selected_nb_re):
        path,filename = os.path.split(tutorial_filename)

        if filename.startswith("_run_"):
            continue

        logger.info("Running tutorial: {}".format(filename))

        # notebook file
        output_filename = os.path.join(path,"_run_{}"
                                       .format(filename))

        # prepend _run_ to the notebook names to create new files
        #   so the user isn't left with a bunch of modified files.
        os.chdir(path)
        r = NotebookRunner(nb, mpl_inline=True)
        r.run_notebook(skip_exceptions=True)
        write(r.nb, open(output_filename, 'w'), 'json')

    os.chdir(_orig_path)
Ejemplo n.º 11
0
def get_notebook_params(path):
    from runipy.notebook_runner import NotebookRunner
    from IPython.nbformat.current import read

    # Load the notebook:
    with open(path) as f:
        notebook = read(f, 'json')
    r = NotebookRunner(notebook)

    def callback(cell_idx):
        try:
            # Has the %params_done magic been called yet?
            has_params = pull(r.shell,
                "get_ipython().magics_manager.registry['ParameterMagics']"
                ".has_params")
        except:
            # The magic probably hasn't been loaded yet.
            pass
        else:
            if has_params: raise StopIteration() # Yes, we're done!

    # Run the notebook, checking whether we're done after every cell:
    try: r.run_notebook(progress_callback=callback)
    except StopIteration: pass

    # Pull the param declarations:
    try: params = pull(r.shell,
                "get_ipython().magics_manager.registry['ParameterMagics']"
                ".params")
    except: params = dict()
    return params
Ejemplo n.º 12
0
Archivo: main.py Proyecto: 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)
Ejemplo n.º 13
0
def nb_run(fname):
    """ given a valid notebook path, run and save notebook """

    # initialize error flag
    err = False

    # file name
    fn = fname

    # run notebook
    try:
        notebook = read(open(fn), 'json')
        r = NotebookRunner(notebook)
        r.run_notebook()
        err = False
    except Exception as e:
        logger.error(e)
        logger.debug('failed to run notebook')
        err = True

    # save notebook
    try:
        write(r.nb, open(fn, 'w'), 'json')
        err = False
    except Exception as e:
        logger.error(e)
        logger.debug('failed to save notebook')
        err = True

    return err
Ejemplo n.º 14
0
def run_notebooks(selected_nb_re=None):
    """ Run the tutorial notebooks. """
    from runipy.notebook_runner import NotebookRunner

    _orig_path = os.getcwd()

    # walk through each directory in tutorials/ to find all .ipynb file
    for tutorial_filename,nb in walk_through_tutorials(only_published=True,
                                selected_nb_re=selected_nb_re):
        path,filename = os.path.split(tutorial_filename)

        if filename.startswith("_run_"):
            continue

        logger.info("Running tutorial: {}".format(filename))

        # notebook file
        output_filename = os.path.join(path,"_run_{}"
                                       .format(filename))

        # prepend _run_ to the notebook names to create new files
        #   so the user isn't left with a bunch of modified files.
        os.chdir(path)
        r = NotebookRunner(nb, mpl_inline=True)
        r.run_notebook(skip_exceptions=True)
        write(r.nb, open(output_filename, 'w'), 'json')

    os.chdir(_orig_path)
Ejemplo n.º 15
0
def run_asm_stats(assembly, out_file):
    os.environ["FASTA_FILE"] = assembly
    notebook = read(open("masmvaliweb/notebooks/assembly-stats.ipynb"), 'json')
    r = NotebookRunner(notebook)
    r.run_notebook()
    os.remove(assembly)
    exportHTML = HTMLExporter(config=Config({'HTMLExporter': {'default_template': 'basic'}}))
    with open(out_file, 'w') as of:
        of.write(exportHTML.from_notebook_node(r.nb)[0])
Ejemplo n.º 16
0
 def testRunNotebooks(self):
     input_glob = path.join('tests', 'input', '*.ipynb')
     for notebook_file in glob(input_glob):
         notebook_file_base = path.basename(notebook_file)
         print notebook_file_base
         expected_file = path.join('tests', 'expected', notebook_file_base)
         runner = NotebookRunner(notebook_file)
         runner.run_notebook(True)
         expected = read(open(expected_file), 'json')
         self.assert_notebooks_equal(expected, runner.nb)
Ejemplo n.º 17
0
 def testRunNotebooks(self):
     input_glob = path.join('tests', 'input', '*.ipynb')
     for notebook_file in glob(input_glob):
         notebook_file_base = path.basename(notebook_file)
         print notebook_file_base
         expected_file = path.join('tests', 'expected', notebook_file_base)
         runner = NotebookRunner(read(open(notebook_file), 'json'))
         runner.run_notebook(True)
         expected = read(open(expected_file), 'json')
         self.assert_notebooks_equal(expected, runner.nb)
Ejemplo n.º 18
0
 def testRunNotebooks(self):
     notebook_dir = path.join('tests', 'input')
     for notebook_path in glob(path.join(notebook_dir, '*.ipynb')):
         notebook_file = path.basename(notebook_path)
         print notebook_file
         expected_file = path.join('tests', 'expected', notebook_file)
         runner = NotebookRunner(read(open(notebook_path), 'json'), working_dir=notebook_dir)
         runner.run_notebook(True)
         expected = read(open(expected_file), 'json')
         self.assert_notebooks_equal(expected, runner.nb)
Ejemplo n.º 19
0
def test_tutorial_notebook():
    from IPython.nbformat.current import read
    from runipy.notebook_runner import NotebookRunner

    rootdir = os.path.join(aospy.__path__[0], 'examples')
    with open(os.path.join(rootdir, 'tutorial.ipynb')) as nb:
        notebook = read(nb, 'json')
    r = NotebookRunner(notebook)
    r.run_notebook()
    r.shutdown_kernel()
Ejemplo n.º 20
0
 def testRunNotebooks(self):
     notebook_dir = path.join('tests', 'input')
     for notebook_path in glob(path.join(notebook_dir, '*.ipynb')):
         notebook_file = path.basename(notebook_path)
         print notebook_file
         expected_file = path.join('tests', 'expected', notebook_file)
         runner = NotebookRunner(read(open(notebook_path), 'json'), working_dir=notebook_dir)
         runner.run_notebook(True)
         expected = read(open(expected_file), 'json')
         self.assert_notebooks_equal(expected, runner.nb)
Ejemplo n.º 21
0
def run(notebook):
    """Run a notebook using runipy."""
    try:
        from runipy.notebook_runner import NotebookRunner
    except ImportError:
        raise('You need runipy installed to run notebooks!'
              'try `pip install runipy`')

    runner = NotebookRunner(notebook)
    runner.run_notebook()
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
Ejemplo n.º 23
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
Ejemplo n.º 24
0
def run(nbtxt, output=None, view=False):
    """ Run a notebook app 100% from JSON (text stream), return the JSON (text stream)

        :param nbtxt: JSON representation of notebook app, ready to run
        :param view:   don't invoke notebook, just view in current form

        NOTE: `view` probably isn't useful, since the input will just be output again
    """

    # TODO: support output parameter to specify only returning certain attributes from notebook
    # create a notebook object from the JSON
    nb_obj    = nb_read_json(nbtxt)
    nb_runner = NotebookRunner(nb_obj)
    try: # get the app name from metadata
        name  = nb_obj['metadata']['conda.app']['name']
    except KeyError as ex:
        name  = "nbapp"

    try:
        if view:
            pass # then don't run it
        else:
            nb_runner.run_notebook(skip_exceptions=False)
        return nb_runner.nb

    except Empty as ex:
        sys.stderr.write("IPython Kernel timeout")
        err = mini_markdown_nb("""
Notebook Error
==============
ERROR: IPython Kernel timeout
```
{error}
```
""".format(error=str(ex).split(':')[-1]))
        return err
    except (NotImplementedError, NotebookError, ValueError) as ex:
        msg = str(ex).splitlines()[-1]
        sys.stderr.write(msg)
        err = mini_markdown_nb("""
Notebook Error
==============
Notebook contains unsupported feature or bad argument:
```
{error}
```
""".format(error=msg))
        return err
    except ImportError:
        msg = "nodejs or pandoc must be installed"
        sys.stderr.write(msg)
        err = mini_markdown_nb(msg)
        return err
Ejemplo n.º 25
0
def check_ipython_notebook():

    notebooks = glob.glob("*ipynb")
    N = len(notebooks)

    pb = Progress(N)
    for i, filename in enumerate(notebooks):
        print(purple(filename))
        notebook = read(open(filename), 'json')
        r = NotebookRunner(notebook)
        r.run_notebook()
        pb.animate(i + 1)
def run_notebook(nbpath):
    logging.info("Reading notebook '%s'", nbpath)
    with open(nbpath) as nbfile:
        notebook = read(nbfile, "json")

    runner = NotebookRunner(notebook, mpl_inline=True)

    try:
        runner.run_notebook()
    except NotebookError:
        logging.error("An error occurred while executing notebook '%s'. " "Exiting with nonzero exit status", nbpath)
        sys.exit(1)
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
Ejemplo n.º 28
0
def run_notebook(nbpath):
    logging.info("Reading notebook '%s'", nbpath)
    with open(nbpath) as nbfile:
        notebook = nbformat.read(nbfile, as_version=3)

    runner = NotebookRunner(notebook)

    try:
        runner.run_notebook()
    except NotebookError:
        logging.error("An error occurred while executing notebook '%s'. "
                      "Exiting with nonzero exit status", nbpath)
        sys.exit(1)
def check_ipython_notebook():


    notebooks = glob.glob("*ipynb")
    N = len(notebooks)

    pb = Progress(N)
    for i,filename in enumerate(notebooks):
        print(purple(filename))
        notebook = read(open(filename), 'json')
        r = NotebookRunner(notebook)
        r.run_notebook()
        pb.animate(i+1)
Ejemplo n.º 30
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
Ejemplo n.º 31
0
def run_notebook(nbpath):
    logging.info("Reading notebook '%s'", nbpath)
    with open(nbpath) as nbfile:
        notebook = read(nbfile, 'json')

    runner = NotebookRunner(notebook, mpl_inline=True)

    try:
        runner.run_notebook()
    except NotebookError:
        logging.error(
            "An error occurred while executing notebook '%s'. "
            "Exiting with nonzero exit status", nbpath)
        sys.exit(1)
Ejemplo n.º 32
0
def evaluate_notebook(nb_path, dest_path=None):
    # Create evaluated version and save it to the dest path.
    # Always use --pylab so figures appear inline
    # perhaps this is questionable?
    nb_json = nbr(open(nb_path), 'json')
    nb_runner = NotebookRunner(nb_json, pylab=True)
    nb_runner.run_notebook()
    if dest_path is None:
        dest_path = 'temp_evaluated.ipynb'
    #nb_runner.save_notebook(dest_path)
    nbw(nb_runner.nb, open(dest_path, 'w'), 'json')
    ret = nb_to_html(dest_path)
    if dest_path is 'temp_evaluated.ipynb':
        os.remove(dest_path)
    return ret
Ejemplo n.º 33
0
def test_nbs():
    path = os.path.join(pyfolio_root(), 'examples', '*.ipynb')
    for ipynb in glob.glob(path):

        # See if bayesian is useable before we run a test
        if ipynb.endswith('bayesian.ipynb'):
            try:
                import pymc3  # NOQA
            except:
                continue

        with open(ipynb) as f:
            nb = read_notebook(f, 'json')
            nb_runner = NotebookRunner(nb)
            nb_runner.run_notebook(skip_exceptions=False)
Ejemplo n.º 34
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)
Ejemplo n.º 35
0
 def testRunNotebooks(self):
     notebook_dir = path.join("tests", "input")
     for notebook_path in glob(path.join(notebook_dir, "*.ipynb")):
         notebook_file = path.basename(notebook_path)
         print(notebook_file)
         expected_file = path.join("tests", "expected", notebook_file)
         notebook = ""
         with open(notebook_path) as notebook_file:
             notebook = read(notebook_file, "json")
         runner = NotebookRunner(notebook, working_dir=notebook_dir)
         runner.run_notebook(True)
         expected = ""
         with open(expected_file) as notebook_file:
             expected = read(notebook_file, "json")
         self.assert_notebooks_equal(expected, runner.nb)
Ejemplo n.º 36
0
def evaluate_nb_file(nb_in_path):
    """ Load and evaluate notebook at `nb_in_path`

    Parameters
    ----------
    nb_in_path : str
        path to notebook to evaluate

    Returns
    -------
    nb : notebook object
        Evaluated notebook object
    """
    nb_runner = NotebookRunner(nb_in=nb_in_path)
    nb_runner.run_notebook()
    return nb_runner.nb
Ejemplo n.º 37
0
 def test(self):
     payload = open(filename)
     nb = read(payload, 'json')
     # turn off colors, so we can properly read exceptions
     cell_nc = new_code_cell(input='%colors NoColor', prompt_number=0)
     nb.worksheets[0].cells.insert(0, cell_nc)
     
     # workaround for matplotlib backend
     cell_mpl = new_code_cell(input="import matplotlib; matplotlib.use('Agg')", prompt_number=1)
     nb.worksheets[0].cells.insert(1, cell_mpl)
     
     # set working dir to notebook path
     wd = os.path.abspath(os.path.dirname(filename))
     runner = NotebookRunner(nb, working_dir=wd)
     #try:
     runner.run_notebook(skip_exceptions=False)
Ejemplo n.º 38
0
def execute_notebook(nb, resources):
    """Execute notebook. With ExecutePreprocessor using IPython >= 3 or runipy instead.
    """

    if is_ipython_3():
        from IPython.nbconvert.preprocessors import ExecutePreprocessor
        nb, resources = ExecutePreprocessor().preprocess(nb, resources)
    elif runipy_available:
        from runipy.notebook_runner import NotebookRunner
        r = NotebookRunner(nb)
        r.run_notebook(skip_exceptions=True)
        nb = r.nb
    else:
        raise ImportError("Can't execute notebooks. Please install IPython >= 3 or runipy.")

    return nb
Ejemplo n.º 39
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)
Ejemplo n.º 40
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)
Ejemplo n.º 41
0
def execute_notebook(nb, resources):
    """Execute notebook. With ExecutePreprocessor using IPython >= 3 or runipy instead.
    """

    if is_ipython_3():
        from IPython.nbconvert.preprocessors import ExecutePreprocessor
        nb, resources = ExecutePreprocessor().preprocess(nb, resources)
    elif runipy_available:
        from runipy.notebook_runner import NotebookRunner
        r = NotebookRunner(nb)
        r.run_notebook(skip_exceptions=True)
        nb = r.nb
    else:
        raise ImportError(
            "Can't execute notebooks. Please install IPython >= 3 or runipy.")

    return nb
Ejemplo n.º 42
0
def render_nb():
    # need ipynb v3 to play nice with runipy
    notebook = read(open("stock_infos.ipynb"), 3)

    nb = NotebookRunner(notebook, pylab=True)
    nb.run_notebook()

    # need ipynb v4 to play nice with Jupyter
    nb = nbformat.convert(nb.nb, 4)

    html_exporter = HTMLExporter()
    body, resources = html_exporter.from_notebook_node(nb)

    html_file = open("static/stock_infos.html", "w")
    html_file.write(body.encode('utf8', 'ignore'))
    html_file.close()

    return app.send_static_file('stock_infos.html')
Ejemplo n.º 43
0
def render_nb():
	# need ipynb v3 to play nice with runipy
	notebook = read(open("stock_infos.ipynb"), 3)

	nb = NotebookRunner(notebook, pylab=True)
	nb.run_notebook()
	
	# need ipynb v4 to play nice with Jupyter
	nb = nbformat.convert(nb.nb, 4)

	html_exporter = HTMLExporter()
	body, resources = html_exporter.from_notebook_node(nb)

	html_file= open("static/stock_infos.html","w")
	html_file.write(body.encode('utf8', 'ignore'))
	html_file.close()

	return app.send_static_file('stock_infos.html')
Ejemplo n.º 44
0
    def run(self):

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

        from runipy.notebook_runner import NotebookRunner
        from IPython.nbformat.current import read, write

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

        for notebook in glob.glob('notebooks/*.ipynb'):
            print("Running {0}...".format(notebook))
            os.chdir(os.path.dirname(notebook))
            with open(os.path.basename(notebook)) as f:
                r = NotebookRunner(read(f, 'json'), pylab=False)
            r.run_notebook(skip_exceptions=True)
            with open(os.path.basename(notebook), 'w') as f:
                write(r.nb, f, 'json')
            os.chdir(start_dir)
Ejemplo n.º 45
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)
Ejemplo n.º 46
0
def notebooks(env, loader, args):
  nb_path = env.resolve('notebooks')
  res_path = env.resolve('results')
  for filename in os.listdir(nb_path):
    if filename.endswith('.ipynb'):
      nb_name = filename.split('.')[0]
      print('running', nb_name)
      src_path = os.path.join(nb_path, filename)
      html_path = os.path.join(res_path, nb_name + '.html')
      with open(src_path, 'r') as f:
        notebook = read(f, 'json')
        r = NotebookRunner(notebook, working_dir=nb_path)
        r.run_notebook()
        dest_path = tempfile.mkstemp()
        t = tempfile.NamedTemporaryFile()
        with open(t.name, 'w') as g:
          write(r.nb, g)
        exporter = nbconvert.HTMLExporter()
        body, resources = exporter.from_filename(t.name)
        with open(html_path, 'wb') as g:
          g.write(body.encode('utf-8'))
Ejemplo n.º 47
0
def call_notebook(path, **kwargs):
    from runipy.notebook_runner import NotebookRunner
    from IPython.nbformat.current import read

    # Load the notebook:
    with open(path) as f:
        notebook = read(f, 'json')
    r = NotebookRunner(notebook)

    # Inject the call arguments into the kernel and run the notebook:
    inject(r.shell, **kwargs)
    r.run_notebook()

    # The user might want to read some output values from the kernel:
    class NotebookResult(object):
        notebook = r.nb
        def __getattr__(self, name):
            return pull(r.shell, name)
        def __getitem__(self, name):
            return pull(r.shell, name)
    return NotebookResult()
Ejemplo n.º 48
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?
    notebook = read(open(nb_path), 'json')
    nb_runner = NotebookRunner(notebook, pylab=True, mpl_inline=True)
    try:
        nb_runner.run_notebook(skip_exceptions=skip_exceptions)
    except NotebookError as e:
        print('\n', '-' * 80)
        print(e)
        print('-' * 80)
        raise

    if dest_path is None:
        dest_path = 'temp_evaluated.ipynb'
    write(nb_runner.nb, open(dest_path, 'w'), 'json')
    ret = nb_to_html(dest_path)
    if dest_path is 'temp_evaluated.ipynb':
        os.remove(dest_path)
    return ret
Ejemplo n.º 49
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?
    notebook = read(open(nb_path), 'json')
    nb_runner = NotebookRunner(notebook, pylab=True, mpl_inline=True)
    try:
        nb_runner.run_notebook(skip_exceptions=skip_exceptions)
    except NotebookError as e:
        print('\n', '-'*80)
        print(e)
        print('-'*80)
        raise

    if dest_path is None:
        dest_path = 'temp_evaluated.ipynb'
    write(nb_runner.nb, open(dest_path, 'w'), 'json')
    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.
    notebook = nbformat.read(open(nb_path), 'json')
    nb_runner = NotebookRunner(notebook, pylab=False)
    try:
        nb_runner.run_notebook(skip_exceptions=skip_exceptions)
    except NotebookError as e:
        print('')
        print(e)
        # Return the traceback, filtering out ANSI color codes.
        # http://stackoverflow.com/questions/13506033/filtering-out-ansi-escape-sequences
        return "Notebook conversion failed with the " \
               "following traceback: \n%s" % \
            re.sub(r'\\033[\[\]]([0-9]{1,2}([;@][0-9]{0,2})*)*[mKP]?', '',
                   str(e))

    if dest_path is None:
        dest_path = 'temp_evaluated.ipynb'
    nbformat.write(nb_runner.nb, open(dest_path, 'w'), 'json')
    ret = nb_to_html(dest_path)
    if dest_path is 'temp_evaluated.ipynb':
        os.remove(dest_path)
    return ret
Ejemplo n.º 51
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'``', '`')
Ejemplo n.º 52
0
def parse(root):
    """returns list of the NB in root
    """
    dirs = sorted([dn for dn in os.listdir(root) if '00' < dn < '99'])
    r = []
    for sub_dir in dirs:
        f = sorted([
            os.path.join(root, sub_dir, fn)
            for fn in os.listdir(os.path.join(root, sub_dir))
            if ('00' < fn < '99' or fn.startswith('content'))
            and fn.endswith('.ipynb')
        ])
        r.extend(f)
    return r


nb_filenames = ["Index.ipynb", "Index-labs1-6.ipynb"]

nb_filenames.extend(parse('./LABS'))
nb_filenames.extend(parse('.'))

for nb_filename in nb_filenames:
    print('*' * 80)
    print(nb_filename)
    print('*' * 80)
    notebook = read(open(nb_filename), 'json')
    r = NotebookRunner(notebook)
    r.run_notebook()
    r.shutdown_kernel()