예제 #1
0
def convert(py_file, ipynb_file):
    cells = []
    imports = ""  # needed for each file

    for cell, line_type, line in parse_blocks(py_file):
        if line_type == file_type:
            # write cell to file
            try:
                fname = line.split()[1]
            except:
                raise Exception(
                    "Markdown for file output must be the "
                    + "following format\n#to_file filename.py: "
                    + "{}\n".format(line)
                )
                sys.exit(0)

            with open(fname, "w") as f:
                f.write(cell)
                new_cell = "%run {}".format(fname)
                ipynb_cell = nbf.new_code_cell(new_cell)
                cells.append(ipynb_cell)

        else:
            # convert cell to ipynb cell
            ipynb_cell = nbf.new_code_cell(cell)
            cells.append(ipynb_cell)

    # create new notebook
    nb = nbf.new_notebook()
    nb["worksheets"].append(nbf.new_worksheet(cells=cells))

    with open(ipynb_file, "w") as f:
        nbf.write(nb, f, "ipynb")
예제 #2
0
def run_and_save(src_notebook, dst_notebook, verbose=False, **kwargs):
    """
    Run a notebook; populate its output cells; save as a new notebook.

    Params
    ------
    src_notebook : file path of the source notebook
    dst_notebook : file path of the location to save the executed notebook
    verbose : set to true if for printed status messages
    kwargs : passed to `run_notebook`
    """
    if verbose:
        print("Running %s" % src_notebook)

    with open(src_notebook) as f:
        nb = reads(f.read(), 'json')

    n_errors = run_notebook(nb, **kwargs)
    if verbose:
        print("\tNumber of errors: %d" % n_errors)

    if verbose:
        print("\tSaving to destination %s" % dst_notebook)

    with io.open(dst_notebook, 'w', encoding='utf8') as f:
        write(nb, f, 'json')

    if verbose:
        print("\tDone!")

    return n_errors
예제 #3
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)
예제 #4
0
    def test_very_long_cells(self):
        """
        Torture test that long cells do not cause issues
        """
        lorem_ipsum_text = textwrap.dedent("""\
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
          dignissim, ipsum non facilisis tempus, dui felis tincidunt metus,
          nec pulvinar neque odio eget risus. Nulla nisi lectus, cursus
          suscipit interdum at, ultrices sit amet orci. Mauris facilisis
          imperdiet elit, vitae scelerisque ipsum dignissim non. Integer
          consequat malesuada neque sit amet pulvinar. Curabitur pretium
          ut turpis eget aliquet. Maecenas sagittis lacus sed lectus
          volutpat, eu adipiscing purus pulvinar. Maecenas consequat
          luctus urna, eget cursus quam mollis a. Aliquam vitae ornare
          erat, non hendrerit urna. Sed eu diam nec massa egestas pharetra
          at nec tellus. Fusce feugiat lacus quis urna sollicitudin volutpat.
          Quisque at sapien non nibh feugiat tempus ac ultricies purus.
           """)
        lorem_ipsum_text = lorem_ipsum_text.replace("\n", " ") + "\n\n"
        large_lorem_ipsum_text = "".join([lorem_ipsum_text] * 3000)

        notebook_name = "lorem_ipsum_long.ipynb"
        tex_name = "lorem_ipsum_long.tex"
        with self.create_temp_cwd([]):
            nb = current.new_notebook(worksheets=[
                current.new_worksheet(cells=[
                    current.new_text_cell('markdown',
                                          source=large_lorem_ipsum_text)
                ])
            ])
            with open(notebook_name, 'w') as f:
                current.write(nb, f, 'ipynb')
            self.call('nbconvert --to latex --log-level 0 ' +
                      os.path.join(notebook_name))
            assert os.path.isfile(tex_name)
예제 #5
0
 def save_notebook_object(self, notebook_id, nb):
     """Save an existing notebook object by notebook_id."""
     if notebook_id not in self.mapping:
         raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
     old_name = self.mapping[notebook_id]
     try:
         new_name = nb.metadata.name
     except AttributeError:
         raise web.HTTPError(400, u'Missing notebook name')
     path = self.get_path_by_name(new_name)
     try:
         with open(path,'w') as f:
             current.write(nb, f, u'json')
     except Exception as e:
         raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e)
     # save .py script as well
     if self.save_script:
         pypath = os.path.splitext(path)[0] + '.py'
         try:
             with io.open(pypath,'w', encoding='utf-8') as f:
                 current.write(nb, f, u'py')
         except Exception as e:
             raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s' % e)
     
     if old_name != new_name:
         old_path = self.get_path_by_name(old_name)
         if os.path.isfile(old_path):
             os.unlink(old_path)
         if self.save_script:
             old_pypath = os.path.splitext(old_path)[0] + '.py'
             if os.path.isfile(old_pypath):
                 os.unlink(old_pypath)
         self.mapping[notebook_id] = new_name
         self.rev_mapping[new_name] = notebook_id
