Beispiel #1
0
    def setUp(self):
        nbdir = self.notebook_dir.name

        if not os.path.isdir(pjoin(nbdir, 'foo')):
            os.mkdir(pjoin(nbdir, 'foo'))

        nb = new_notebook()

        nb.cells.append(new_markdown_cell(u'Created by test ³'))
        cc1 = new_code_cell(source=u'print(2*6)')
        cc1.outputs.append(new_output(output_type="stream", text=u'12'))
        cc1.outputs.append(
            new_output(
                output_type="execute_result",
                data={'image/png': png_green_pixel},
                execution_count=1,
            ))
        nb.cells.append(cc1)

        with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'),
                     'w',
                     encoding='utf-8') as f:
            write(nb, f, version=4)

        self.nbconvert_api = NbconvertAPI(self.base_url())
    def test_coalesce_replace_streams(self):
        """Are \\r characters handled?"""
        outputs = [
            nbformat.new_output(output_type="stream", name="stdout", text="z"),
            nbformat.new_output(output_type="stream",
                                name="stdout",
                                text="\ra"),
            nbformat.new_output(output_type="stream",
                                name="stdout",
                                text="\nz\rb"),
            nbformat.new_output(output_type="stream",
                                name="stdout",
                                text="\nz"),
            nbformat.new_output(output_type="stream",
                                name="stdout",
                                text="\rc\n"),
            nbformat.new_output(output_type="stream",
                                name="stdout",
                                text="z\rz\rd")
        ]
        cells = [
            nbformat.new_code_cell(source="# None",
                                   execution_count=1,
                                   outputs=outputs)
        ]

        nb = nbformat.new_notebook(cells=cells)
        res = self.build_resources()
        nb, res = coalesce_streams(nb, res)
        outputs = nb.cells[0].outputs
        self.assertEqual(outputs[0].text, u'a\nb\nc\nd')
Beispiel #3
0
    def build_notebook(self):
        """Build a reveal slides notebook in memory for use with tests.
        Overrides base in PreprocessorTestsBase"""

        outputs = [nbformat.new_output(output_type='display_data',
                                       data={'image/svg+xml':self.simple_svg})
                  ]

        cells=[nbformat.new_code_cell(source="", execution_count=1, outputs=outputs)]

        return nbformat.new_notebook(cells=cells)
Beispiel #4
0
 def test_javascript_output(self):
     nb = v4.new_notebook(cells=[
         v4.new_code_cell(outputs=[
             v4.new_output(
                 output_type='display_data',
                 data={'application/javascript': "javascript_output();"})
         ])
     ])
     (output, resources) = HTMLExporter(
         template_file='basic').from_notebook_node(nb)
     self.assertIn('javascript_output', output)
Beispiel #5
0
    def test_empty_code_cell(self):
        """No empty code cells in rst"""
        nbname = self._get_notebook()
        with io.open(nbname, encoding='utf8') as f:
            nb = nbformat.read(f, 4)

        exporter = self.exporter_class()

        (output, resources) = exporter.from_notebook_node(nb)
        # add an empty code cell
        nb.cells.append(v4.new_code_cell(source=""))
        (output2, resources) = exporter.from_notebook_node(nb)
        # adding an empty code cell shouldn't change output
        self.assertEqual(output.strip(), output2.strip())
 def test_javascript_output(self):
     nb = v4.new_notebook(
         cells=[
             v4.new_code_cell(
                 outputs=[v4.new_output(
                     output_type='display_data',
                     data={
                         'application/javascript': "javascript_output();"
                     }
                 )]
             )
         ]
     )
     (output, resources) = HTMLExporter(template_file='basic').from_notebook_node(nb)
     self.assertIn('javascript_output', output)
 def test_empty_code_cell(self):
     """No empty code cells in rst"""
     nbname = self._get_notebook()
     with io.open(nbname, encoding='utf8') as f:
         nb = nbformat.read(f, 4)
     
     exporter = self.exporter_class()
     
     (output, resources) = exporter.from_notebook_node(nb)
     # add an empty code cell
     nb.cells.append(
         v4.new_code_cell(source="")
     )
     (output2, resources) = exporter.from_notebook_node(nb)
     # adding an empty code cell shouldn't change output
     self.assertEqual(output.strip(), output2.strip())
Beispiel #8
0
    def build_notebook(self):
        """Build a notebook in memory for use with preprocessor tests"""

        outputs = [
            nbformat.new_output("stream", name="stdout", text="a"),
            nbformat.new_output("display_data", data={'text/plain': 'b'}),
            nbformat.new_output("stream", name="stdout", text="c"),
            nbformat.new_output("stream", name="stdout", text="d"),
            nbformat.new_output("stream", name="stderr", text="e"),
            nbformat.new_output("stream", name="stderr", text="f"),
            nbformat.new_output("display_data", data={'image/png': 'Zw=='}), # g
            nbformat.new_output("display_data", data={'application/pdf': 'aA=='}), # h
        ]
        
        cells=[nbformat.new_code_cell(source="$ e $", execution_count=1, outputs=outputs),
               nbformat.new_markdown_cell(source="$ e $")]

        return nbformat.new_notebook(cells=cells)
