Пример #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)
Пример #2
0
def test_downgrade_heading():
    v3h = v3.new_heading_cell
    v4m = v4.new_markdown_cell
    v3m = lambda source: v3.new_text_cell('markdown', source)
    for v4cell, expected in [
        (
            v4m(source='# foo'),
            v3h(source='foo', level=1),
        ),
        (
            v4m(source='#foo'),
            v3h(source='foo', level=1),
        ),
        (
            v4m(source='#\tfoo'),
            v3h(source='foo', level=1),
        ),
        (
            v4m(source='# \t  foo'),
            v3h(source='foo', level=1),
        ),
        (
            v4m(source='# foo\nbar'),
            v3m(source='# foo\nbar'),
        ),
    ]:
        downgraded = convert.downgrade_cell(v4cell)
        assert downgraded == expected
Пример #3
0
def test_downgrade_heading():
    v3h = v3.new_heading_cell
    v4m = v4.new_markdown_cell
    v3m = lambda source: v3.new_text_cell('markdown', source)
    for v4cell, expected in [
        (
            v4m(source='# foo'),
            v3h(source='foo', level=1),
        ),
        (
            v4m(source='#foo'),
            v3h(source='foo', level=1),
        ),
        (
            v4m(source='#\tfoo'),
            v3h(source='foo', level=1),
        ),
        (
            v4m(source='# \t  foo'),
            v3h(source='foo', level=1),
        ),
        (
            v4m(source='# foo\nbar'),
            v3m(source='# foo\nbar'),
        ),
    ]:
        downgraded = convert.downgrade_cell(v4cell)
        assert downgraded == expected
 def to_list(self):
     cells = []
     if self.description:
         cells.append(nbf.new_text_cell('markdown', source=self.description))
     if self.contents:
         cells.append(nbf.new_code_cell(input=self.header + self.contents))
     return cells
Пример #5
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)
Пример #6
0
def test_downgrade_heading():
    v3h = v3.new_heading_cell
    v4m = v4.new_markdown_cell
    v3m = lambda source: v3.new_text_cell("markdown", source)  # noqa
    for v4cell, expected in [
        (
            v4m(source="# foo"),
            v3h(source="foo", level=1),
        ),
        (
            v4m(source="#foo"),
            v3h(source="foo", level=1),
        ),
        (
            v4m(source="#\tfoo"),
            v3h(source="foo", level=1),
        ),
        (
            v4m(source="# \t  foo"),
            v3h(source="foo", level=1),
        ),
        (
            v4m(source="# foo\nbar"),
            v3m(source="# foo\nbar"),
        ),
    ]:
        downgraded = convert.downgrade_cell(v4cell)
        assert downgraded == expected
Пример #7
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)
Пример #8
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