##// END OF EJS Templates
Merge pull request #6045 from minrk/nbformat4...
Merge pull request #6045 from minrk/nbformat4 nbformat v4

File last commit:

r18613:380a2e94
r18617:482c7bd6 merge
Show More
test_nbconvert_handlers.py
132 lines | 4.8 KiB | text/x-python | PythonLexer
/ IPython / html / nbconvert / tests / test_nbconvert_handlers.py
Thomas Kluyver
Separate listing nbconvert exporters to /api/nbconvert
r13837 # coding: utf-8
Thomas Kluyver
Extract output preprocessor only extracts specified formats
r13921 import base64
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 import io
import json
import os
from os.path import join as pjoin
import shutil
import requests
from IPython.html.utils import url_path_join
from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
MinRK
don't use nbformat.current in IPython.html...
r18607 from IPython.nbformat import write
from IPython.nbformat.v4 import (
new_notebook, new_markdown_cell, new_code_cell, new_output,
)
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828
MinRK
skip nbconvert html tests without pandoc
r13947 from IPython.testing.decorators import onlyif_cmds_exist
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 class NbconvertAPI(object):
"""Wrapper for nbconvert API calls."""
def __init__(self, base_url):
self.base_url = base_url
Thomas Kluyver
Add menu entries for getting converted views of a notebook
r13829 def _req(self, verb, path, body=None, params=None):
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 response = requests.request(verb,
url_path_join(self.base_url, 'nbconvert', path),
Thomas Kluyver
Add menu entries for getting converted views of a notebook
r13829 data=body, params=params,
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 )
response.raise_for_status()
return response
Thomas Kluyver
Add menu entries for getting converted views of a notebook
r13829 def from_file(self, format, path, name, download=False):
return self._req('GET', url_path_join(format, path, name),
params={'download':download})
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828
def from_post(self, format, nbmodel):
body = json.dumps(nbmodel)
return self._req('POST', format, body)
Thomas Kluyver
Add GET /nbconvert to list available formats
r13835 def list_formats(self):
return self._req('GET', '')
Thomas Kluyver
Extract output preprocessor only extracts specified formats
r13921 png_green_pixel = base64.encodestring(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00'
b'\x00\x00\x01\x00\x00x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDAT'
MinRK
update html/js to nbformat 4
r18584 b'\x08\xd7c\x90\xfb\xcf\x00\x00\x02\\\x01\x1e.~d\x87\x00\x00\x00\x00IEND\xaeB`\x82'
).decode('ascii')
Thomas Kluyver
Extract output preprocessor only extracts specified formats
r13921
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 class APITest(NotebookTestBase):
def setUp(self):
nbdir = self.notebook_dir.name
if not os.path.isdir(pjoin(nbdir, 'foo')):
os.mkdir(pjoin(nbdir, 'foo'))
MinRK
update html/js to nbformat 4
r18584 nb = new_notebook()
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828
MinRK
remove heading cells in v4
r18596 nb.cells.append(new_markdown_cell(u'Created by test ³'))
MinRK
update html/js to nbformat 4
r18584 cc1 = new_code_cell(source=u'print(2*6)')
MinRK
msgspec: stream.data -> stream.text
r18588 cc1.outputs.append(new_output(output_type="stream", text=u'12'))
MinRK
update html/js to nbformat 4
r18584 cc1.outputs.append(new_output(output_type="execute_result",
MinRK
move mime-bundle data to rich output.data...
r18589 data={'image/png' : png_green_pixel},
MinRK
s/prompt_number/execution_count in nbformat 4
r18587 execution_count=1,
MinRK
update html/js to nbformat 4
r18584 ))
nb.cells.append(cc1)
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828
with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w',
encoding='utf-8') as f:
Min RK
fix backward `f, nb` args for nbformat.write
r18613 write(nb, f, version=4)
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828
self.nbconvert_api = NbconvertAPI(self.base_url())
def tearDown(self):
nbdir = self.notebook_dir.name
for dname in ['foo']:
shutil.rmtree(pjoin(nbdir, dname), ignore_errors=True)
MinRK
skip nbconvert html tests without pandoc
r13947 @onlyif_cmds_exist('pandoc')
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 def test_from_file(self):
r = self.nbconvert_api.from_file('html', 'foo', 'testnb.ipynb')
self.assertEqual(r.status_code, 200)
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830 self.assertIn(u'text/html', r.headers['Content-Type'])
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 self.assertIn(u'Created by test', r.text)
self.assertIn(u'print', r.text)
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb')
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830 self.assertIn(u'text/x-python', r.headers['Content-Type'])
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 self.assertIn(u'print(2*6)', r.text)
MinRK
skip nbconvert html tests without pandoc
r13947 @onlyif_cmds_exist('pandoc')
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 def test_from_file_404(self):
with assert_http_error(404):
self.nbconvert_api.from_file('html', 'foo', 'thisdoesntexist.ipynb')
MinRK
skip nbconvert html tests without pandoc
r13947 @onlyif_cmds_exist('pandoc')
Thomas Kluyver
Add menu entries for getting converted views of a notebook
r13829 def test_from_file_download(self):
r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb', download=True)
content_disposition = r.headers['Content-Disposition']
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830 self.assertIn('attachment', content_disposition)
self.assertIn('testnb.py', content_disposition)
Thomas Kluyver
Add menu entries for getting converted views of a notebook
r13829
MinRK
skip nbconvert html tests without pandoc
r13947 @onlyif_cmds_exist('pandoc')
Thomas Kluyver
Serve nbconvert output as zip when it has multiple files
r13919 def test_from_file_zip(self):
r = self.nbconvert_api.from_file('latex', 'foo', 'testnb.ipynb', download=True)
self.assertIn(u'application/zip', r.headers['Content-Type'])
self.assertIn(u'.zip', r.headers['Content-Disposition'])
MinRK
skip nbconvert html tests without pandoc
r13947 @onlyif_cmds_exist('pandoc')
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 def test_from_post(self):
MinRK
rename notebooks service to contents service...
r17524 nbmodel_url = url_path_join(self.base_url(), 'api/contents/foo/testnb.ipynb')
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 nbmodel = requests.get(nbmodel_url).json()
r = self.nbconvert_api.from_post(format='html', nbmodel=nbmodel)
self.assertEqual(r.status_code, 200)
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830 self.assertIn(u'text/html', r.headers['Content-Type'])
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 self.assertIn(u'Created by test', r.text)
self.assertIn(u'print', r.text)
r = self.nbconvert_api.from_post(format='python', nbmodel=nbmodel)
Thomas Kluyver
Add MIME types to nbconvert exporters
r13830 self.assertIn(u'text/x-python', r.headers['Content-Type'])
Thomas Kluyver
Add GET /nbconvert to list available formats
r13835 self.assertIn(u'print(2*6)', r.text)
Thomas Kluyver
Serve nbconvert output as zip when it has multiple files
r13919
MinRK
skip nbconvert html tests without pandoc
r13947 @onlyif_cmds_exist('pandoc')
Thomas Kluyver
Serve nbconvert output as zip when it has multiple files
r13919 def test_from_post_zip(self):
MinRK
rename notebooks service to contents service...
r17524 nbmodel_url = url_path_join(self.base_url(), 'api/contents/foo/testnb.ipynb')
Thomas Kluyver
Serve nbconvert output as zip when it has multiple files
r13919 nbmodel = requests.get(nbmodel_url).json()
r = self.nbconvert_api.from_post(format='latex', nbmodel=nbmodel)
self.assertIn(u'application/zip', r.headers['Content-Type'])
self.assertIn(u'.zip', r.headers['Content-Disposition'])