Exemplo n.º 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())
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
0
    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')
Exemplo n.º 5
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)
Exemplo n.º 6
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)
Exemplo n.º 7
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')
Exemplo n.º 8
0
    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')
Exemplo n.º 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')
Exemplo n.º 10
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)
Exemplo n.º 11
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)