예제 #6
0
    def test_very_long_cells(self):
        """
        Torture test that long cells do not cause issues
        """
        lorem_ipsum_text = textwrap.dedent("""\
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
          dignissim, ipsum non facilisis tempus, dui felis tincidunt metus,
          nec pulvinar neque odio eget risus. Nulla nisi lectus, cursus
          suscipit interdum at, ultrices sit amet orci. Mauris facilisis
          imperdiet elit, vitae scelerisque ipsum dignissim non. Integer
          consequat malesuada neque sit amet pulvinar. Curabitur pretium
          ut turpis eget aliquet. Maecenas sagittis lacus sed lectus
          volutpat, eu adipiscing purus pulvinar. Maecenas consequat
          luctus urna, eget cursus quam mollis a. Aliquam vitae ornare
          erat, non hendrerit urna. Sed eu diam nec massa egestas pharetra
          at nec tellus. Fusce feugiat lacus quis urna sollicitudin volutpat.
          Quisque at sapien non nibh feugiat tempus ac ultricies purus.
           """)
        lorem_ipsum_text = lorem_ipsum_text.replace("\n"," ") + "\n\n"
        large_lorem_ipsum_text = "".join([lorem_ipsum_text]*3000)

        notebook_name = "lorem_ipsum_long.ipynb"
        tex_name = "lorem_ipsum_long.tex"
        with self.create_temp_cwd([]):
            nb = current.new_notebook(
                worksheets=[
                    current.new_worksheet(cells=[
                        current.new_text_cell('markdown',source=large_lorem_ipsum_text)
                    ])
                ]
            )
            with open(notebook_name, 'w') as f: current.write(nb, f, 'ipynb')
            self.call('nbconvert --to latex --log-level 0 ' + 
                      os.path.join(notebook_name))
            assert os.path.isfile(tex_name)
예제 #7
0
    def setUp(self):
        nbdir = self.notebook_dir.name
        self.blob = os.urandom(100)
        self.b64_blob = base64.encodestring(self.blob).decode('ascii')

        for d in (self.dirs + self.hidden_dirs):
            d.replace('/', os.sep)
            if not os.path.isdir(pjoin(nbdir, d)):
                os.mkdir(pjoin(nbdir, d))

        for d, name in self.dirs_nbs:
            d = d.replace('/', os.sep)
            # create a notebook
            with io.open(pjoin(nbdir, d, '%s.ipynb' % name),
                         'w',
                         encoding='utf-8') as f:
                nb = new_notebook(name=name)
                write(nb, f, format='ipynb')

            # create a text file
            with io.open(pjoin(nbdir, d, '%s.txt' % name),
                         'w',
                         encoding='utf-8') as f:
                f.write(self._txt_for_name(name))

            # create a binary file
            with io.open(pjoin(nbdir, d, '%s.blob' % name), 'wb') as f:
                f.write(self._blob_for_name(name))

        self.api = API(self.base_url())
예제 #8
0
def convert_nb(nbname):
    rst_name = "%s.rst" % nbname
    nbname = "%s.ipynb" % nbname

    # Do nothing if already built.
    if os.path.exists(rst_name) and \
            os.path.getmtime(rst_name) >= os.path.getmtime(nbname):
        print("\t%s is up to date; nothing to do." % rst_name)
        return

    os.system("runipy --o %s --matplotlib --quiet" % nbname)

    with io.open(nbname, 'r', encoding='utf8') as f:
        nb = current.read(f, 'json')
    nb = clean_for_doc(nb)
    print("Writing to", nbname)
    with io.open(nbname, 'w', encoding='utf8') as f:
        current.write(nb, f, 'json')

    # Convert to rst.
    os.system("jupyter nbconvert --to rst %s" % nbname)

    with io.open(nbname, 'r', encoding='utf8') as f:
        nb = current.read(f, 'json')
    nb = strip_output(nb)
    print("Writing to", nbname)
    with io.open(nbname, 'w', encoding='utf8') as f:
        current.write(nb, f, 'json')
예제 #9
0
def convert_nb(nbname):
    rst_name = "%s.rst" % nbname
    nbname = "%s.ipynb" % nbname

    # Do nothing if already built.
    if os.path.exists(rst_name) and \
            os.path.getmtime(rst_name) >= os.path.getmtime(nbname):
        print("\t%s is up to date; nothing to do." % rst_name)
        return

    os.system("runipy --o %s --matplotlib --quiet" % nbname)

    with io.open(nbname, 'r', encoding='utf8') as f:
        nb = current.read(f, 'json')
    nb = clean_for_doc(nb)
    print("Writing to", nbname)
    with io.open(nbname, 'w', encoding='utf8') as f:
        current.write(nb, f, 'json')

    # Convert to rst.
    os.system("ipython nbconvert --to rst %s" % nbname)

    with io.open(nbname, 'r', encoding='utf8') as f:
        nb = current.read(f, 'json')
    nb = strip_output(nb)
    print("Writing to", nbname)
    with io.open(nbname, 'w', encoding='utf8') as f:
        current.write(nb, f, 'json')
