Beispiel #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)
Beispiel #2
0
    def test_splitlines(self):
        """Test splitlines in mime-bundles"""
        s = writes(nb0, split_lines=True)
        raw_nb = json.loads(s)

        for i, ref_cell in enumerate(nb0.cells):
            if ref_cell.source.strip() == "Cell with attachments":
                attach_ref = ref_cell["attachments"]["attachment1"]
                attach_json = raw_nb["cells"][i]["attachments"]["attachment1"]
            if ref_cell.source.strip() == "json_outputs()":
                output_ref = ref_cell["outputs"][0]["data"]
                output_json = raw_nb["cells"][i]["outputs"][0]["data"]

        for key, json_value in attach_json.items():
            if key == "text/plain":
                # text should be split
                assert json_value == attach_ref["text/plain"].splitlines(True)
            else:
                # JSON attachments
                assert json_value == attach_ref[key]

        # check that JSON outputs are left alone:
        for key, json_value in output_json.items():
            if key == "text/plain":
                # text should be split
                assert json_value == output_ref["text/plain"].splitlines(True)
            else:
                # JSON outputs should be left alone
                assert json_value == output_ref[key]
Beispiel #3
0
 def test_read_jpeg(self):
     """JPEG output data is b64 unicode"""
     s = writes(nb0)
     nb1 = nbjson.reads(s)
     found_jpeg = False
     for cell in nb1.cells:
         if "outputs" not in cell:
             continue
         for output in cell.outputs:
             if "data" not in output:
                 continue
             if "image/jpeg" in output.data:
                 found_jpeg = True
                 jpegdata = output.data["image/jpeg"]
                 self.assertEqual(type(jpegdata), str)
                 # test that it is valid b64 data
                 b64bytes = jpegdata.encode("ascii")
                 raw_bytes = decodebytes(b64bytes)
     assert found_jpeg, "never found jpeg output"
Beispiel #4
0
 def test_roundtrip_split(self):
     """Ensure that splitting multiline blocks is safe"""
     # This won't differ from test_roundtrip unless the default changes
     s = writes(nb0, split_lines=True)
     self.assertEqual(nbjson.reads(s), nb0)
Beispiel #5
0
 def test_roundtrip_nosplit(self):
     """Ensure that multiline blobs are still readable"""
     # ensures that notebooks written prior to splitlines change
     # are still readable.
     s = writes(nb0, split_lines=False)
     self.assertEqual(nbjson.reads(s), nb0)
Beispiel #6
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