##// END OF EJS Templates
output[mime/type] -> output.data[mime/type] in javascript
output[mime/type] -> output.data[mime/type] in javascript

File last commit:

r18579:bd730f70
r18592:bbf8ee41
Show More
reader.py
95 lines | 2.1 KiB | text/x-python | PythonLexer
MinRK
update nbformat.current to v4
r18579 """API for reading notebooks of different versions"""
Jonathan Frederic
Notebook version conversions done right?
r12493
MinRK
update nbformat.current to v4
r18579 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Jonathan Frederic
Notebook version conversions done right?
r12493
import json
Thomas Kluyver
Fix new relative imports
r13402 from . import v1
from . import v2
from . import v3
MinRK
update nbformat.current to v4
r18579 from . import v4
Jonathan Frederic
Fixes (iptest), simplified v1, v2, .. , vX import code
r12494
versions = {
1: v1,
2: v2,
3: v3,
MinRK
update nbformat.current to v4
r18579 4: v4,
Jonathan Frederic
Fixes (iptest), simplified v1, v2, .. , vX import code
r12494 }
Jonathan Frederic
Notebook version conversions done right?
r12493
class NotJSONError(ValueError):
pass
def parse_json(s, **kwargs):
"""Parse a JSON string into a dict."""
try:
nb_dict = json.loads(s, **kwargs)
except ValueError:
Jonathan Frederic
Updated JSON error message to limit full error to 80 characters....
r12975 # Limit the error message to 80 characters. Display whatever JSON will fit.
raise NotJSONError(("Notebook does not appear to be JSON: %r" % s)[:77] + "...")
Jonathan Frederic
Notebook version conversions done right?
r12493 return nb_dict
# High level API
def get_version(nb):
"""Get the version of a notebook.
Parameters
----------
nb : dict
NotebookNode or dict containing notebook data.
Returns
-------
Tuple containing major (int) and minor (int) version numbers
"""
major = nb.get('nbformat', 1)
minor = nb.get('nbformat_minor', 0)
return (major, minor)
Jonathan Frederic
Some small fixes and changes of nb version conversion
r12755 def reads(s, **kwargs):
"""Read a notebook from a json string and return the
Jonathan Frederic
Notebook version conversions done right?
r12493 NotebookNode object.
This function properly reads notebooks of any version. No version
conversion is performed.
Parameters
----------
s : unicode
The raw unicode string to read the notebook from.
Returns
-------
nb : NotebookNode
The notebook that was read.
"""
MinRK
add missing NBFormatError import in nbformat.reader.reads...
r17636 from .current import NBFormatError
Jonathan Frederic
Notebook version conversions done right?
r12493 nb_dict = parse_json(s, **kwargs)
(major, minor) = get_version(nb_dict)
if major in versions:
Jonathan Frederic
Fixes (iptest), simplified v1, v2, .. , vX import code
r12494 return versions[major].to_notebook_json(nb_dict, minor=minor)
Jonathan Frederic
Notebook version conversions done right?
r12493 else:
raise NBFormatError('Unsupported nbformat version %s' % major)
def read(fp, **kwargs):
"""Read a notebook from a file and return the NotebookNode object.
This function properly reads notebooks of any version. No version
conversion is performed.
Parameters
----------
fp : file
Any file-like object with a read method.
Returns
-------
nb : NotebookNode
The notebook that was read.
"""
Jonathan Frederic
Some small fixes and changes of nb version conversion
r12755 return reads(fp.read(), **kwargs)