예제 #10
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
예제 #11
0
파일: git_catalog.py 프로젝트: nywfan/bokeh
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
예제 #12
0
    def setUp(self):
        nbdir = self.notebook_dir.name
        self.blob = os.urandom(100)
        self.b64_blob = base64.encodestring(self.blob).decode('ascii')



        for d in (self.dirs + self.hidden_dirs):
            d.replace('/', os.sep)
            if not os.path.isdir(pjoin(nbdir, d)):
                os.mkdir(pjoin(nbdir, d))

        for d, name in self.dirs_nbs:
            d = d.replace('/', os.sep)
            # create a notebook
            with io.open(pjoin(nbdir, d, '%s.ipynb' % name), 'w',
                         encoding='utf-8') as f:
                nb = new_notebook(name=name)
                write(nb, f, format='ipynb')

            # create a text file
            with io.open(pjoin(nbdir, d, '%s.txt' % name), 'w',
                         encoding='utf-8') as f:
                f.write(self._txt_for_name(name))

            # create a binary file
            with io.open(pjoin(nbdir, d, '%s.blob' % name), 'wb') as f:
                f.write(self._blob_for_name(name))

        self.api = API(self.base_url())
예제 #13
0
def main():
    fname = sys.argv[1]
    nb_fname = splitext(fname)[0] + '.ipynb'
    res = parse_tex(fname)
    with open(nb_fname, 'wt') as fobj:
        nbf.write(res, fobj, 'ipynb')
    run_notebook(res)
예제 #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)
예제 #15
0
def main():
    fname = sys.argv[1]
    nb_fname = splitext(fname)[0] + '.ipynb'
    res = parse_tex(fname)
    with open(nb_fname, 'wt') as fobj:
        nbf.write(res, fobj, 'ipynb')
    run_notebook(res)
예제 #16
0
def run_and_save(src_notebook, dst_notebook, verbose=False, **kwargs):
    """
    Run a notebook; populate its output cells; save as a new notebook.

    Params
    ------
    src_notebook : file path of the source notebook
    dst_notebook : file path of the location to save the executed notebook
    verbose : set to true if for printed status messages
    kwargs : passed to `run_notebook`
    """
    if verbose:
        print("Running %s" % src_notebook)

    with open(src_notebook) as f:
        nb = reads(f.read(), 'json')

    n_errors = run_notebook(nb, **kwargs)
    if verbose:
        print("\tNumber of errors: %d" % n_errors)

    if verbose:
        print("\tSaving to destination %s" % dst_notebook)

    with io.open(dst_notebook, 'w', encoding='utf8') as f:
        write(nb, f, 'json')

    if verbose:
        print("\tDone!")

    return n_errors
예제 #17
0
def mdstrip(paths):
    for path in paths:

        if os.path.isdir(path):
            files = glob(os.path.join(path, "*.ipynb"))
        else:
            files = [path]
            
        for in_file in files:
            input_nb_name = basename(in_file)
            slug = input_nb_name[:-6]
            title = slug.replace("_", " ")
            actual_title_nb = io.StringIO(
                    title_data.replace("{{ title }}", title))
            title_nb = nbf.read(actual_title_nb, "ipynb")
            title_cell = title_nb.worksheets[0].cells[0]
            input_nb = nbf.read(open(in_file), "ipynb")
            worksheet = input_nb.worksheets[0]
            # add graphic here & append to cell_list
            cell_list = [title_cell]
            for cell in worksheet.cells:
                if cell.cell_type == ("code"):
                    cell.outputs = []
                    cell_list.append(cell)
                elif cell.cell_type == "heading":
                    cell_list.append(cell)
            output_nb = nbf.new_notebook()
            output_nb_name = slug+".prod.ipynb"
            output_nb.worksheets.append(nbf.new_worksheet(cells=cell_list))
            
            with open(output_nb_name, 'w') as f:
                nbf.write(output_nb, f, "ipynb")