Beispiel #9
0
    def test_contents_manager(self):
        "make sure ContentsManager returns right files (ipynb, bin, txt)."

        nbdir = self.notebook_dir.name
        base = self.base_url()

        nb = new_notebook(
            cells=[
                new_markdown_cell(u'Created by test ³'),
                new_code_cell("print(2*6)", outputs=[
                    new_output("stream", text="12"),
                ])
            ]
        )

        with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', 
            encoding='utf-8') as f:
            write(nb, f, version=4)

        with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f:
            f.write(b'\xff' + os.urandom(5))
            f.close()

        with io.open(pjoin(nbdir, 'test.txt'), 'w') as f:
            f.write(u'foobar')
            f.close()

        r = requests.get(url_path_join(base, 'files', 'testnb.ipynb'))
        self.assertEqual(r.status_code, 200)
        self.assertIn('print(2*6)', r.text)
        json.loads(r.text)

        r = requests.get(url_path_join(base, 'files', 'test.bin'))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers['content-type'], 'application/octet-stream')
        self.assertEqual(r.content[:1], b'\xff')
        self.assertEqual(len(r.content), 6)

        r = requests.get(url_path_join(base, 'files', 'test.txt'))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers['content-type'], 'text/plain')
        self.assertEqual(r.text, 'foobar')
Beispiel #10
0
    def test_contents_manager(self):
        "make sure ContentsManager returns right files (ipynb, bin, txt)."

        nbdir = self.notebook_dir.name
        base = self.base_url()

        nb = new_notebook(cells=[
            new_markdown_cell(u'Created by test ³'),
            new_code_cell("print(2*6)",
                          outputs=[
                              new_output("stream", text="12"),
                          ])
        ])

        with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', encoding='utf-8') as f:
            write(nb, f, version=4)

        with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f:
            f.write(b'\xff' + os.urandom(5))
            f.close()

        with io.open(pjoin(nbdir, 'test.txt'), 'w') as f:
            f.write(u'foobar')
            f.close()

        r = requests.get(url_path_join(base, 'files', 'testnb.ipynb'))
        self.assertEqual(r.status_code, 200)
        self.assertIn('print(2*6)', r.text)
        json.loads(r.text)

        r = requests.get(url_path_join(base, 'files', 'test.bin'))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers['content-type'], 'application/octet-stream')
        self.assertEqual(r.content[:1], b'\xff')
        self.assertEqual(len(r.content), 6)

        r = requests.get(url_path_join(base, 'files', 'test.txt'))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers['content-type'], 'text/plain')
        self.assertEqual(r.text, 'foobar')
    def test_coalesce_sequenced_streams(self):
        """Can the coalesce streams preprocessor merge a sequence of streams?"""
        outputs = [
            nbformat.new_output(output_type="stream", name="stdout", text="0"),
            nbformat.new_output(output_type="stream", name="stdout", text="1"),
            nbformat.new_output(output_type="stream", name="stdout", text="2"),
            nbformat.new_output(output_type="stream", name="stdout", text="3"),
            nbformat.new_output(output_type="stream", name="stdout", text="4"),
            nbformat.new_output(output_type="stream", name="stdout", text="5"),
            nbformat.new_output(output_type="stream", name="stdout", text="6"),
            nbformat.new_output(output_type="stream", name="stdout", text="7")
        ]
        cells = [
            nbformat.new_code_cell(source="# None",
                                   execution_count=1,
                                   outputs=outputs)
        ]

        nb = nbformat.new_notebook(cells=cells)
        res = self.build_resources()
        nb, res = coalesce_streams(nb, res)
        outputs = nb.cells[0].outputs
        self.assertEqual(outputs[0].text, u'01234567')
Beispiel #12
0
    def notebook(self, s):
        """Export and convert IPython notebooks.

        This function can export the current IPython history to a notebook file.
        For example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
        To export the history to "foo.py" do "%notebook -e foo.py".
        """
        args = magic_arguments.parse_argstring(self.notebook, s)

        from jupyter_nbformat import write, v4
        args.filename = unquote_filename(args.filename)
        if args.export:
            cells = []
            hist = list(self.shell.history_manager.get_range())
            if(len(hist)<=1):
                raise ValueError('History is empty, cannot export')
            for session, execution_count, source in hist[:-1]:
                cells.append(v4.new_code_cell(
                    execution_count=execution_count,
                    source=source
                ))
            nb = v4.new_notebook(cells=cells)
            with io.open(args.filename, 'w', encoding='utf-8') as f:
                write(nb, f, version=4)
Beispiel #13
0
 def add_code_cell(self, nb):
     output = nbformat.new_output("display_data", {'application/javascript': "alert('hi');"})
     cell = nbformat.new_code_cell("print('hi')", outputs=[output])
     nb.cells.append(cell)
Beispiel #14
0
 def add_code_cell(self, nb):
     output = nbformat.new_output(
         "display_data", {'application/javascript': "alert('hi');"})
     cell = nbformat.new_code_cell("print('hi')", outputs=[output])
     nb.cells.append(cell)