##// END OF EJS Templates
Add option to download as reST
Add option to download as reST

File last commit:

r13830:8d4507b1
r13831:0f8dc9ca
Show More
test_nbconvert_api.py
93 lines | 3.4 KiB | text/x-python | PythonLexer
/ IPython / html / nbconvert / tests / test_nbconvert_api.py
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
from IPython.nbformat.current import (new_notebook, write, new_worksheet,
new_heading_cell, new_code_cell)
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)
class APITest(NotebookTestBase):
def setUp(self):
nbdir = self.notebook_dir.name
if not os.path.isdir(pjoin(nbdir, 'foo')):
os.mkdir(pjoin(nbdir, 'foo'))
nb = new_notebook(name='testnb')
ws = new_worksheet()
nb.worksheets = [ws]
ws.cells.append(new_heading_cell(u'Created by test ³'))
ws.cells.append(new_code_cell(input=u'print(2*6)'))
with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w',
encoding='utf-8') as f:
write(nb, f, format='ipynb')
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)
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)
def test_from_file_404(self):
with assert_http_error(404):
self.nbconvert_api.from_file('html', 'foo', 'thisdoesntexist.ipynb')
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
Thomas Kluyver
Add tests for nbconvert HTTP service
r13828 def test_from_post(self):
nbmodel_url = url_path_join(self.base_url(), 'api/notebooks/foo/testnb.ipynb')
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 tests for nbconvert HTTP service
r13828 self.assertIn(u'print(2*6)', r.text)