예제 #18
0
def process_notebook_file(fname, actions=('clean',), asciidoc_fname=None):
    print("Performing '{}' on: {}".format("', '".join(actions), fname))

    orig_wd = os.getcwd()
    os.chdir(os.path.dirname(fname))

    with io.open(fname, 'rb') as f:
        nb = current.read(f, 'json')

    if 'uuid' in actions:
        check_uuid(nb)

    if 'render' in actions:
        run_notebook(nb)
    elif 'check' in actions:
        run_notebook(deepcopy(nb))

    if 'clean' in actions:
        remove_outputs(nb)

    if 'merge' in actions:
        doc = AsciidDoc(asciidoc_fname)
        doc.merge_code_from(nb)
        doc.save()
    elif 'convert' in actions:
        doc = AsciidDoc()
        doc.convert_from(nb)
        doc.save(asciidoc_fname)

    os.chdir(orig_wd)

    with io.open(fname, 'wb') as f:
        current.write(nb, f, 'json')
예제 #19
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
예제 #20
0
파일: snippet.py 프로젝트: szabo92/gistable
def process_notebook_file(fname, actions=('clean', ), asciidoc_fname=None):
    print("Performing '{}' on: {}".format("', '".join(actions), fname))

    orig_wd = os.getcwd()
    os.chdir(os.path.dirname(fname))

    with io.open(fname, 'rb') as f:
        nb = current.read(f, 'json')

    if 'uuid' in actions:
        check_uuid(nb)

    if 'render' in actions:
        run_notebook(nb)
    elif 'check' in actions:
        run_notebook(deepcopy(nb))

    if 'clean' in actions:
        remove_outputs(nb)

    if 'merge' in actions:
        doc = AsciidDoc(asciidoc_fname)
        doc.merge_code_from(nb)
        doc.save()
    elif 'convert' in actions:
        doc = AsciidDoc()
        doc.convert_from(nb)
        doc.save(asciidoc_fname)

    os.chdir(orig_wd)

    with io.open(fname, 'wb') as f:
        current.write(nb, f, 'json')
예제 #21
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
예제 #22
0
def test_notebook_reformat_py():
    with TemporaryDirectory() as td:
        infile = os.path.join(td, "nb.ipynb")
        with io.open(infile, 'w', encoding='utf-8') as f:
            current.write(nb0, f, 'json')
            
        _ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
        _ip.magic("notebook -f py %s" % infile)
예제 #23
0
def test_notebook_reformat_py():
    with TemporaryDirectory() as td:
        infile = os.path.join(td, "nb.ipynb")
        with io.open(infile, 'w', encoding='utf-8') as f:
            current.write(nb0, f, 'json')

        _ip.ex(py3compat.u_format("u = {u}'héllo'"))
        _ip.magic("notebook -f py %s" % infile)
예제 #24
0
    def setUp(self):
        nbdir = self.notebook_dir.name
        os.mkdir(pjoin(nbdir, 'foo'))

        with io.open(pjoin(nbdir, 'foo', 'nb1.ipynb'), 'w') as f:
            nb = new_notebook(name='nb1')
            write(nb, f, format='ipynb')

        self.sess_api = SessionAPI(self.base_url())
예제 #25
0
def test_notebook_reformat_json():
    with TemporaryDirectory() as td:
        infile = os.path.join(td, "nb.py")
        with io.open(infile, "w", encoding="utf-8") as f:
            current.write(nb0, f, "py")

        _ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
        _ip.magic("notebook -f ipynb %s" % infile)
        _ip.magic("notebook -f json %s" % infile)
예제 #26
0
 def write_notebook(self, bundle_path, name, nb):
     nb_path = os.path.join(bundle_path, name)
     if 'name' in nb['metadata']:
         nb['metadata']['name'] = u''
     try:
         with io.open(nb_path, 'w', encoding='utf-8') as f:
             current.write(nb, f, u'json')
     except Exception as e:
         raise Exception(u'Unexpected error while autosaving notebook: %s %s' % (nb_path, e))
예제 #27
0
def downgrade_ipynb(fname):
    base, ext = os.path.splitext(fname)
    newname = base+'.v2'+ext
    print "downgrading %s -> %s" % (fname, newname)
    with io.open(fname, 'r', encoding='utf8') as f:
        nb = current.read(f, 'json')
    nb = downgrade(nb)
    with open(newname, 'w') as f:
        current.write(nb, f, 'json')
예제 #28
0
 def new_notebook(self):
     """Create a new notebook and return its notebook_id."""
     path, name = self.increment_filename('Untitled')
     notebook_id = self.new_notebook_id(name)
     metadata = current.new_metadata(name=name)
     nb = current.new_notebook(metadata=metadata)
     with open(path, 'w') as f:
         current.write(nb, f, u'json')
     return notebook_id
