Exemplo n.º 1
0
def translatenb_v3(cells):
    from nbformat.v3 import (new_code_cell, new_text_cell, new_worksheet,
                             new_notebook, new_metadata, new_author)
    from nbformat.v3 import new_text_cell

    nb = new_worksheet()
    for cell_type, language, block in cells:
        block = '\n'.join(block)

        if cell_type == 'markdown':
            nb.cells.append(new_text_cell(u'markdown', source=block))
        elif cell_type == 'code':
            nb.cells.append(new_code_cell(input=block))
        elif cell_type == 'raw':
            nb.cells.append(new_text_cell('raw', source=block))
        else:
            raise ValueError(
                'Wrong cell_type was given [{}]'.format(cell_type))

    nb = new_notebook(worksheets=[nb], metadata=new_metadata())
    # upgrade notebook to v4
    from nbformat.v4 import upgrade
    nb = upgrade(nb)
    import nbformat.v4.nbjson as nbjson
    return nbjson.writes(nb)
Exemplo n.º 2
0
def downgrade(nb):
    """Convert a v4 notebook to v3.

    Parameters
    ----------
    nb : NotebookNode
        The Python representation of the notebook to convert.
    """
    if nb.nbformat != nbformat:
        return nb

    # Validate the notebook before conversion
    _warn_if_invalid(nb, nbformat)

    nb.nbformat = v3.nbformat
    nb.nbformat_minor = v3.nbformat_minor
    cells = [downgrade_cell(cell) for cell in nb.pop('cells')]
    nb.worksheets = [v3.new_worksheet(cells=cells)]
    nb.metadata.setdefault('name', '')

    # Validate the converted notebook before returning it
    _warn_if_invalid(nb, v3.nbformat)

    nb.orig_nbformat = nb.metadata.pop('orig_nbformat', nbformat)
    nb.orig_nbformat_minor = nb.metadata.pop('orig_nbformat_minor',
                                             nbformat_minor)

    return nb
Exemplo n.º 3
0
def downgrade(nb):
    """Convert a v4 notebook to v3.

    Parameters
    ----------
    nb : NotebookNode
        The Python representation of the notebook to convert.
    """
    if nb.nbformat != nbformat:
        return nb

    # Validate the notebook before conversion
    _warn_if_invalid(nb, nbformat)

    nb.nbformat = v3.nbformat
    nb.nbformat_minor = v3.nbformat_minor
    cells = [ downgrade_cell(cell) for cell in nb.pop('cells') ]
    nb.worksheets = [v3.new_worksheet(cells=cells)]
    nb.metadata.setdefault('name', '')

    # Validate the converted notebook before returning it
    _warn_if_invalid(nb, v3.nbformat)

    nb.orig_nbformat = nb.metadata.pop('orig_nbformat', nbformat)
    nb.orig_nbformat_minor = nb.metadata.pop('orig_nbformat_minor', nbformat_minor)

    return nb
Exemplo n.º 4
0
    def convert(self):
        from nbformat.v3 import (
            new_notebook,
            new_worksheet,
            new_code_cell,
            new_text_cell,
            writes_json,
        )

        ws = new_worksheet()

        for chunk in self.doc.parsed:
            if chunk["type"] == "doc":
                # TODO: this relies on pandoc converting into
                # markdown
                fmt = u"markdown"
                doc = self.format_docchunk(chunk["content"])
                ws.cells.append(new_text_cell(fmt, source=doc))
            if chunk["type"] == "code":
                lang = u"python"
                code = chunk["content"]
                ws.cells.append(new_code_cell(input=code, language=lang))

        NB = new_notebook(name="Pweaved ipython notebook", worksheets=[ws])

        self.converted = writes_json(NB)
def make_notebook(outfile, metadata, conf_args=None):
    """Create notebook with parsed contents from metadata"""
    nb = nbf.new_notebook()

    cells = []
    # Create a notebook by Library type existing in the metadata file
    for samples_df in get_samples_by_library_type(metadata, conf_args['sep']):
        cells.extend(create_cells(samples_df, conf_args=conf_args))

    nb['worksheets'].append(nbf.new_worksheet(cells=cells))

    with open(outfile, 'w') as _:
        nbformat.write(nb, _)
Exemplo n.º 6
0
    def convert(self):
        from nbformat.v3 import (new_notebook, new_worksheet,
                                     new_code_cell, new_text_cell,
                                     writes_json)
        ws = new_worksheet()

        for chunk in self.doc.parsed:
            if chunk["type"] == "doc":
                # TODO: this relies on pandoc converting into
                # markdown
                fmt = u'markdown'
                doc = self.format_docchunk(chunk['content'])
                ws.cells.append(new_text_cell(fmt, source=doc))
            if chunk["type"] == "code":
                lang = u'python'
                code = chunk['content']
                ws.cells.append(new_code_cell(input=code, language=lang))

        NB = new_notebook(name='Pweaved ipython notebook',
                          worksheets=[ws])

        self.converted = writes_json(NB)
Exemplo n.º 7
0
def write(cells, nb_version=4):
    """Turn cells list into valid IPython notebook code."""
    # Use IPython.nbformat functionality for writing the notebook
    
    Dmetadata = {
        "anaconda-cloud": {},
        "kernalspec": {
            "display_name": "Python [Root]",
            "language": "python",
            "name": "Python [Root]"
        },
        "language_info": {
            "codemirror_mode": {
            "name": "ipython",
            "version": 3
            }
        },
        "file_extension": ".py",
        "mimetype": "test/x-python",
        "name": "python",
        "nbconvert_exporter": "python",
        "pygments_lexer": "ipython3",
        "version": "3.5.2"
    }

    if nb_version == 3:
        from nbformat.v3 import (
            new_code_cell, new_text_cell, new_worksheet,
            new_notebook, new_metadata, new_author)
        nb = new_worksheet()

    elif nb_version == 4:
        from nbformat.v4 import (
            new_code_cell, new_markdown_cell, new_notebook)
        nb_cells = []

    for cell_tp, language, block in cells:
        if cell_tp == 'markdown':
            if nb_version == 3:
                nb.cells.append(
                    new_text_cell(u'markdown', source=block))
            elif nb_version == 4:
                nb_cells.append(
                    new_markdown_cell(source=block))
        elif cell_tp == 'codecell':
            if nb_version == 3:
                nb.cells.append(new_code_cell(input=block))
            elif nb_version == 4:
                nb_cells.append(new_code_cell(source=block))

    if nb_version == 3:
        nb = new_notebook(worksheets=[nb], metadata=new_metadata())
        # Let us make v4 notebook here by upgrading
        from nbformat.v4 import upgrade
        nb = upgrade(nb)
        import nbformat.v4.nbjson as nbjson
        # Convert nb to json format
        filestr = nbjson.writes(nb)
    elif nb_version == 4:
        nb = new_notebook(cells=nb_cells, metadata=Dmetadata)
        from nbformat import writes
        filestr = writes(nb, version=4)
    return filestr