Show More
@@ -13,7 +13,7 from ..base.handlers import ( | |||||
13 | IPythonHandler, FilesRedirectHandler, |
|
13 | IPythonHandler, FilesRedirectHandler, | |
14 | notebook_path_regex, path_regex, |
|
14 | notebook_path_regex, path_regex, | |
15 | ) |
|
15 | ) | |
16 |
from IPython.nbformat |
|
16 | from IPython.nbformat import from_dict | |
17 |
|
17 | |||
18 | from IPython.utils.py3compat import cast_bytes |
|
18 | from IPython.utils.py3compat import cast_bytes | |
19 |
|
19 |
@@ -10,9 +10,10 import requests | |||||
10 |
|
10 | |||
11 | from IPython.html.utils import url_path_join |
|
11 | from IPython.html.utils import url_path_join | |
12 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error |
|
12 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error | |
13 |
from IPython.nbformat |
|
13 | from IPython.nbformat import write | |
14 | new_markdown_cell, new_code_cell, |
|
14 | from IPython.nbformat.v4 import ( | |
15 | new_output) |
|
15 | new_notebook, new_markdown_cell, new_code_cell, new_output, | |
|
16 | ) | |||
16 |
|
17 | |||
17 | from IPython.testing.decorators import onlyif_cmds_exist |
|
18 | from IPython.testing.decorators import onlyif_cmds_exist | |
18 |
|
19 | |||
@@ -66,7 +67,7 class APITest(NotebookTestBase): | |||||
66 |
|
67 | |||
67 | with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w', |
|
68 | with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w', | |
68 | encoding='utf-8') as f: |
|
69 | encoding='utf-8') as f: | |
69 |
write(nb, |
|
70 | write(f, nb, version=4) | |
70 |
|
71 | |||
71 | self.nbconvert_api = NbconvertAPI(self.base_url()) |
|
72 | self.nbconvert_api = NbconvertAPI(self.base_url()) | |
72 |
|
73 |
@@ -12,7 +12,7 import shutil | |||||
12 | from tornado import web |
|
12 | from tornado import web | |
13 |
|
13 | |||
14 | from .manager import ContentsManager |
|
14 | from .manager import ContentsManager | |
15 |
from IPython |
|
15 | from IPython import nbformat | |
16 | from IPython.utils.io import atomic_writing |
|
16 | from IPython.utils.io import atomic_writing | |
17 | from IPython.utils.path import ensure_dir_exists |
|
17 | from IPython.utils.path import ensure_dir_exists | |
18 | from IPython.utils.traitlets import Unicode, Bool, TraitError |
|
18 | from IPython.utils.traitlets import Unicode, Bool, TraitError | |
@@ -253,7 +253,7 class FileContentsManager(ContentsManager): | |||||
253 | os_path = self._get_os_path(name, path) |
|
253 | os_path = self._get_os_path(name, path) | |
254 | with io.open(os_path, 'r', encoding='utf-8') as f: |
|
254 | with io.open(os_path, 'r', encoding='utf-8') as f: | |
255 | try: |
|
255 | try: | |
256 |
nb = |
|
256 | nb = nbformat.read(f, as_version=4) | |
257 | except Exception as e: |
|
257 | except Exception as e: | |
258 | raise web.HTTPError(400, u"Unreadable Notebook: %s %r" % (os_path, e)) |
|
258 | raise web.HTTPError(400, u"Unreadable Notebook: %s %r" % (os_path, e)) | |
259 | self.mark_trusted_cells(nb, name, path) |
|
259 | self.mark_trusted_cells(nb, name, path) | |
@@ -295,7 +295,7 class FileContentsManager(ContentsManager): | |||||
295 | def _save_notebook(self, os_path, model, name='', path=''): |
|
295 | def _save_notebook(self, os_path, model, name='', path=''): | |
296 | """save a notebook file""" |
|
296 | """save a notebook file""" | |
297 | # Save the notebook file |
|
297 | # Save the notebook file | |
298 |
nb = |
|
298 | nb = nbformat.from_dict(model['content']) | |
299 |
|
299 | |||
300 | self.check_and_sign(nb, name, path) |
|
300 | self.check_and_sign(nb, name, path) | |
301 |
|
301 | |||
@@ -303,7 +303,7 class FileContentsManager(ContentsManager): | |||||
303 | nb['metadata']['name'] = u'' |
|
303 | nb['metadata']['name'] = u'' | |
304 |
|
304 | |||
305 | with atomic_writing(os_path, encoding='utf-8') as f: |
|
305 | with atomic_writing(os_path, encoding='utf-8') as f: | |
306 |
|
|
306 | nbformat.write(f, nb, version=nbformat.NO_CONVERT) | |
307 |
|
307 | |||
308 | def _save_file(self, os_path, model, name='', path=''): |
|
308 | def _save_file(self, os_path, model, name='', path=''): | |
309 | """save a non-notebook file""" |
|
309 | """save a non-notebook file""" | |
@@ -522,7 +522,7 class FileContentsManager(ContentsManager): | |||||
522 | # ensure notebook is readable (never restore from an unreadable notebook) |
|
522 | # ensure notebook is readable (never restore from an unreadable notebook) | |
523 | if cp_path.endswith('.ipynb'): |
|
523 | if cp_path.endswith('.ipynb'): | |
524 | with io.open(cp_path, 'r', encoding='utf-8') as f: |
|
524 | with io.open(cp_path, 'r', encoding='utf-8') as f: | |
525 |
|
|
525 | nbformat.read(f, as_version=4) | |
526 | self._copy(cp_path, nb_path) |
|
526 | self._copy(cp_path, nb_path) | |
527 | self.log.debug("copying %s -> %s", cp_path, nb_path) |
|
527 | self.log.debug("copying %s -> %s", cp_path, nb_path) | |
528 |
|
528 |
@@ -11,7 +11,8 import os | |||||
11 | from tornado.web import HTTPError |
|
11 | from tornado.web import HTTPError | |
12 |
|
12 | |||
13 | from IPython.config.configurable import LoggingConfigurable |
|
13 | from IPython.config.configurable import LoggingConfigurable | |
14 |
from IPython.nbformat import |
|
14 | from IPython.nbformat import sign, validate, ValidationError | |
|
15 | from IPython.nbformat.v4 import new_notebook | |||
15 | from IPython.utils.traitlets import Instance, Unicode, List |
|
16 | from IPython.utils.traitlets import Instance, Unicode, List | |
16 |
|
17 | |||
17 |
|
18 | |||
@@ -220,8 +221,8 class ContentsManager(LoggingConfigurable): | |||||
220 | def validate_notebook_model(self, model): |
|
221 | def validate_notebook_model(self, model): | |
221 | """Add failed-validation message to model""" |
|
222 | """Add failed-validation message to model""" | |
222 | try: |
|
223 | try: | |
223 |
|
|
224 | validate(model['content']) | |
224 |
except |
|
225 | except ValidationError as e: | |
225 | model['message'] = 'Notebook Validation failed: {}:\n{}'.format( |
|
226 | model['message'] = 'Notebook Validation failed: {}:\n{}'.format( | |
226 | e.message, json.dumps(e.instance, indent=1, default=lambda obj: '<UNKNOWN>'), |
|
227 | e.message, json.dumps(e.instance, indent=1, default=lambda obj: '<UNKNOWN>'), | |
227 | ) |
|
228 | ) | |
@@ -234,7 +235,7 class ContentsManager(LoggingConfigurable): | |||||
234 | model = {} |
|
235 | model = {} | |
235 | if 'content' not in model and model.get('type', None) != 'directory': |
|
236 | if 'content' not in model and model.get('type', None) != 'directory': | |
236 | if ext == '.ipynb': |
|
237 | if ext == '.ipynb': | |
237 |
model['content'] = |
|
238 | model['content'] = new_notebook() | |
238 | model['type'] = 'notebook' |
|
239 | model['type'] = 'notebook' | |
239 | model['format'] = 'json' |
|
240 | model['format'] = 'json' | |
240 | else: |
|
241 | else: | |
@@ -308,13 +309,14 class ContentsManager(LoggingConfigurable): | |||||
308 | Parameters |
|
309 | Parameters | |
309 | ---------- |
|
310 | ---------- | |
310 | nb : dict |
|
311 | nb : dict | |
311 |
The notebook object (in |
|
312 | The notebook object (in current nbformat) | |
312 | name : string |
|
313 | name : string | |
313 | The filename of the notebook (for logging) |
|
314 | The filename of the notebook (for logging) | |
314 | path : string |
|
315 | path : string | |
315 | The notebook's directory (for logging) |
|
316 | The notebook's directory (for logging) | |
316 | """ |
|
317 | """ | |
317 | if nb['nbformat'] != current.nbformat: |
|
318 | # can't sign old notebooks | |
|
319 | if nb['nbformat'] != 4: | |||
318 | return |
|
320 | return | |
319 | if self.notary.check_cells(nb): |
|
321 | if self.notary.check_cells(nb): | |
320 | self.notary.sign(nb) |
|
322 | self.notary.sign(nb) | |
@@ -329,7 +331,7 class ContentsManager(LoggingConfigurable): | |||||
329 | Parameters |
|
331 | Parameters | |
330 | ---------- |
|
332 | ---------- | |
331 | nb : dict |
|
333 | nb : dict | |
332 |
The notebook object (in |
|
334 | The notebook object (in current nbformat) | |
333 | name : string |
|
335 | name : string | |
334 | The filename of the notebook (for logging) |
|
336 | The filename of the notebook (for logging) | |
335 | path : string |
|
337 | path : string |
@@ -14,9 +14,10 import requests | |||||
14 |
|
14 | |||
15 | from IPython.html.utils import url_path_join, url_escape |
|
15 | from IPython.html.utils import url_path_join, url_escape | |
16 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error |
|
16 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error | |
17 |
from IPython.nbformat import |
|
17 | from IPython.nbformat import read, write, from_dict | |
18 |
from IPython.nbformat. |
|
18 | from IPython.nbformat.v4 import ( | |
19 | new_markdown_cell, from_dict) |
|
19 | new_notebook, new_markdown_cell, | |
|
20 | ) | |||
20 | from IPython.nbformat import v2 |
|
21 | from IPython.nbformat import v2 | |
21 | from IPython.utils import py3compat |
|
22 | from IPython.utils import py3compat | |
22 | from IPython.utils.data import uniq_stable |
|
23 | from IPython.utils.data import uniq_stable | |
@@ -143,7 +144,7 class APITest(NotebookTestBase): | |||||
143 | with io.open(pjoin(nbdir, d, '%s.ipynb' % name), 'w', |
|
144 | with io.open(pjoin(nbdir, d, '%s.ipynb' % name), 'w', | |
144 | encoding='utf-8') as f: |
|
145 | encoding='utf-8') as f: | |
145 | nb = new_notebook() |
|
146 | nb = new_notebook() | |
146 |
write(nb, |
|
147 | write(f, nb, version=4) | |
147 |
|
148 | |||
148 | # create a text file |
|
149 | # create a text file | |
149 | with io.open(pjoin(nbdir, d, '%s.txt' % name), 'w', |
|
150 | with io.open(pjoin(nbdir, d, '%s.txt' % name), 'w', | |
@@ -354,7 +355,7 class APITest(NotebookTestBase): | |||||
354 | self._check_created(resp, u'Upload tést.ipynb', u'å b') |
|
355 | self._check_created(resp, u'Upload tést.ipynb', u'å b') | |
355 | resp = self.api.read(u'Upload tést.ipynb', u'å b') |
|
356 | resp = self.api.read(u'Upload tést.ipynb', u'å b') | |
356 | data = resp.json() |
|
357 | data = resp.json() | |
357 |
self.assertEqual(data['content']['nbformat'], |
|
358 | self.assertEqual(data['content']['nbformat'], 4) | |
358 |
|
359 | |||
359 | def test_copy_untitled(self): |
|
360 | def test_copy_untitled(self): | |
360 | resp = self.api.copy_untitled(u'ç d.ipynb', path=u'å b') |
|
361 | resp = self.api.copy_untitled(u'ç d.ipynb', path=u'å b') | |
@@ -422,7 +423,7 class APITest(NotebookTestBase): | |||||
422 |
|
423 | |||
423 | nbfile = pjoin(self.notebook_dir.name, 'foo', 'a.ipynb') |
|
424 | nbfile = pjoin(self.notebook_dir.name, 'foo', 'a.ipynb') | |
424 | with io.open(nbfile, 'r', encoding='utf-8') as f: |
|
425 | with io.open(nbfile, 'r', encoding='utf-8') as f: | |
425 |
newnb = read(f, |
|
426 | newnb = read(f, as_version=4) | |
426 | self.assertEqual(newnb.cells[0].source, |
|
427 | self.assertEqual(newnb.cells[0].source, | |
427 | u'Created by test ³') |
|
428 | u'Created by test ³') | |
428 | nbcontent = self.api.read('a.ipynb', 'foo').json()['content'] |
|
429 | nbcontent = self.api.read('a.ipynb', 'foo').json()['content'] |
@@ -9,7 +9,7 from tornado.web import HTTPError | |||||
9 | from unittest import TestCase |
|
9 | from unittest import TestCase | |
10 | from tempfile import NamedTemporaryFile |
|
10 | from tempfile import NamedTemporaryFile | |
11 |
|
11 | |||
12 |
from IPython.nbformat import |
|
12 | from IPython.nbformat import v4 as nbformat | |
13 |
|
13 | |||
14 | from IPython.utils.tempdir import TemporaryDirectory |
|
14 | from IPython.utils.tempdir import TemporaryDirectory | |
15 | from IPython.utils.traitlets import TraitError |
|
15 | from IPython.utils.traitlets import TraitError | |
@@ -95,8 +95,8 class TestContentsManager(TestCase): | |||||
95 | return os_path |
|
95 | return os_path | |
96 |
|
96 | |||
97 | def add_code_cell(self, nb): |
|
97 | def add_code_cell(self, nb): | |
98 |
output = |
|
98 | output = nbformat.new_output("display_data", {'application/javascript': "alert('hi');"}) | |
99 |
cell = |
|
99 | cell = nbformat.new_code_cell("print('hi')", outputs=[output]) | |
100 | nb.cells.append(cell) |
|
100 | nb.cells.append(cell) | |
101 |
|
101 | |||
102 | def new_notebook(self): |
|
102 | def new_notebook(self): |
@@ -11,7 +11,8 pjoin = os.path.join | |||||
11 |
|
11 | |||
12 | from IPython.html.utils import url_path_join |
|
12 | from IPython.html.utils import url_path_join | |
13 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error |
|
13 | from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error | |
14 |
from IPython.nbformat. |
|
14 | from IPython.nbformat.v4 import new_notebook | |
|
15 | from IPython.nbformat import write | |||
15 |
|
16 | |||
16 | class SessionAPI(object): |
|
17 | class SessionAPI(object): | |
17 | """Wrapper for notebook API calls.""" |
|
18 | """Wrapper for notebook API calls.""" | |
@@ -63,7 +64,7 class SessionAPITest(NotebookTestBase): | |||||
63 | with io.open(pjoin(nbdir, 'foo', 'nb1.ipynb'), 'w', |
|
64 | with io.open(pjoin(nbdir, 'foo', 'nb1.ipynb'), 'w', | |
64 | encoding='utf-8') as f: |
|
65 | encoding='utf-8') as f: | |
65 | nb = new_notebook() |
|
66 | nb = new_notebook() | |
66 |
write(nb, |
|
67 | write(f, nb, version=4) | |
67 |
|
68 | |||
68 | self.sess_api = SessionAPI(self.base_url()) |
|
69 | self.sess_api = SessionAPI(self.base_url()) | |
69 |
|
70 |
@@ -10,7 +10,8 pjoin = os.path.join | |||||
10 | import requests |
|
10 | import requests | |
11 | import json |
|
11 | import json | |
12 |
|
12 | |||
13 |
from IPython.nbformat |
|
13 | from IPython.nbformat import write | |
|
14 | from IPython.nbformat.v4 import (new_notebook, | |||
14 | new_markdown_cell, new_code_cell, |
|
15 | new_markdown_cell, new_code_cell, | |
15 | new_output) |
|
16 | new_output) | |
16 |
|
17 | |||
@@ -73,7 +74,7 class FilesTest(NotebookTestBase): | |||||
73 |
|
74 | |||
74 | with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', |
|
75 | with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', | |
75 | encoding='utf-8') as f: |
|
76 | encoding='utf-8') as f: | |
76 |
write(nb, |
|
77 | write(f, nb, version=4) | |
77 |
|
78 | |||
78 | with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f: |
|
79 | with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f: | |
79 | f.write(b'\xff' + os.urandom(5)) |
|
80 | f.write(b'\xff' + os.urandom(5)) |
General Comments 0
You need to be logged in to leave comments.
Login now