예제 #29
0
 def new_notebook(self):
     """Create a new notebook and return its notebook_id."""
     path, name = self.increment_filename('Untitled')
     notebook_id = self.new_notebook_id(name)
     metadata = current.new_metadata(name=name)
     nb = current.new_notebook(metadata=metadata)
     with open(path,'w') as f:
         current.write(nb, f, u'json')
     return notebook_id
 def generateFinalCrossTabReport(self, output_notebook_base_directory,
                                 output_dir, outputFname,
                                 notebook_base_url):
     self.nb['worksheets'].append(nbf.new_worksheet(cells=self.cells))
     with open(
             output_notebook_base_directory + output_dir + "/" +
             outputFname, 'w') as f:
         nbf.write(self.nb, f, 'ipynb')
     report_url = notebook_base_url + "notebooks/" + output_dir + "/" + outputFname
     return report_url, outputFname
예제 #31
0
    def test_notebook_reformat_py(self):
        from IPython.nbformat.v3.tests.nbexamples import nb0
        from IPython.nbformat import current
        with TemporaryDirectory() as td:
            infile = os.path.join(td, "nb.ipynb")
            with io.open(infile, 'w', encoding='utf-8') as f:
                current.write(nb0, f, 'json')

            _ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
            _ip.magic("notebook -f py %s" % infile)
예제 #32
0
    def test_notebook_reformat_py(self):
        from IPython.nbformat.v3.tests.nbexamples import nb0
        from IPython.nbformat import current

        with TemporaryDirectory() as td:
            infile = os.path.join(td, "nb.ipynb")
            with io.open(infile, "w", encoding="utf-8") as f:
                current.write(nb0, f, "json")

            _ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
            _ip.magic("notebook -f py %s" % infile)
예제 #33
0
    def _save_notebook(self, os_path, model, name="", path=""):
        """save a notebook file"""
        # Save the notebook file
        nb = current.to_notebook_json(model["content"])

        self.check_and_sign(nb, name, path)

        if "name" in nb["metadata"]:
            nb["metadata"]["name"] = u""

        with atomic_writing(os_path, encoding="utf-8") as f:
            current.write(nb, f, u"json")
예제 #34
0
파일: ipython.py 프로젝트: dunovank/nengo
def export_evaluated(nb, dest_path=None, skip_exceptions=False):
    """Convert notebook to an evaluated notebook.

    Optionally saves the notebook to dest_path.
    """
    nb_runner = NotebookRunner(nb)
    nb_runner.run_notebook(skip_exceptions=skip_exceptions)

    if dest_path is not None:
        with open(dest_path, 'w') as f:
            current.write(nb_runner.nb, f, 'json')
    return nb_runner.nb
예제 #35
0
    def _save_notebook(self, os_path, model, name='', path=''):
        """save a notebook file"""
        # Save the notebook file
        nb = current.to_notebook_json(model['content'])

        self.check_and_sign(nb, name, path)

        if 'name' in nb['metadata']:
            nb['metadata']['name'] = u''

        with atomic_writing(os_path, encoding='utf-8') as f:
            current.write(nb, f, version=nb.nbformat)
예제 #36
0
    def _save_notebook(self, os_path, model, name='', path=''):
        """save a notebook file"""
        # Save the notebook file
        nb = current.to_notebook_json(model['content'])

        self.check_and_sign(nb, name, path)

        if 'name' in nb['metadata']:
            nb['metadata']['name'] = u''

        with io.open(os_path, 'w', encoding='utf-8') as f:
            current.write(nb, f, u'json')
예제 #37
0
    def _save_notebook(self, os_path, model, name='', path=''):
        """save a notebook file"""
        # Save the notebook file
        nb = current.to_notebook_json(model['content'])

        self.check_and_sign(nb, name, path)

        if 'name' in nb['metadata']:
            nb['metadata']['name'] = u''

        with atomic_writing(os_path, encoding='utf-8') as f:
            current.write(nb, f, u'json')
    def save_notebook(self, model, name, path=''):
        """Save the notebook model and return the model with no content."""

        self.log.debug("save_notebook(%s, '%s', '%s')",
                       model, str(name), str(path))
        assert name.endswith(self.filename_ext)
        path = path.strip('/')

        if 'content' not in model:
            raise web.HTTPError(400, u'No notebook JSON data provided')
        
        # One checkpoint should always exist
        if self.notebook_exists(name, path) \
           and not self.list_checkpoints(name, path):
            self.create_checkpoint(name, path)

        new_path = model.get('path', path)
        new_name = model.get('name', name)

        if path != new_path or name != new_name:
            self._rename_notebook(name, path, new_name, new_path)

        # Create the path and notebook entries if necessary
        if new_path not in self.tree:
            self.tree[new_path] = {}
        if new_name not in self.tree[new_path]:
            self.tree[new_path][new_name] = \
                   dict(created = tz.utcnow(), checkpoints=[])
        notebook = self.tree[new_path][new_name]

        # Save the notebook file
        nb = current.to_notebook_json(model['content'])
        self.check_and_sign(nb, new_path, new_name)
        if 'name' in nb['metadata']:
            nb['metadata']['name'] = u''
        ipynb_stream = StringIO()
        current.write(nb, ipynb_stream, u'json')
        notebook['ipynb'] = ipynb_stream.getvalue()
        notebook['ipynb_last_modified'] = tz.utcnow()
        ipynb_stream.close()

        # Save .py script as well
        py_stream = StringIO()
        current.write(nb, py_stream, u'json')
        notebook['py'] = py_stream.getvalue()
        notebook['py_last_modified'] = tz.utcnow()
        py_stream.close()

        # Return model
        model = self.get_notebook(new_name, new_path, content=False)
        self.log.debug("save_notebook -> %s", model)
        return model
def main():
    parser = ArgumentParser(description=DESCRIP, epilog=EPILOG, formatter_class=RawDescriptionHelpFormatter)
    parser.add_argument("filename", type=str, nargs="+", help="notebook filenames")
    args = parser.parse_args()
    for fname in args.filename:
        with io.open(fname, "r") as f:
            nb = current.read(f, "json")
        for cell in cellgen(nb, "code"):
            if hasattr(cell, "prompt_number"):
                del cell["prompt_number"]
            cell.outputs = []
        with io.open(fname, "w") as f:
            current.write(nb, f, "ipynb")
예제 #40
0
    def setUp(self):
        nbdir = self.notebook_dir.name
        try:
            os.mkdir(pjoin(nbdir, "foo"))
        except OSError as e:
            # Deleting the folder in an earlier test may have failed
            if e.errno != errno.EEXIST:
                raise

        with io.open(pjoin(nbdir, "foo", "nb1.ipynb"), "w", encoding="utf-8") as f:
            nb = new_notebook(name="nb1")
            write(nb, f, format="ipynb")

        self.sess_api = SessionAPI(self.base_url())
예제 #41
0
 def save_notebook_object(self, nb, path):
     try:
         with open(path,'w') as f:
             current.write(nb, f, u'json')
     except Exception as e:
         raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e)
     # save .py script as well
     if self.save_script:
         pypath = os.path.splitext(path)[0] + '.py'
         try:
             with io.open(pypath,'w', encoding='utf-8') as f:
                 current.write(nb, f, u'py')
         except Exception as e:
             raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s' % e)
예제 #42
0
    def setUp(self):
        nbdir = self.notebook_dir.name
        try:
            os.mkdir(pjoin(nbdir, 'foo'))
        except OSError as e:
            # Deleting the folder in an earlier test may have failed
            if e.errno != errno.EEXIST:
                raise

        with io.open(pjoin(nbdir, 'foo', 'nb1.ipynb'), 'w') as f:
            nb = new_notebook(name='nb1')
            write(nb, f, format='ipynb')

        self.sess_api = SessionAPI(self.base_url())
예제 #43
0
    def setUp(self):
        nbdir = self.notebook_dir.name
        try:
            os.mkdir(pjoin(nbdir, 'foo'))
        except OSError as e:
            # Deleting the folder in an earlier test may have failed
            if e.errno != errno.EEXIST:
                raise

        with io.open(pjoin(nbdir, 'foo', 'nb1.ipynb'), 'w') as f:
            nb = new_notebook(name='nb1')
            write(nb, f, format='ipynb')

        self.sess_api = SessionAPI(self.base_url())
예제 #44
0
    def save_notebook(self, model, name='', path=''):
        """Save the notebook model and return the model with no content."""
        path = path.strip('/')

        if 'content' not in model:
            raise web.HTTPError(400, u'No notebook JSON data provided')

        # One checkpoint should always exist
        if self.notebook_exists(
                name, path) and not self.list_checkpoints(name, path):
            self.create_checkpoint(name, path)

        new_path = model.get('path', path).strip('/')
        new_name = model.get('name', name)

        if path != new_path or name != new_name:
            self.rename_notebook(name, path, new_name, new_path)

        # Save the notebook file
        os_path = self._get_os_path(new_name, new_path)
        nb = current.to_notebook_json(model['content'])

        self.check_and_sign(nb, new_name, new_path)

        if 'name' in nb['metadata']:
            nb['metadata']['name'] = u''
        try:
            self.log.debug("Autosaving notebook %s", os_path)
            with io.open(os_path, 'w', encoding='utf-8') as f:
                current.write(nb, f, u'json')
        except Exception as e:
            raise web.HTTPError(
                400, u'Unexpected error while autosaving notebook: %s %s' %
                (os_path, e))

        # Save .py script as well
        if self.save_script:
            py_path = os.path.splitext(os_path)[0] + '.py'
            self.log.debug("Writing script %s", py_path)
            try:
                with io.open(py_path, 'w', encoding='utf-8') as f:
                    current.write(nb, f, u'py')
            except Exception as e:
                raise web.HTTPError(
                    400,
                    u'Unexpected error while saving notebook as script: %s %s'
                    % (py_path, e))

        model = self.get_notebook(new_name, new_path, content=False)
        return model
예제 #45
0
    def write_notebook_object(self, nb, notebook_id=None):
        """Save an existing notebook object by notebook_id."""
        try:
            new_name = nb.metadata.name
        except AttributeError:
            raise web.HTTPError(400, u'Missing notebook name')

        if notebook_id is None:
            notebook_id = self.new_notebook_id(new_name)

        if notebook_id not in self.mapping:
            raise web.HTTPError(404,
                                u'Notebook does not exist: %s' % notebook_id)

        old_name = self.mapping[notebook_id]
        path = self.get_path_by_name(new_name)
        try:
            with open(path, 'w') as f:
                current.write(nb, f, u'json')
        except Exception as e:
            raise web.HTTPError(
                400, u'Unexpected error while saving notebook: %s' % e)

        # save .py script as well
        if self.save_script:
            pypath = os.path.splitext(path)[0] + '.py'
            try:
                with io.open(pypath, 'w', encoding='utf-8') as f:
                    current.write(nb, f, u'py')
            except Exception as e:
                raise web.HTTPError(
                    400,
                    u'Unexpected error while saving notebook as script: %s' %
                    e)

        # remove old files if the name changed
        if old_name != new_name:
            old_path = self.get_path_by_name(old_name)
            if os.path.isfile(old_path):
                os.unlink(old_path)
            if self.save_script:
                old_pypath = os.path.splitext(old_path)[0] + '.py'
                if os.path.isfile(old_pypath):
                    os.unlink(old_pypath)
            self.mapping[notebook_id] = new_name
            self.rev_mapping[new_name] = notebook_id
            del self.rev_mapping[old_name]

        return notebook_id
예제 #46
0
    def write_notebook(self, result, output='parsed-output.ipynb'):
        """
        based on StackOverflow quetsion and answer:
            http://stackoverflow.com/questions/17972273/
            is-there-a-ipython-notebook-api
        """

        # open an empty notebook
        notebook = current.reads('', format='py')
        # add all elements as seperate cells
        notebook['worksheets'][0]['cells'] = list(map(current.new_code_cell,
                                                      result))
        # Save the notebook
        with io.open(output, 'w', encoding='utf-8') as f:
            current.write(notebook, f, format='ipynb')
예제 #47
0
    def setUp(self):
        nbdir = self.notebook_dir.name

        for d in self.dirs:
            d.replace('/', os.sep)
            if not os.path.isdir(pjoin(nbdir, d)):
                os.mkdir(pjoin(nbdir, d))

        for d, name in self.dirs_nbs:
            d = d.replace('/', os.sep)
            with io.open(pjoin(nbdir, d, '%s.ipynb' % name), 'w') as f:
                nb = new_notebook(name=name)
                write(nb, f, format='ipynb')

        self.nb_api = NBAPI(self.base_url())
예제 #48
0
    def setUp(self):
        nbdir = self.notebook_dir.name

        for d in self.dirs:
            d.replace('/', os.sep)
            if not os.path.isdir(pjoin(nbdir, d)):
                os.mkdir(pjoin(nbdir, d))

        for d, name in self.dirs_nbs:
            d = d.replace('/', os.sep)
            with io.open(pjoin(nbdir, d, '%s.ipynb' % name), 'w') as f:
                nb = new_notebook(name=name)
                write(nb, f, format='ipynb')

        self.nb_api = NBAPI(self.base_url())
예제 #49
0
    def save_notebook(self, model, name='', path=''):
        path = path.strip('/')

        if 'content' not in model:
            raise web.HTTPError(400, u'No notebook JSON data provided')

        # One checkpoint should always exist
        if self.notebook_exists(
                name, path) and not self.list_checkpoints(name, path):
            self.create_checkpoint(name, path)

        new_path = model.get('path', path).strip('/')
        new_name = model.get('name', name)

        if path != new_path or name != new_name:
            self.rename_notebook(name, path, new_name, new_path)

        # Save the notebook file
        nb = current.to_notebook_json(model['content'])

        self.check_and_sign(nb, new_name, new_path)

        if 'name' in nb['metadata']:
            nb['metadata']['name'] = u''
        try:
            with StringIO() as f:
                current.write(nb, f, u'json')
                spec = {'path': path, 'name': name}
                data = {
                    '$set': {
                        'type': 'notebook',
                        'content': f.getvalue(),
                        'lastModified': datetime.datetime.now(),
                    }
                }
                f.close()
                if 'created' in model:
                    data['$set']['created'] = model['created']
                else:
                    data['$set']['created'] = datetime.datetime.now()
                notebook = self._connect_collection(
                    self.notebook_collection).update(spec, data, upsert=True)
        except Exception as e:
            raise web.HTTPError(
                400, u'Unexpected error while autosaving notebook: %s' % (e))
        model = self.get_notebook(new_name, new_path, content=False)

        return model
def convert_file(path, outputdir, is_convert_simple = False):
    filename = os.path.basename(path)
    
    notebookname, _ = os.path.splitext(filename)
    if is_convert_simple:
        with open(path, 'r') as stream:
            notebook = notebook_format.read(stream, u'py')
    else:
        with open(path, 'r') as stream:
            sourcestring = stream.read()
        
        
        notebook = new_notebook_from_string(notebookname, filename, sourcestring)
        
    with open(os.path.join(outputdir, notebookname+'.ipynb'),'w') as stream:
        notebook_format.write(notebook, stream, u'json')
예제 #51
0
 def new_notebook(self):
     """Create a new notebook and returns its notebook_id."""
     i = 0
     while True:
         name = u'Untitled%i' % i
         path = self.get_path_by_name(name)
         if not os.path.isfile(path):
             break
         else:
             i = i + 1
     notebook_id = self.new_notebook_id(name)
     metadata = current.new_metadata(name=name)
     nb = current.new_notebook(metadata=metadata)
     with open(path, 'w') as f:
         current.write(nb, f, u'json')
     return notebook_id
def convert_file(path, outputdir, is_convert_simple=False):
    filename = os.path.basename(path)

    notebookname, _ = os.path.splitext(filename)
    if is_convert_simple:
        with open(path, 'r') as stream:
            notebook = notebook_format.read(stream, 'py')
    else:
        with open(path, 'r') as stream:
            sourcestring = stream.read()

        notebook = new_notebook_from_string(notebookname, filename,
                                            sourcestring)

    with open(os.path.join(outputdir, notebookname + '.ipynb'), 'w') as stream:
        notebook_format.write(notebook, stream, 'json')
예제 #53
0
 def new_notebook(self):
     """Create a new notebook and returns its notebook_id."""
     i = 0
     while True:
         name = u"Untitled%i" % i
         path = self.get_path_by_name(name)
         if not os.path.isfile(path):
             break
         else:
             i = i + 1
     notebook_id = self.new_notebook_id(name)
     metadata = current.new_metadata(name=name)
     nb = current.new_notebook(metadata=metadata)
     with open(path, "w") as f:
         current.write(nb, f, u"json")
     return notebook_id
예제 #54
0
    def run(self):

        from nbformat import read, write

        for notebook in glob.glob('?.???/*.ipynb'):

            with open(notebook, 'r') as f:
                nb = read(f, 4)

            for cell in nb['cells']:
                if cell.cell_type == 'code':
                    cell.outputs = []
                    if 'prompt_number' in cell:
                        cell.pop('prompt_number')

            with open(notebook, 'w') as f:
                write(nb, f)
예제 #55
0
def remove_outputs(fname):
    """
    remove the outputs from a notebook "fname" and create a new notebook
    """
    with io.open(fname, 'r') as f:
	nb = read(f, 'json')
    for ws in nb.worksheets:
        for cell in ws.cells:
            if cell.cell_type == 'code':
                cell.outputs = []

    base, ext = os.path.splitext(fname)
    new_ipynb = "%s_removed%s" % (base, ext)
    with io.open(new_ipynb, 'w', encoding='utf8') as f:
        write(nb, f, 'json')
    print "wrote %s" % new_ipynb

    return "Done"
예제 #56
0
    def run(self):

        from IPython.nbformat.current import read, write

        for notebook in glob.glob('notebooks/*.ipynb'):

            with open(notebook, 'r') as f:
                nb = read(f, 'json')

            for ws in nb.worksheets:
                for cell in ws.cells:
                    if cell.cell_type == 'code':
                        cell.outputs = []
                        if 'prompt_number' in cell:
                            cell.pop('prompt_number')

            with open(notebook, 'w') as f:
                write(nb, f, 'json')
예제 #57
0
def main():
    parser = ArgumentParser(description=DESCRIP,
                            epilog=EPILOG,
                            formatter_class=RawDescriptionHelpFormatter)
    parser.add_argument('filename',
                        type=str,
                        nargs='+',
                        help='notebook filenames')
    args = parser.parse_args()
    for fname in args.filename:
        with io.open(fname, 'r') as f:
            nb = current.read(f, 'json')
        for cell in cellgen(nb, 'code'):
            if hasattr(cell, 'prompt_number'):
                del cell['prompt_number']
            cell.outputs = []
        with io.open(fname, 'w') as f:
            current.write(nb, f, 'ipynb')
예제 #58
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)