Uploaded Test files

This commit is contained in:
Batuhan Berk Başoğlu 2020-11-12 11:05:57 -05:00
parent f584ad9d97
commit 2e81cb7d99
16627 changed files with 2065359 additions and 102444 deletions

View file

@ -0,0 +1,23 @@
"""The main API for the v4 notebook format."""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
__all__ = ['nbformat', 'nbformat_minor', 'nbformat_schema', 'new_code_cell',
'new_markdown_cell', 'new_notebook', 'new_output', 'output_from_msg',
'reads', 'writes', 'to_notebook', 'downgrade', 'upgrade']
from .nbbase import (
nbformat, nbformat_minor, nbformat_schema,
new_code_cell, new_markdown_cell, new_raw_cell, new_notebook,
new_output, output_from_msg,
)
from .nbjson import reads, writes, to_notebook
reads_json = reads
writes_json = writes
to_notebook_json = to_notebook
from .convert import downgrade, upgrade

View file

@ -0,0 +1,257 @@
"""Code for converting notebooks to and from v3."""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import json
import re
from .nbbase import (
nbformat, nbformat_minor,
NotebookNode,
)
from nbformat import v3
from traitlets.log import get_logger
def _warn_if_invalid(nb, version):
"""Log validation errors, if there are any."""
from nbformat import validate, ValidationError
try:
validate(nb, version=version)
except ValidationError as e:
get_logger().error("Notebook JSON is not valid v%i: %s", version, e)
def upgrade(nb, from_version=3, from_minor=0):
"""Convert a notebook to v4.
Parameters
----------
nb : NotebookNode
The Python representation of the notebook to convert.
from_version : int
The original version of the notebook to convert.
from_minor : int
The original minor version of the notebook to convert (only relevant for v >= 3).
"""
if from_version == 3:
# Validate the notebook before conversion
_warn_if_invalid(nb, from_version)
# Mark the original nbformat so consumers know it has been converted
orig_nbformat = nb.pop('orig_nbformat', None)
orig_nbformat_minor = nb.pop('orig_nbformat_minor', None)
nb.metadata.orig_nbformat = orig_nbformat or 3
nb.metadata.orig_nbformat_minor = orig_nbformat_minor or 0
# Mark the new format
nb.nbformat = nbformat
nb.nbformat_minor = nbformat_minor
# remove worksheet(s)
nb['cells'] = cells = []
# In the unlikely event of multiple worksheets,
# they will be flattened
for ws in nb.pop('worksheets', []):
# upgrade each cell
for cell in ws['cells']:
cells.append(upgrade_cell(cell))
# upgrade metadata
nb.metadata.pop('name', '')
nb.metadata.pop('signature', '')
# Validate the converted notebook before returning it
_warn_if_invalid(nb, nbformat)
return nb
elif from_version == 4:
# nothing to do
if from_minor != nbformat_minor:
nb.metadata.orig_nbformat_minor = from_minor
nb.nbformat_minor = nbformat_minor
return nb
else:
raise ValueError('Cannot convert a notebook directly from v%s to v4. ' \
'Try using the nbformat.convert module.' % from_version)
def upgrade_cell(cell):
"""upgrade a cell from v3 to v4
heading cell:
- -> markdown heading
code cell:
- remove language metadata
- cell.input -> cell.source
- cell.prompt_number -> cell.execution_count
- update outputs
"""
cell.setdefault('metadata', NotebookNode())
if cell.cell_type == 'code':
cell.pop('language', '')
if 'collapsed' in cell:
cell.metadata['collapsed'] = cell.pop('collapsed')
cell.source = cell.pop('input', '')
cell.execution_count = cell.pop('prompt_number', None)
cell.outputs = upgrade_outputs(cell.outputs)
elif cell.cell_type == 'heading':
cell.cell_type = 'markdown'
level = cell.pop('level', 1)
cell.source = u'{hashes} {single_line}'.format(
hashes='#' * level,
single_line = ' '.join(cell.get('source', '').splitlines()),
)
elif cell.cell_type == 'html':
# Technically, this exists. It will never happen in practice.
cell.cell_type = 'markdown'
return cell
def downgrade_cell(cell):
"""downgrade a cell from v4 to v3
code cell:
- set cell.language
- cell.input <- cell.source
- cell.prompt_number <- cell.execution_count
- update outputs
markdown cell:
- single-line heading -> heading cell
"""
if cell.cell_type == 'code':
cell.language = 'python'
cell.input = cell.pop('source', '')
cell.prompt_number = cell.pop('execution_count', None)
cell.collapsed = cell.metadata.pop('collapsed', False)
cell.outputs = downgrade_outputs(cell.outputs)
elif cell.cell_type == 'markdown':
source = cell.get('source', '')
if '\n' not in source and source.startswith('#'):
prefix, text = re.match(r'(#+)\s*(.*)', source).groups()
cell.cell_type = 'heading'
cell.source = text
cell.level = len(prefix)
cell.pop('attachments', None)
return cell
_mime_map = {
"text" : "text/plain",
"html" : "text/html",
"svg" : "image/svg+xml",
"png" : "image/png",
"jpeg" : "image/jpeg",
"latex" : "text/latex",
"json" : "application/json",
"javascript" : "application/javascript",
};
def to_mime_key(d):
"""convert dict with v3 aliases to plain mime-type keys"""
for alias, mime in _mime_map.items():
if alias in d:
d[mime] = d.pop(alias)
return d
def from_mime_key(d):
"""convert dict with mime-type keys to v3 aliases"""
d2 = {}
for alias, mime in _mime_map.items():
if mime in d:
d2[alias] = d[mime]
return d2
def upgrade_output(output):
"""upgrade a single code cell output from v3 to v4
- pyout -> execute_result
- pyerr -> error
- output.type -> output.data.mime/type
- mime-type keys
- stream.stream -> stream.name
"""
if output['output_type'] in {'pyout', 'display_data'}:
output.setdefault('metadata', NotebookNode())
if output['output_type'] == 'pyout':
output['output_type'] = 'execute_result'
output['execution_count'] = output.pop('prompt_number', None)
# move output data into data sub-dict
data = {}
for key in list(output):
if key in {'output_type', 'execution_count', 'metadata'}:
continue
data[key] = output.pop(key)
to_mime_key(data)
output['data'] = data
to_mime_key(output.metadata)
if 'application/json' in data:
data['application/json'] = json.loads(data['application/json'])
# promote ascii bytes (from v2) to unicode
for key in ('image/png', 'image/jpeg'):
if key in data and isinstance(data[key], bytes):
data[key] = data[key].decode('ascii')
elif output['output_type'] == 'pyerr':
output['output_type'] = 'error'
elif output['output_type'] == 'stream':
output['name'] = output.pop('stream', 'stdout')
return output
def downgrade_output(output):
"""downgrade a single code cell output to v3 from v4
- pyout <- execute_result
- pyerr <- error
- output.data.mime/type -> output.type
- un-mime-type keys
- stream.stream <- stream.name
"""
if output['output_type'] in {'execute_result', 'display_data'}:
if output['output_type'] == 'execute_result':
output['output_type'] = 'pyout'
output['prompt_number'] = output.pop('execution_count', None)
# promote data dict to top-level output namespace
data = output.pop('data', {})
if 'application/json' in data:
data['application/json'] = json.dumps(data['application/json'])
data = from_mime_key(data)
output.update(data)
from_mime_key(output.get('metadata', {}))
elif output['output_type'] == 'error':
output['output_type'] = 'pyerr'
elif output['output_type'] == 'stream':
output['stream'] = output.pop('name')
return output
def upgrade_outputs(outputs):
"""upgrade outputs of a code cell from v3 to v4"""
return [upgrade_output(op) for op in outputs]
def downgrade_outputs(outputs):
"""downgrade outputs of a code cell to v3 from v4"""
return [downgrade_output(op) for op in outputs]
def downgrade(nb):
"""Convert a v4 notebook to v3.
Parameters
----------
nb : NotebookNode
The Python representation of the notebook to convert.
"""
if nb.nbformat != nbformat:
return nb
# Validate the notebook before conversion
_warn_if_invalid(nb, nbformat)
nb.nbformat = v3.nbformat
nb.nbformat_minor = v3.nbformat_minor
cells = [ downgrade_cell(cell) for cell in nb.pop('cells') ]
nb.worksheets = [v3.new_worksheet(cells=cells)]
nb.metadata.setdefault('name', '')
# Validate the converted notebook before returning it
_warn_if_invalid(nb, v3.nbformat)
nb.orig_nbformat = nb.metadata.pop('orig_nbformat', nbformat)
nb.orig_nbformat_minor = nb.metadata.pop('orig_nbformat_minor', nbformat_minor)
return nb

View file

@ -0,0 +1,161 @@
"""Python API for composing notebook elements
The Python representation of a notebook is a nested structure of
dictionary subclasses that support attribute access
(ipython_genutils.ipstruct.Struct). The functions in this module are merely
helpers to build the structs in the right form.
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from ..notebooknode import NotebookNode
# Change the nbformat_minor and nbformat_schema variables when incrementing the
# nbformat version
# current major version
nbformat = 4
# current minor version
nbformat_minor = 4
# schema files for (major, minor) version tuples. (None, None) means the current version
nbformat_schema = {
(None, None): 'nbformat.v4.schema.json',
(4, 0): 'nbformat.v4.0.schema.json',
(4, 1): 'nbformat.v4.1.schema.json',
(4, 2): 'nbformat.v4.2.schema.json',
(4, 3): 'nbformat.v4.3.schema.json',
(4, 4): 'nbformat.v4.4.schema.json'
}
def validate(node, ref=None):
"""validate a v4 node"""
from .. import validate
return validate(node, ref=ref, version=nbformat)
def new_output(output_type, data=None, **kwargs):
"""Create a new output, to go in the ``cell.outputs`` list of a code cell."""
output = NotebookNode(output_type=output_type)
# populate defaults:
if output_type == 'stream':
output.name = u'stdout'
output.text = u''
elif output_type == 'display_data':
output.metadata = NotebookNode()
output.data = NotebookNode()
elif output_type == 'execute_result':
output.metadata = NotebookNode()
output.data = NotebookNode()
output.execution_count = None
elif output_type == 'error':
output.ename = "NotImplementedError"
output.evalue = ""
output.traceback = []
# load from args:
output.update(kwargs)
if data is not None:
output.data = data
# validate
validate(output, output_type)
return output
def output_from_msg(msg):
"""Create a NotebookNode for an output from a kernel's IOPub message.
Returns
-------
NotebookNode: the output as a notebook node.
Raises
------
ValueError: if the message is not an output message.
"""
msg_type = msg['header']['msg_type']
content = msg['content']
if msg_type == 'execute_result':
return new_output(output_type=msg_type,
metadata=content['metadata'],
data=content['data'],
execution_count=content['execution_count'],
)
elif msg_type == 'stream':
return new_output(output_type=msg_type,
name=content['name'],
text=content['text'],
)
elif msg_type == 'display_data':
return new_output(output_type=msg_type,
metadata=content['metadata'],
data=content['data'],
)
elif msg_type == 'error':
return new_output(output_type=msg_type,
ename=content['ename'],
evalue=content['evalue'],
traceback=content['traceback'],
)
else:
raise ValueError("Unrecognized output msg type: %r" % msg_type)
def new_code_cell(source='', **kwargs):
"""Create a new code cell"""
cell = NotebookNode(
cell_type='code',
metadata=NotebookNode(),
execution_count=None,
source=source,
outputs=[],
)
cell.update(kwargs)
validate(cell, 'code_cell')
return cell
def new_markdown_cell(source='', **kwargs):
"""Create a new markdown cell"""
cell = NotebookNode(
cell_type='markdown',
source=source,
metadata=NotebookNode(),
)
cell.update(kwargs)
validate(cell, 'markdown_cell')
return cell
def new_raw_cell(source='', **kwargs):
"""Create a new raw cell"""
cell = NotebookNode(
cell_type='raw',
source=source,
metadata=NotebookNode(),
)
cell.update(kwargs)
validate(cell, 'raw_cell')
return cell
def new_notebook(**kwargs):
"""Create a new notebook"""
nb = NotebookNode(
nbformat=nbformat,
nbformat_minor=nbformat_minor,
metadata=NotebookNode(),
cells=[],
)
nb.update(kwargs)
validate(nb)
return nb

View file

@ -0,0 +1,380 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "IPython Notebook v4.0 JSON schema.",
"type": "object",
"additionalProperties": false,
"required": ["metadata", "nbformat_minor", "nbformat", "cells"],
"properties": {
"metadata": {
"description": "Notebook root-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"kernelspec": {
"description": "Kernel information.",
"type": "object",
"required": ["name", "display_name"],
"properties": {
"name": {
"description": "Name of the kernel specification.",
"type": "string"
},
"display_name": {
"description": "Name to display in UI.",
"type": "string"
}
}
},
"language_info": {
"description": "Kernel information.",
"type": "object",
"required": ["name"],
"properties": {
"name": {
"description": "The programming language which this kernel runs.",
"type": "string"
},
"codemirror_mode": {
"description": "The codemirror mode to use for code in this language.",
"oneOf": [
{"type": "string"},
{"type": "object"}
]
},
"file_extension": {
"description": "The file extension for files in this language.",
"type": "string"
},
"mimetype": {
"description": "The mimetype corresponding to files in this language.",
"type": "string"
},
"pygments_lexer": {
"description": "The pygments lexer to use for code in this language.",
"type": "string"
}
}
},
"orig_nbformat": {
"description": "Original notebook format (major number) before converting the notebook between versions. This should never be written to a file.",
"type": "integer",
"minimum": 1
}
}
},
"nbformat_minor": {
"description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
"type": "integer",
"minimum": 0
},
"nbformat": {
"description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
"type": "integer",
"minimum": 4,
"maximum": 4
},
"cells": {
"description": "Array of cells of the current notebook.",
"type": "array",
"items": {"$ref": "#/definitions/cell"}
}
},
"definitions": {
"cell": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/raw_cell"},
{"$ref": "#/definitions/markdown_cell"},
{"$ref": "#/definitions/code_cell"}
]
},
"raw_cell": {
"description": "Notebook raw nbconvert cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["raw"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"format": {
"description": "Raw cell metadata format for nbconvert.",
"type": "string"
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"markdown_cell": {
"description": "Notebook markdown cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["markdown"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
},
"additionalProperties": true
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"code_cell": {
"description": "Notebook code cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source", "outputs", "execution_count"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["code"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"collapsed": {
"description": "Whether the cell is collapsed/expanded.",
"type": "boolean"
},
"scrolled": {
"description": "Whether the cell's output is scrolled, unscrolled, or autoscrolled.",
"enum": [true, false, "auto"]
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"source": {"$ref": "#/definitions/misc/source"},
"outputs": {
"description": "Execution, display, or stream outputs.",
"type": "array",
"items": {"$ref": "#/definitions/output"}
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
}
}
},
"unrecognized_cell": {
"description": "Unrecognized cell from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["cell_type", "metadata"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"not" : {
"enum": ["markdown", "code", "raw"]
}
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
},
"additionalProperties": true
}
}
},
"output": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/execute_result"},
{"$ref": "#/definitions/display_data"},
{"$ref": "#/definitions/stream"},
{"$ref": "#/definitions/error"}
]
},
"execute_result": {
"description": "Result of executing a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata", "execution_count"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["execute_result"]
},
"execution_count": {
"description": "A result's prompt number.",
"type": ["integer", "null"],
"minimum": 0
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"display_data": {
"description": "Data displayed as a result of code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["display_data"]
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"stream": {
"description": "Stream output from a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "name", "text"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["stream"]
},
"name": {
"description": "The name of the stream (stdout, stderr).",
"type": "string"
},
"text": {
"description": "The stream's text output, represented as an array of strings.",
"$ref": "#/definitions/misc/multiline_string"
}
}
},
"error": {
"description": "Output of an error that occurred during code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "ename", "evalue", "traceback"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["error"]
},
"ename": {
"description": "The name of the error.",
"type": "string"
},
"evalue": {
"description": "The value, or message, of the error.",
"type": "string"
},
"traceback": {
"description": "The error's traceback, represented as an array of strings.",
"type": "array",
"items": {"type": "string"}
}
}
},
"unrecognized_output": {
"description": "Unrecognized output from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["output_type"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"not": {
"enum": ["execute_result", "display_data", "stream", "error"]
}
}
}
},
"misc": {
"metadata_name": {
"description": "The cell's name. If present, must be a non-empty string.",
"type": "string",
"pattern": "^.+$"
},
"metadata_tags": {
"description": "The cell's tags. Tags must be unique, and must not contain commas.",
"type": "array",
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "^[^,]+$"
}
},
"attachments": {
"description": "Media attachments (e.g. inline images), stored as mimebundle keyed by filename.",
"type": "object",
"patternProperties": {
".*": {
"description": "The attachment's data stored as a mimebundle.",
"$ref": "#/definitions/misc/mimebundle"
}
}
},
"source": {
"description": "Contents of the cell, represented as an array of lines.",
"$ref": "#/definitions/misc/multiline_string"
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
},
"mimebundle": {
"description": "A mime-type keyed dictionary of data",
"type": "object",
"additionalProperties": {
"description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
"$ref": "#/definitions/misc/multiline_string"
},
"patternProperties": {
"^application/(.*\\+)?json$": {
"description": "Mimetypes with JSON output, can be any type"
}
}
},
"output_metadata": {
"description": "Cell output metadata.",
"type": "object",
"additionalProperties": true
},
"multiline_string": {
"oneOf" : [
{"type": "string"},
{
"type": "array",
"items": {"type": "string"}
}
]
}
}
}
}

View file

@ -0,0 +1,380 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "IPython Notebook v4.1 JSON schema.",
"type": "object",
"additionalProperties": false,
"required": ["metadata", "nbformat_minor", "nbformat", "cells"],
"properties": {
"metadata": {
"description": "Notebook root-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"kernelspec": {
"description": "Kernel information.",
"type": "object",
"required": ["name", "display_name"],
"properties": {
"name": {
"description": "Name of the kernel specification.",
"type": "string"
},
"display_name": {
"description": "Name to display in UI.",
"type": "string"
}
}
},
"language_info": {
"description": "Kernel information.",
"type": "object",
"required": ["name"],
"properties": {
"name": {
"description": "The programming language which this kernel runs.",
"type": "string"
},
"codemirror_mode": {
"description": "The codemirror mode to use for code in this language.",
"oneOf": [
{"type": "string"},
{"type": "object"}
]
},
"file_extension": {
"description": "The file extension for files in this language.",
"type": "string"
},
"mimetype": {
"description": "The mimetype corresponding to files in this language.",
"type": "string"
},
"pygments_lexer": {
"description": "The pygments lexer to use for code in this language.",
"type": "string"
}
}
},
"orig_nbformat": {
"description": "Original notebook format (major number) before converting the notebook between versions. This should never be written to a file.",
"type": "integer",
"minimum": 1
}
}
},
"nbformat_minor": {
"description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
"type": "integer",
"minimum": 1
},
"nbformat": {
"description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
"type": "integer",
"minimum": 4,
"maximum": 4
},
"cells": {
"description": "Array of cells of the current notebook.",
"type": "array",
"items": {"$ref": "#/definitions/cell"}
}
},
"definitions": {
"cell": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/raw_cell"},
{"$ref": "#/definitions/markdown_cell"},
{"$ref": "#/definitions/code_cell"}
]
},
"raw_cell": {
"description": "Notebook raw nbconvert cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["raw"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"format": {
"description": "Raw cell metadata format for nbconvert.",
"type": "string"
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"markdown_cell": {
"description": "Notebook markdown cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["markdown"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
},
"additionalProperties": true
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"code_cell": {
"description": "Notebook code cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source", "outputs", "execution_count"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["code"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"collapsed": {
"description": "Whether the cell is collapsed/expanded.",
"type": "boolean"
},
"scrolled": {
"description": "Whether the cell's output is scrolled, unscrolled, or autoscrolled.",
"enum": [true, false, "auto"]
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"source": {"$ref": "#/definitions/misc/source"},
"outputs": {
"description": "Execution, display, or stream outputs.",
"type": "array",
"items": {"$ref": "#/definitions/output"}
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
}
}
},
"unrecognized_cell": {
"description": "Unrecognized cell from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["cell_type", "metadata"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"not" : {
"enum": ["markdown", "code", "raw"]
}
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
},
"additionalProperties": true
}
}
},
"output": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/execute_result"},
{"$ref": "#/definitions/display_data"},
{"$ref": "#/definitions/stream"},
{"$ref": "#/definitions/error"}
]
},
"execute_result": {
"description": "Result of executing a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata", "execution_count"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["execute_result"]
},
"execution_count": {
"description": "A result's prompt number.",
"type": ["integer", "null"],
"minimum": 0
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"display_data": {
"description": "Data displayed as a result of code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["display_data"]
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"stream": {
"description": "Stream output from a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "name", "text"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["stream"]
},
"name": {
"description": "The name of the stream (stdout, stderr).",
"type": "string"
},
"text": {
"description": "The stream's text output, represented as an array of strings.",
"$ref": "#/definitions/misc/multiline_string"
}
}
},
"error": {
"description": "Output of an error that occurred during code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "ename", "evalue", "traceback"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["error"]
},
"ename": {
"description": "The name of the error.",
"type": "string"
},
"evalue": {
"description": "The value, or message, of the error.",
"type": "string"
},
"traceback": {
"description": "The error's traceback, represented as an array of strings.",
"type": "array",
"items": {"type": "string"}
}
}
},
"unrecognized_output": {
"description": "Unrecognized output from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["output_type"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"not": {
"enum": ["execute_result", "display_data", "stream", "error"]
}
}
}
},
"misc": {
"metadata_name": {
"description": "The cell's name. If present, must be a non-empty string.",
"type": "string",
"pattern": "^.+$"
},
"metadata_tags": {
"description": "The cell's tags. Tags must be unique, and must not contain commas.",
"type": "array",
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "^[^,]+$"
}
},
"attachments": {
"description": "Media attachments (e.g. inline images), stored as mimebundle keyed by filename.",
"type": "object",
"patternProperties": {
".*": {
"description": "The attachment's data stored as a mimebundle.",
"$ref": "#/definitions/misc/mimebundle"
}
}
},
"source": {
"description": "Contents of the cell, represented as an array of lines.",
"$ref": "#/definitions/misc/multiline_string"
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
},
"mimebundle": {
"description": "A mime-type keyed dictionary of data",
"type": "object",
"additionalProperties": {
"description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
"$ref": "#/definitions/misc/multiline_string"
},
"patternProperties": {
"^application/(.*\\+)?json$": {
"description": "Mimetypes with JSON output, can be any type"
}
}
},
"output_metadata": {
"description": "Cell output metadata.",
"type": "object",
"additionalProperties": true
},
"multiline_string": {
"oneOf" : [
{"type": "string"},
{
"type": "array",
"items": {"type": "string"}
}
]
}
}
}
}

View file

@ -0,0 +1,397 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Jupyter Notebook v4.2 JSON schema.",
"type": "object",
"additionalProperties": false,
"required": ["metadata", "nbformat_minor", "nbformat", "cells"],
"properties": {
"metadata": {
"description": "Notebook root-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"kernelspec": {
"description": "Kernel information.",
"type": "object",
"required": ["name", "display_name"],
"properties": {
"name": {
"description": "Name of the kernel specification.",
"type": "string"
},
"display_name": {
"description": "Name to display in UI.",
"type": "string"
}
}
},
"language_info": {
"description": "Kernel information.",
"type": "object",
"required": ["name"],
"properties": {
"name": {
"description": "The programming language which this kernel runs.",
"type": "string"
},
"codemirror_mode": {
"description": "The codemirror mode to use for code in this language.",
"oneOf": [
{"type": "string"},
{"type": "object"}
]
},
"file_extension": {
"description": "The file extension for files in this language.",
"type": "string"
},
"mimetype": {
"description": "The mimetype corresponding to files in this language.",
"type": "string"
},
"pygments_lexer": {
"description": "The pygments lexer to use for code in this language.",
"type": "string"
}
}
},
"orig_nbformat": {
"description": "Original notebook format (major number) before converting the notebook between versions. This should never be written to a file.",
"type": "integer",
"minimum": 1
},
"title": {
"description": "The title of the notebook document",
"type": "string"
},
"authors": {
"description": "The author(s) of the notebook document",
"type": "array",
"item": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"additionalProperties": true
}
}
}
},
"nbformat_minor": {
"description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
"type": "integer",
"minimum": 2
},
"nbformat": {
"description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
"type": "integer",
"minimum": 4,
"maximum": 4
},
"cells": {
"description": "Array of cells of the current notebook.",
"type": "array",
"items": {"$ref": "#/definitions/cell"}
}
},
"definitions": {
"cell": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/raw_cell"},
{"$ref": "#/definitions/markdown_cell"},
{"$ref": "#/definitions/code_cell"}
]
},
"raw_cell": {
"description": "Notebook raw nbconvert cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["raw"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"format": {
"description": "Raw cell metadata format for nbconvert.",
"type": "string"
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"markdown_cell": {
"description": "Notebook markdown cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["markdown"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
},
"additionalProperties": true
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"code_cell": {
"description": "Notebook code cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source", "outputs", "execution_count"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["code"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"collapsed": {
"description": "Whether the cell is collapsed/expanded.",
"type": "boolean"
},
"scrolled": {
"description": "Whether the cell's output is scrolled, unscrolled, or autoscrolled.",
"enum": [true, false, "auto"]
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"source": {"$ref": "#/definitions/misc/source"},
"outputs": {
"description": "Execution, display, or stream outputs.",
"type": "array",
"items": {"$ref": "#/definitions/output"}
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
}
}
},
"unrecognized_cell": {
"description": "Unrecognized cell from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["cell_type", "metadata"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"not" : {
"enum": ["markdown", "code", "raw"]
}
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
},
"additionalProperties": true
}
}
},
"output": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/execute_result"},
{"$ref": "#/definitions/display_data"},
{"$ref": "#/definitions/stream"},
{"$ref": "#/definitions/error"}
]
},
"execute_result": {
"description": "Result of executing a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata", "execution_count"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["execute_result"]
},
"execution_count": {
"description": "A result's prompt number.",
"type": ["integer", "null"],
"minimum": 0
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"display_data": {
"description": "Data displayed as a result of code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["display_data"]
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"stream": {
"description": "Stream output from a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "name", "text"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["stream"]
},
"name": {
"description": "The name of the stream (stdout, stderr).",
"type": "string"
},
"text": {
"description": "The stream's text output, represented as an array of strings.",
"$ref": "#/definitions/misc/multiline_string"
}
}
},
"error": {
"description": "Output of an error that occurred during code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "ename", "evalue", "traceback"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["error"]
},
"ename": {
"description": "The name of the error.",
"type": "string"
},
"evalue": {
"description": "The value, or message, of the error.",
"type": "string"
},
"traceback": {
"description": "The error's traceback, represented as an array of strings.",
"type": "array",
"items": {"type": "string"}
}
}
},
"unrecognized_output": {
"description": "Unrecognized output from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["output_type"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"not": {
"enum": ["execute_result", "display_data", "stream", "error"]
}
}
}
},
"misc": {
"metadata_name": {
"description": "The cell's name. If present, must be a non-empty string. Must be unique across all the cells of a given notebook.",
"type": "string",
"pattern": "^.+$"
},
"metadata_tags": {
"description": "The cell's tags. Tags must be unique, and must not contain commas.",
"type": "array",
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "^[^,]+$"
}
},
"attachments": {
"description": "Media attachments (e.g. inline images), stored as mimebundle keyed by filename.",
"type": "object",
"patternProperties": {
".*": {
"description": "The attachment's data stored as a mimebundle.",
"$ref": "#/definitions/misc/mimebundle"
}
}
},
"source": {
"description": "Contents of the cell, represented as an array of lines.",
"$ref": "#/definitions/misc/multiline_string"
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
},
"mimebundle": {
"description": "A mime-type keyed dictionary of data",
"type": "object",
"additionalProperties": {
"description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
"$ref": "#/definitions/misc/multiline_string"
},
"patternProperties": {
"^application/(.*\\+)?json$": {
"description": "Mimetypes with JSON output, can be any type"
}
}
},
"output_metadata": {
"description": "Cell output metadata.",
"type": "object",
"additionalProperties": true
},
"multiline_string": {
"oneOf" : [
{"type": "string"},
{
"type": "array",
"items": {"type": "string"}
}
]
}
}
}
}

View file

@ -0,0 +1,428 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Jupyter Notebook v4.3 JSON schema.",
"type": "object",
"additionalProperties": false,
"required": ["metadata", "nbformat_minor", "nbformat", "cells"],
"properties": {
"metadata": {
"description": "Notebook root-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"kernelspec": {
"description": "Kernel information.",
"type": "object",
"required": ["name", "display_name"],
"properties": {
"name": {
"description": "Name of the kernel specification.",
"type": "string"
},
"display_name": {
"description": "Name to display in UI.",
"type": "string"
}
}
},
"language_info": {
"description": "Kernel information.",
"type": "object",
"required": ["name"],
"properties": {
"name": {
"description": "The programming language which this kernel runs.",
"type": "string"
},
"codemirror_mode": {
"description": "The codemirror mode to use for code in this language.",
"oneOf": [
{"type": "string"},
{"type": "object"}
]
},
"file_extension": {
"description": "The file extension for files in this language.",
"type": "string"
},
"mimetype": {
"description": "The mimetype corresponding to files in this language.",
"type": "string"
},
"pygments_lexer": {
"description": "The pygments lexer to use for code in this language.",
"type": "string"
}
}
},
"orig_nbformat": {
"description": "Original notebook format (major number) before converting the notebook between versions. This should never be written to a file.",
"type": "integer",
"minimum": 1
},
"title": {
"description": "The title of the notebook document",
"type": "string"
},
"authors": {
"description": "The author(s) of the notebook document",
"type": "array",
"item": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"additionalProperties": true
}
}
}
},
"nbformat_minor": {
"description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
"type": "integer",
"minimum": 3
},
"nbformat": {
"description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
"type": "integer",
"minimum": 4,
"maximum": 4
},
"cells": {
"description": "Array of cells of the current notebook.",
"type": "array",
"items": {"$ref": "#/definitions/cell"}
}
},
"definitions": {
"cell": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/raw_cell"},
{"$ref": "#/definitions/markdown_cell"},
{"$ref": "#/definitions/code_cell"}
]
},
"raw_cell": {
"description": "Notebook raw nbconvert cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["raw"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"format": {
"description": "Raw cell metadata format for nbconvert.",
"type": "string"
},
"jupyter": {
"description": "Official Jupyter Metadata for Raw Cells",
"type": "object",
"additionalProperties": true,
"source_hidden": {
"description": "Whether the source is hidden.",
"type": "boolean"
}
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"markdown_cell": {
"description": "Notebook markdown cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["markdown"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"},
"jupyter": {
"description": "Official Jupyter Metadata for Markdown Cells",
"type": "object",
"additionalProperties": true,
"source_hidden": {
"description": "Whether the source is hidden.",
"type": "boolean"
}
}
},
"additionalProperties": true
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"code_cell": {
"description": "Notebook code cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source", "outputs", "execution_count"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["code"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"jupyter": {
"description": "Official Jupyter Metadata for Code Cells",
"type": "object",
"additionalProperties": true,
"source_hidden": {
"description": "Whether the source is hidden.",
"type": "boolean"
},
"outputs_hidden": {
"description": "Whether the outputs are hidden.",
"type": "boolean"
}
},
"collapsed": {
"description": "Whether the cell is collapsed/expanded.",
"type": "boolean"
},
"scrolled": {
"description": "Whether the cell's output is scrolled, unscrolled, or autoscrolled.",
"enum": [true, false, "auto"]
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"source": {"$ref": "#/definitions/misc/source"},
"outputs": {
"description": "Execution, display, or stream outputs.",
"type": "array",
"items": {"$ref": "#/definitions/output"}
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
}
}
},
"unrecognized_cell": {
"description": "Unrecognized cell from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["cell_type", "metadata"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"not" : {
"enum": ["markdown", "code", "raw"]
}
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
},
"additionalProperties": true
}
}
},
"output": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/execute_result"},
{"$ref": "#/definitions/display_data"},
{"$ref": "#/definitions/stream"},
{"$ref": "#/definitions/error"}
]
},
"execute_result": {
"description": "Result of executing a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata", "execution_count"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["execute_result"]
},
"execution_count": {
"description": "A result's prompt number.",
"type": ["integer", "null"],
"minimum": 0
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"display_data": {
"description": "Data displayed as a result of code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["display_data"]
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"stream": {
"description": "Stream output from a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "name", "text"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["stream"]
},
"name": {
"description": "The name of the stream (stdout, stderr).",
"type": "string"
},
"text": {
"description": "The stream's text output, represented as an array of strings.",
"$ref": "#/definitions/misc/multiline_string"
}
}
},
"error": {
"description": "Output of an error that occurred during code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "ename", "evalue", "traceback"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["error"]
},
"ename": {
"description": "The name of the error.",
"type": "string"
},
"evalue": {
"description": "The value, or message, of the error.",
"type": "string"
},
"traceback": {
"description": "The error's traceback, represented as an array of strings.",
"type": "array",
"items": {"type": "string"}
}
}
},
"unrecognized_output": {
"description": "Unrecognized output from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["output_type"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"not": {
"enum": ["execute_result", "display_data", "stream", "error"]
}
}
}
},
"misc": {
"metadata_name": {
"description": "The cell's name. If present, must be a non-empty string. Must be unique across all the cells of a given notebook.",
"type": "string",
"pattern": "^.+$"
},
"metadata_tags": {
"description": "The cell's tags. Tags must be unique, and must not contain commas.",
"type": "array",
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "^[^,]+$"
}
},
"attachments": {
"description": "Media attachments (e.g. inline images), stored as mimebundle keyed by filename.",
"type": "object",
"patternProperties": {
".*": {
"description": "The attachment's data stored as a mimebundle.",
"$ref": "#/definitions/misc/mimebundle"
}
}
},
"source": {
"description": "Contents of the cell, represented as an array of lines.",
"$ref": "#/definitions/misc/multiline_string"
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
},
"mimebundle": {
"description": "A mime-type keyed dictionary of data",
"type": "object",
"additionalProperties": {
"description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
"$ref": "#/definitions/misc/multiline_string"
},
"patternProperties": {
"^application/(.*\\+)?json$": {
"description": "Mimetypes with JSON output, can be any type"
}
}
},
"output_metadata": {
"description": "Cell output metadata.",
"type": "object",
"additionalProperties": true
},
"multiline_string": {
"oneOf" : [
{"type": "string"},
{
"type": "array",
"items": {"type": "string"}
}
]
}
}
}
}

View file

@ -0,0 +1,456 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Jupyter Notebook v4.4 JSON schema.",
"type": "object",
"additionalProperties": false,
"required": ["metadata", "nbformat_minor", "nbformat", "cells"],
"properties": {
"metadata": {
"description": "Notebook root-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"kernelspec": {
"description": "Kernel information.",
"type": "object",
"required": ["name", "display_name"],
"properties": {
"name": {
"description": "Name of the kernel specification.",
"type": "string"
},
"display_name": {
"description": "Name to display in UI.",
"type": "string"
}
}
},
"language_info": {
"description": "Kernel information.",
"type": "object",
"required": ["name"],
"properties": {
"name": {
"description": "The programming language which this kernel runs.",
"type": "string"
},
"codemirror_mode": {
"description": "The codemirror mode to use for code in this language.",
"oneOf": [
{"type": "string"},
{"type": "object"}
]
},
"file_extension": {
"description": "The file extension for files in this language.",
"type": "string"
},
"mimetype": {
"description": "The mimetype corresponding to files in this language.",
"type": "string"
},
"pygments_lexer": {
"description": "The pygments lexer to use for code in this language.",
"type": "string"
}
}
},
"orig_nbformat": {
"description": "Original notebook format (major number) before converting the notebook between versions. This should never be written to a file.",
"type": "integer",
"minimum": 1
},
"title": {
"description": "The title of the notebook document",
"type": "string"
},
"authors": {
"description": "The author(s) of the notebook document",
"type": "array",
"item": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"additionalProperties": true
}
}
}
},
"nbformat_minor": {
"description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
"type": "integer",
"minimum": 4
},
"nbformat": {
"description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
"type": "integer",
"minimum": 4,
"maximum": 4
},
"cells": {
"description": "Array of cells of the current notebook.",
"type": "array",
"items": {"$ref": "#/definitions/cell"}
}
},
"definitions": {
"cell": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/raw_cell"},
{"$ref": "#/definitions/markdown_cell"},
{"$ref": "#/definitions/code_cell"}
]
},
"raw_cell": {
"description": "Notebook raw nbconvert cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["raw"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"format": {
"description": "Raw cell metadata format for nbconvert.",
"type": "string"
},
"jupyter": {
"description": "Official Jupyter Metadata for Raw Cells",
"type": "object",
"additionalProperties": true,
"source_hidden": {
"description": "Whether the source is hidden.",
"type": "boolean"
}
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"markdown_cell": {
"description": "Notebook markdown cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["markdown"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"},
"jupyter": {
"description": "Official Jupyter Metadata for Markdown Cells",
"type": "object",
"additionalProperties": true,
"source_hidden": {
"description": "Whether the source is hidden.",
"type": "boolean"
}
}
},
"additionalProperties": true
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"code_cell": {
"description": "Notebook code cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source", "outputs", "execution_count"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["code"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"jupyter": {
"description": "Official Jupyter Metadata for Code Cells",
"type": "object",
"additionalProperties": true,
"source_hidden": {
"description": "Whether the source is hidden.",
"type": "boolean"
},
"outputs_hidden": {
"description": "Whether the outputs are hidden.",
"type": "boolean"
}
},
"execution": {
"description": "Execution time for the code in the cell. This tracks time at which messages are received from iopub or shell channels",
"type": "object",
"properties": {
"iopub.execute_input": {
"description": "header.date (in ISO 8601 format) of iopub channel's execute_input message. It indicates the time at which the kernel broadcasts an execute_input message to connected frontends",
"type": "string"
},
"iopub.status.busy": {
"description": "header.date (in ISO 8601 format) of iopub channel's kernel status message when the status is 'busy'",
"type": "string"
},
"shell.execute_reply": {
"description": "header.date (in ISO 8601 format) of the shell channel's execute_reply message. It indicates the time at which the execute_reply message was created",
"type": "string"
},
"iopub.status.idle": {
"description": "header.date (in ISO 8601 format) of iopub channel's kernel status message when the status is 'idle'. It indicates the time at which kernel finished processing the associated request",
"type": "string"
}
},
"additionalProperties": true,
"patternProperties": {
"^.*$": {
"type": "string"
}
}
},
"collapsed": {
"description": "Whether the cell's output is collapsed/expanded.",
"type": "boolean"
},
"scrolled": {
"description": "Whether the cell's output is scrolled, unscrolled, or autoscrolled.",
"enum": [true, false, "auto"]
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"source": {"$ref": "#/definitions/misc/source"},
"outputs": {
"description": "Execution, display, or stream outputs.",
"type": "array",
"items": {"$ref": "#/definitions/output"}
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
}
}
},
"unrecognized_cell": {
"description": "Unrecognized cell from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["cell_type", "metadata"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"not" : {
"enum": ["markdown", "code", "raw"]
}
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
},
"additionalProperties": true
}
}
},
"output": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/execute_result"},
{"$ref": "#/definitions/display_data"},
{"$ref": "#/definitions/stream"},
{"$ref": "#/definitions/error"}
]
},
"execute_result": {
"description": "Result of executing a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata", "execution_count"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["execute_result"]
},
"execution_count": {
"description": "A result's prompt number.",
"type": ["integer", "null"],
"minimum": 0
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"display_data": {
"description": "Data displayed as a result of code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["display_data"]
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"stream": {
"description": "Stream output from a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "name", "text"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["stream"]
},
"name": {
"description": "The name of the stream (stdout, stderr).",
"type": "string"
},
"text": {
"description": "The stream's text output, represented as an array of strings.",
"$ref": "#/definitions/misc/multiline_string"
}
}
},
"error": {
"description": "Output of an error that occurred during code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "ename", "evalue", "traceback"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["error"]
},
"ename": {
"description": "The name of the error.",
"type": "string"
},
"evalue": {
"description": "The value, or message, of the error.",
"type": "string"
},
"traceback": {
"description": "The error's traceback, represented as an array of strings.",
"type": "array",
"items": {"type": "string"}
}
}
},
"unrecognized_output": {
"description": "Unrecognized output from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["output_type"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"not": {
"enum": ["execute_result", "display_data", "stream", "error"]
}
}
}
},
"misc": {
"metadata_name": {
"description": "The cell's name. If present, must be a non-empty string. Cell names are expected to be unique across all the cells in a given notebook. This criterion cannot be checked by the json schema and must be established by an additional check.",
"type": "string",
"pattern": "^.+$"
},
"metadata_tags": {
"description": "The cell's tags. Tags must be unique, and must not contain commas.",
"type": "array",
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "^[^,]+$"
}
},
"attachments": {
"description": "Media attachments (e.g. inline images), stored as mimebundle keyed by filename.",
"type": "object",
"patternProperties": {
".*": {
"description": "The attachment's data stored as a mimebundle.",
"$ref": "#/definitions/misc/mimebundle"
}
}
},
"source": {
"description": "Contents of the cell, represented as an array of lines.",
"$ref": "#/definitions/misc/multiline_string"
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
},
"mimebundle": {
"description": "A mime-type keyed dictionary of data",
"type": "object",
"additionalProperties": {
"description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
"$ref": "#/definitions/misc/multiline_string"
},
"patternProperties": {
"^application/(.*\\+)?json$": {
"description": "Mimetypes with JSON output, can be any type"
}
}
},
"output_metadata": {
"description": "Cell output metadata.",
"type": "object",
"additionalProperties": true
},
"multiline_string": {
"oneOf" : [
{"type": "string"},
{
"type": "array",
"items": {"type": "string"}
}
]
}
}
}
}

View file

@ -0,0 +1,456 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Jupyter Notebook v4.4 JSON schema.",
"type": "object",
"additionalProperties": false,
"required": ["metadata", "nbformat_minor", "nbformat", "cells"],
"properties": {
"metadata": {
"description": "Notebook root-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"kernelspec": {
"description": "Kernel information.",
"type": "object",
"required": ["name", "display_name"],
"properties": {
"name": {
"description": "Name of the kernel specification.",
"type": "string"
},
"display_name": {
"description": "Name to display in UI.",
"type": "string"
}
}
},
"language_info": {
"description": "Kernel information.",
"type": "object",
"required": ["name"],
"properties": {
"name": {
"description": "The programming language which this kernel runs.",
"type": "string"
},
"codemirror_mode": {
"description": "The codemirror mode to use for code in this language.",
"oneOf": [
{"type": "string"},
{"type": "object"}
]
},
"file_extension": {
"description": "The file extension for files in this language.",
"type": "string"
},
"mimetype": {
"description": "The mimetype corresponding to files in this language.",
"type": "string"
},
"pygments_lexer": {
"description": "The pygments lexer to use for code in this language.",
"type": "string"
}
}
},
"orig_nbformat": {
"description": "Original notebook format (major number) before converting the notebook between versions. This should never be written to a file.",
"type": "integer",
"minimum": 1
},
"title": {
"description": "The title of the notebook document",
"type": "string"
},
"authors": {
"description": "The author(s) of the notebook document",
"type": "array",
"item": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"additionalProperties": true
}
}
}
},
"nbformat_minor": {
"description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
"type": "integer",
"minimum": 4
},
"nbformat": {
"description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
"type": "integer",
"minimum": 4,
"maximum": 4
},
"cells": {
"description": "Array of cells of the current notebook.",
"type": "array",
"items": {"$ref": "#/definitions/cell"}
}
},
"definitions": {
"cell": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/raw_cell"},
{"$ref": "#/definitions/markdown_cell"},
{"$ref": "#/definitions/code_cell"}
]
},
"raw_cell": {
"description": "Notebook raw nbconvert cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["raw"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"format": {
"description": "Raw cell metadata format for nbconvert.",
"type": "string"
},
"jupyter": {
"description": "Official Jupyter Metadata for Raw Cells",
"type": "object",
"additionalProperties": true,
"source_hidden": {
"description": "Whether the source is hidden.",
"type": "boolean"
}
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"markdown_cell": {
"description": "Notebook markdown cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["markdown"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"},
"jupyter": {
"description": "Official Jupyter Metadata for Markdown Cells",
"type": "object",
"additionalProperties": true,
"source_hidden": {
"description": "Whether the source is hidden.",
"type": "boolean"
}
}
},
"additionalProperties": true
},
"attachments": {"$ref": "#/definitions/misc/attachments"},
"source": {"$ref": "#/definitions/misc/source"}
}
},
"code_cell": {
"description": "Notebook code cell.",
"type": "object",
"additionalProperties": false,
"required": ["cell_type", "metadata", "source", "outputs", "execution_count"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"enum": ["code"]
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"additionalProperties": true,
"properties": {
"jupyter": {
"description": "Official Jupyter Metadata for Code Cells",
"type": "object",
"additionalProperties": true,
"source_hidden": {
"description": "Whether the source is hidden.",
"type": "boolean"
},
"outputs_hidden": {
"description": "Whether the outputs are hidden.",
"type": "boolean"
}
},
"execution": {
"description": "Execution time for the code in the cell. This tracks time at which messages are received from iopub or shell channels",
"type": "object",
"properties": {
"iopub.execute_input": {
"description": "header.date (in ISO 8601 format) of iopub channel's execute_input message. It indicates the time at which the kernel broadcasts an execute_input message to connected frontends",
"type": "string"
},
"iopub.status.busy": {
"description": "header.date (in ISO 8601 format) of iopub channel's kernel status message when the status is 'busy'",
"type": "string"
},
"shell.execute_reply": {
"description": "header.date (in ISO 8601 format) of the shell channel's execute_reply message. It indicates the time at which the execute_reply message was created",
"type": "string"
},
"iopub.status.idle": {
"description": "header.date (in ISO 8601 format) of iopub channel's kernel status message when the status is 'idle'. It indicates the time at which kernel finished processing the associated request",
"type": "string"
}
},
"additionalProperties": true,
"patternProperties": {
"^.*$": {
"type": "string"
}
}
},
"collapsed": {
"description": "Whether the cell's output is collapsed/expanded.",
"type": "boolean"
},
"scrolled": {
"description": "Whether the cell's output is scrolled, unscrolled, or autoscrolled.",
"enum": [true, false, "auto"]
},
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
}
},
"source": {"$ref": "#/definitions/misc/source"},
"outputs": {
"description": "Execution, display, or stream outputs.",
"type": "array",
"items": {"$ref": "#/definitions/output"}
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
}
}
},
"unrecognized_cell": {
"description": "Unrecognized cell from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["cell_type", "metadata"],
"properties": {
"cell_type": {
"description": "String identifying the type of cell.",
"not" : {
"enum": ["markdown", "code", "raw"]
}
},
"metadata": {
"description": "Cell-level metadata.",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/misc/metadata_name"},
"tags": {"$ref": "#/definitions/misc/metadata_tags"}
},
"additionalProperties": true
}
}
},
"output": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/execute_result"},
{"$ref": "#/definitions/display_data"},
{"$ref": "#/definitions/stream"},
{"$ref": "#/definitions/error"}
]
},
"execute_result": {
"description": "Result of executing a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata", "execution_count"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["execute_result"]
},
"execution_count": {
"description": "A result's prompt number.",
"type": ["integer", "null"],
"minimum": 0
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"display_data": {
"description": "Data displayed as a result of code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "data", "metadata"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["display_data"]
},
"data": {"$ref": "#/definitions/misc/mimebundle"},
"metadata": {"$ref": "#/definitions/misc/output_metadata"}
}
},
"stream": {
"description": "Stream output from a code cell.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "name", "text"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["stream"]
},
"name": {
"description": "The name of the stream (stdout, stderr).",
"type": "string"
},
"text": {
"description": "The stream's text output, represented as an array of strings.",
"$ref": "#/definitions/misc/multiline_string"
}
}
},
"error": {
"description": "Output of an error that occurred during code cell execution.",
"type": "object",
"additionalProperties": false,
"required": ["output_type", "ename", "evalue", "traceback"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"enum": ["error"]
},
"ename": {
"description": "The name of the error.",
"type": "string"
},
"evalue": {
"description": "The value, or message, of the error.",
"type": "string"
},
"traceback": {
"description": "The error's traceback, represented as an array of strings.",
"type": "array",
"items": {"type": "string"}
}
}
},
"unrecognized_output": {
"description": "Unrecognized output from a future minor-revision to the notebook format.",
"type": "object",
"additionalProperties": true,
"required": ["output_type"],
"properties": {
"output_type": {
"description": "Type of cell output.",
"not": {
"enum": ["execute_result", "display_data", "stream", "error"]
}
}
}
},
"misc": {
"metadata_name": {
"description": "The cell's name. If present, must be a non-empty string. Cell names are expected to be unique across all the cells in a given notebook. This criterion cannot be checked by the json schema and must be established by an additional check.",
"type": "string",
"pattern": "^.+$"
},
"metadata_tags": {
"description": "The cell's tags. Tags must be unique, and must not contain commas.",
"type": "array",
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "^[^,]+$"
}
},
"attachments": {
"description": "Media attachments (e.g. inline images), stored as mimebundle keyed by filename.",
"type": "object",
"patternProperties": {
".*": {
"description": "The attachment's data stored as a mimebundle.",
"$ref": "#/definitions/misc/mimebundle"
}
}
},
"source": {
"description": "Contents of the cell, represented as an array of lines.",
"$ref": "#/definitions/misc/multiline_string"
},
"execution_count": {
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
"type": ["integer", "null"],
"minimum": 0
},
"mimebundle": {
"description": "A mime-type keyed dictionary of data",
"type": "object",
"additionalProperties": {
"description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
"$ref": "#/definitions/misc/multiline_string"
},
"patternProperties": {
"^application/(.*\\+)?json$": {
"description": "Mimetypes with JSON output, can be any type"
}
}
},
"output_metadata": {
"description": "Cell output metadata.",
"type": "object",
"additionalProperties": true
},
"multiline_string": {
"oneOf" : [
{"type": "string"},
{
"type": "array",
"items": {"type": "string"}
}
]
}
}
}
}

View file

@ -0,0 +1,66 @@
"""Read and write notebooks in JSON format."""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import copy
import json
from ..notebooknode import from_dict
from .rwbase import (
NotebookReader, NotebookWriter, rejoin_lines, split_lines, strip_transient
)
class BytesEncoder(json.JSONEncoder):
"""A JSON encoder that accepts b64 (and other *ascii*) bytestrings."""
def default(self, obj):
if isinstance(obj, bytes):
return obj.decode('ascii')
return json.JSONEncoder.default(self, obj)
class JSONReader(NotebookReader):
def reads(self, s, **kwargs):
"""Read a JSON string into a Notebook object"""
nb = json.loads(s, **kwargs)
nb = self.to_notebook(nb, **kwargs)
return nb
def to_notebook(self, d, **kwargs):
"""Convert a disk-format notebook dict to in-memory NotebookNode
handles multi-line values as strings, scrubbing of transient values, etc.
"""
nb = from_dict(d)
nb = rejoin_lines(nb)
nb = strip_transient(nb)
return nb
class JSONWriter(NotebookWriter):
def writes(self, nb, **kwargs):
"""Serialize a NotebookNode object as a JSON string"""
kwargs['cls'] = BytesEncoder
kwargs['indent'] = 1
kwargs['sort_keys'] = True
kwargs['separators'] = (',',': ')
kwargs.setdefault('ensure_ascii', False)
# don't modify in-memory dict
nb = copy.deepcopy(nb)
if kwargs.pop('split_lines', True):
nb = split_lines(nb)
nb = strip_transient(nb)
return json.dumps(nb, **kwargs)
_reader = JSONReader()
_writer = JSONWriter()
reads = _reader.reads
read = _reader.read
to_notebook = _reader.to_notebook
write = _writer.write
writes = _writer.writes

View file

@ -0,0 +1,125 @@
"""Base classes and utilities for readers and writers."""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
def _is_json_mime(mime):
"""Is a key a JSON mime-type that should be left alone?"""
return mime == 'application/json' or \
(mime.startswith('application/') and mime.endswith('+json'))
def _rejoin_mimebundle(data):
"""Rejoin the multi-line string fields in a mimebundle (in-place)"""
for key, value in list(data.items()):
if not _is_json_mime(key) \
and isinstance(value, list) \
and all(isinstance(line, str) for line in value):
data[key] = ''.join(value)
return data
def rejoin_lines(nb):
"""rejoin multiline text into strings
For reversing effects of ``split_lines(nb)``.
This only rejoins lines that have been split, so if text objects were not split
they will pass through unchanged.
Used when reading JSON files that may have been passed through split_lines.
"""
for cell in nb.cells:
if 'source' in cell and isinstance(cell.source, list):
cell.source = ''.join(cell.source)
attachments = cell.get('attachments', {})
for key, attachment in attachments.items():
_rejoin_mimebundle(attachment)
if cell.get('cell_type', None) == 'code':
for output in cell.get('outputs', []):
output_type = output.get('output_type', '')
if output_type in {'execute_result', 'display_data'}:
_rejoin_mimebundle(output.get('data', {}))
elif output_type:
if isinstance(output.get('text', ''), list):
output.text = ''.join(output.text)
return nb
_non_text_split_mimes = {
'application/javascript',
'image/svg+xml',
}
def _split_mimebundle(data):
"""Split multi-line string fields in a mimebundle (in-place)"""
for key, value in list(data.items()):
if isinstance(value, str) and (
key.startswith('text/') or key in _non_text_split_mimes
):
data[key] = value.splitlines(True)
return data
def split_lines(nb):
"""split likely multiline text into lists of strings
For file output more friendly to line-based VCS. ``rejoin_lines(nb)`` will
reverse the effects of ``split_lines(nb)``.
Used when writing JSON files.
"""
for cell in nb.cells:
source = cell.get('source', None)
if isinstance(source, str):
cell['source'] = source.splitlines(True)
attachments = cell.get('attachments', {})
for key, attachment in attachments.items():
_split_mimebundle(attachment)
if cell.cell_type == 'code':
for output in cell.outputs:
if output.output_type in {'execute_result', 'display_data'}:
_split_mimebundle(output.get('data', {}))
elif output.output_type == 'stream':
if isinstance(output.text, str):
output.text = output.text.splitlines(True)
return nb
def strip_transient(nb):
"""Strip transient values that shouldn't be stored in files.
This should be called in *both* read and write.
"""
nb.metadata.pop('orig_nbformat', None)
nb.metadata.pop('orig_nbformat_minor', None)
nb.metadata.pop('signature', None)
for cell in nb.cells:
cell.metadata.pop('trusted', None)
return nb
class NotebookReader(object):
"""A class for reading notebooks."""
def reads(self, s, **kwargs):
"""Read a notebook from a string."""
raise NotImplementedError("reads must be implemented in a subclass")
def read(self, fp, **kwargs):
"""Read a notebook from a file like object"""
nbs = fp.read()
return self.reads(nbs, **kwargs)
class NotebookWriter(object):
"""A class for writing notebooks."""
def writes(self, nb, **kwargs):
"""Write a notebook to a string."""
raise NotImplementedError("writes must be implemented in a subclass")
def write(self, nb, fp, **kwargs):
"""Write a notebook to a file like object"""
nbs = self.writes(nb, **kwargs)
return fp.write(nbs)

View file

@ -0,0 +1,54 @@
# -*- coding: utf8 -*-
import io
import os
import shutil
import tempfile
pjoin = os.path.join
from .nbexamples import nb0
def open_utf8(fname, mode):
return io.open(fname, mode=mode, encoding='utf-8')
class NBFormatTest:
"""Mixin for writing notebook format tests"""
# override with appropriate values in subclasses
nb0_ref = None
ext = None
mod = None
def setUp(self):
self.wd = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.wd)
def assertNBEquals(self, nba, nbb):
self.assertEqual(nba, nbb)
def test_writes(self):
s = self.mod.writes(nb0)
if self.nb0_ref:
self.assertEqual(s, self.nb0_ref)
def test_reads(self):
s = self.mod.writes(nb0)
nb = self.mod.reads(s)
def test_roundtrip(self):
s = self.mod.writes(nb0)
self.assertNBEquals(self.mod.reads(s),nb0)
def test_write_file(self):
with open_utf8(pjoin(self.wd, "nb0.%s" % self.ext), 'w') as f:
self.mod.write(nb0, f)
def test_read_file(self):
with open_utf8(pjoin(self.wd, "nb0.%s" % self.ext), 'w') as f:
self.mod.write(nb0, f)
with open_utf8(pjoin(self.wd, "nb0.%s" % self.ext), 'r') as f:
nb = self.mod.read(f)

View file

@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-
import os
from ..._compat import encodebytes
from ..nbbase import (
new_code_cell, new_markdown_cell, new_notebook,
new_output, new_raw_cell
)
# some random base64-encoded *text*
png = encodebytes(os.urandom(5)).decode('ascii')
jpeg = encodebytes(os.urandom(6)).decode('ascii')
cells = []
cells.append(new_markdown_cell(
source='Some NumPy Examples',
))
cells.append(new_code_cell(
source='import numpy',
execution_count=1,
))
cells.append(new_markdown_cell(
source='Cell with attachments',
attachments={
'attachment1': {
'text/plain': '\n'.join(['a', 'b', 'c']),
'application/vnd.stuff+json': ['a', 1, 'x'],
}
}
))
cells.append(new_raw_cell(
source='A random array',
))
cells.append(new_markdown_cell(
source=u'## My Heading',
))
cells.append(new_code_cell(
source='a = numpy.random.rand(100)',
execution_count=2,
))
cells.append(new_code_cell(
source='a = 10\nb = 5\n',
execution_count=3,
))
cells.append(new_code_cell(
source='a = 10\nb = 5',
execution_count=4,
))
cells.append(new_code_cell(
source=u'json_outputs()',
execution_count=12,
outputs=[new_output(
output_type=u'display_data',
data={
'text/plain': u'<json outputs>',
'application/json': {
'key': 'value',
'x': 5,
'lis': [1, 2, 'x']
},
'application/vnd.listofstr+json': ['a', 'b', 'c'],
'application/vnd.numbers+json': [1, 2, 3],
'application/vnd.number+json': 42,
'application/vnd.object+json': {
'number': 5,
'array': [1,2],
'str': 'x'
},
'application/vnd.string+json': 'ok',
},
)]
))
cells.append(new_code_cell(
source=u'print "ünîcødé"',
execution_count=3,
outputs=[new_output(
output_type=u'execute_result',
data={
'text/plain': u'<array a>',
'text/html': u'The HTML rep',
'text/latex': u'$a$',
'image/png': png,
'image/jpeg': jpeg,
'image/svg+xml': u'<svg>',
'application/json': {
'key': 'value'
},
'application/javascript': u'var i=0;'
},
execution_count=3
),new_output(
output_type=u'display_data',
data={
'text/plain': u'<array a>',
'text/html': u'The HTML rep',
'text/latex': u'$a$',
'image/png': png,
'image/jpeg': jpeg,
'image/svg+xml': u'<svg>',
'application/json': {
'key': 'value'
},
'application/javascript': u'var i=0;'
},
),new_output(
output_type=u'error',
ename=u'NameError',
evalue=u'NameError was here',
traceback=[u'frame 0', u'frame 1', u'frame 2']
),new_output(
output_type=u'stream',
text='foo\rbar\r\n'
),new_output(
output_type=u'stream',
name='stderr',
text='\rfoo\rbar\n'
)]
))
nb0 = new_notebook(cells=cells,
metadata={
'language': 'python',
}
)

View file

@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
import copy
from nbformat import validate
from .. import convert
from . import nbexamples
from nbformat.v3.tests import nbexamples as v3examples
from nbformat import v3, v4
def test_upgrade_notebook():
nb03 = copy.deepcopy(v3examples.nb0)
validate(nb03)
nb04 = convert.upgrade(nb03)
validate(nb04)
def test_downgrade_notebook():
nb04 = copy.deepcopy(nbexamples.nb0)
validate(nb04)
nb03 = convert.downgrade(nb04)
validate(nb03)
def test_upgrade_heading():
v3h = v3.new_heading_cell
v4m = v4.new_markdown_cell
for v3cell, expected in [
(
v3h(source='foo', level=1),
v4m(source='# foo'),
),
(
v3h(source='foo\nbar\nmulti-line\n', level=4),
v4m(source='#### foo bar multi-line'),
),
(
v3h(source=u'ünìcö∂ecønvërsioñ', level=4),
v4m(source=u'#### ünìcö∂ecønvërsioñ'),
),
]:
upgraded = convert.upgrade_cell(v3cell)
assert upgraded == expected
def test_downgrade_heading():
v3h = v3.new_heading_cell
v4m = v4.new_markdown_cell
v3m = lambda source: v3.new_text_cell('markdown', source)
for v4cell, expected in [
(
v4m(source='# foo'),
v3h(source='foo', level=1),
),
(
v4m(source='#foo'),
v3h(source='foo', level=1),
),
(
v4m(source='#\tfoo'),
v3h(source='foo', level=1),
),
(
v4m(source='# \t foo'),
v3h(source='foo', level=1),
),
(
v4m(source='# foo\nbar'),
v3m(source='# foo\nbar'),
),
]:
downgraded = convert.downgrade_cell(v4cell)
assert downgraded == expected

View file

@ -0,0 +1,122 @@
import os
import json
from unittest import TestCase
from ..._compat import decodebytes
from ..nbjson import reads, writes
from .. import nbjson, nbformat, nbformat_minor
from .nbexamples import nb0
from . import formattest
class TestJSON(formattest.NBFormatTest, TestCase):
nb0_ref = None
ext = 'ipynb'
mod = nbjson
def test_roundtrip_nosplit(self):
"""Ensure that multiline blobs are still readable"""
# ensures that notebooks written prior to splitlines change
# are still readable.
s = writes(nb0, split_lines=False)
self.assertEqual(nbjson.reads(s),nb0)
def test_roundtrip_split(self):
"""Ensure that splitting multiline blocks is safe"""
# This won't differ from test_roundtrip unless the default changes
s = writes(nb0, split_lines=True)
self.assertEqual(nbjson.reads(s),nb0)
def test_splitlines(self):
"""Test splitlines in mime-bundles"""
s = writes(nb0, split_lines=True)
raw_nb = json.loads(s)
for i, ref_cell in enumerate(nb0.cells):
if ref_cell.source.strip() == 'Cell with attachments':
attach_ref = ref_cell['attachments']['attachment1']
attach_json = raw_nb['cells'][i]['attachments']['attachment1']
if ref_cell.source.strip() == 'json_outputs()':
output_ref = ref_cell['outputs'][0]['data']
output_json = raw_nb['cells'][i]['outputs'][0]['data']
for key, json_value in attach_json.items():
if key == 'text/plain':
# text should be split
assert json_value == attach_ref['text/plain'].splitlines(True)
else:
# JSON attachments
assert json_value == attach_ref[key]
# check that JSON outputs are left alone:
for key, json_value in output_json.items():
if key == 'text/plain':
# text should be split
assert json_value == output_ref['text/plain'].splitlines(True)
else:
# JSON outputs should be left alone
assert json_value == output_ref[key]
def test_read_png(self):
"""PNG output data is b64 unicode"""
s = writes(nb0)
nb1 = nbjson.reads(s)
found_png = False
for cell in nb1.cells:
if not 'outputs' in cell:
continue
for output in cell.outputs:
if not 'data' in output:
continue
if 'image/png' in output.data:
found_png = True
pngdata = output.data['image/png']
self.assertEqual(type(pngdata), str)
# test that it is valid b64 data
b64bytes = pngdata.encode('ascii')
raw_bytes = decodebytes(b64bytes)
assert found_png, "never found png output"
def test_read_jpeg(self):
"""JPEG output data is b64 unicode"""
s = writes(nb0)
nb1 = nbjson.reads(s)
found_jpeg = False
for cell in nb1.cells:
if not 'outputs' in cell:
continue
for output in cell.outputs:
if not 'data' in output:
continue
if 'image/jpeg' in output.data:
found_jpeg = True
jpegdata = output.data['image/jpeg']
self.assertEqual(type(jpegdata), str)
# test that it is valid b64 data
b64bytes = jpegdata.encode('ascii')
raw_bytes = decodebytes(b64bytes)
assert found_jpeg, "never found jpeg output"
def test_latest_schema_matches(self):
"""Test to ensure all schema is locked to a known version"""
assert nbformat == 4
assert nbformat_minor == 4
def test_base_version_matches_latest(self):
"""Test to ensure latest version file matches latest verison"""
with open(os.path.join(os.path.dirname(__file__), '..', 'nbformat.v4.schema.json'), 'r') as schema_file:
latest_schema = json.load(schema_file)
with open(os.path.join(os.path.dirname(__file__), '..', 'nbformat.v{major}.{minor}.schema.json'.format(
major=nbformat, minor=nbformat_minor)), 'r') as schema_file:
ver_schema = json.load(schema_file)
assert latest_schema == ver_schema
def test_latest_matches_nbformat(self):
"""Test to ensure that the nbformat version matches the description of the latest schema"""
with open(os.path.join(os.path.dirname(__file__), '..', 'nbformat.v4.schema.json'), 'r') as schema_file:
schema = json.load(schema_file)
assert schema['description'] == 'Jupyter Notebook v{major}.{minor} JSON schema.'.format(
major=nbformat, minor=nbformat_minor
)

View file

@ -0,0 +1,99 @@
# coding: utf-8
"""Tests for the Python API for composing notebook elements"""
from nbformat.validator import isvalid, validate, ValidationError
from ..nbbase import (
NotebookNode, nbformat,
new_code_cell, new_markdown_cell, new_notebook,
new_output, new_raw_cell,
)
def test_empty_notebook():
nb = new_notebook()
assert nb.cells == []
assert nb.metadata == NotebookNode()
assert nb.nbformat == nbformat
def test_empty_markdown_cell():
cell = new_markdown_cell()
assert cell.cell_type == 'markdown'
assert cell.source == ''
def test_markdown_cell():
cell = new_markdown_cell(u'* Søme markdown')
assert cell.source == u'* Søme markdown'
def test_empty_raw_cell():
cell = new_raw_cell()
assert cell.cell_type == u'raw'
assert cell.source == ''
def test_raw_cell():
cell = new_raw_cell('hi')
assert cell.source == u'hi'
def test_empty_code_cell():
cell = new_code_cell('hi')
assert cell.cell_type == 'code'
assert cell.source == u'hi'
def test_empty_display_data():
output = new_output('display_data')
assert output.output_type == 'display_data'
def test_empty_stream():
output = new_output('stream')
assert output.output_type == 'stream'
assert output.name == 'stdout'
assert output.text == ''
def test_empty_execute_result():
output = new_output('execute_result', execution_count=1)
assert output.output_type == 'execute_result'
mimebundle = {
'text/plain': "some text",
"application/json": {
"key": "value"
},
"image/svg+xml": 'ABCDEF',
"application/octet-stream": 'ABC-123',
"application/vnd.foo+bar": "Some other stuff",
}
def test_display_data():
output = new_output('display_data', mimebundle)
for key, expected in mimebundle.items():
assert output.data[key] == expected
def test_execute_result():
output = new_output('execute_result', mimebundle, execution_count=10)
assert output.execution_count == 10
for key, expected in mimebundle.items():
assert output.data[key] == expected
def test_error():
o = new_output(output_type=u'error', ename=u'NameError',
evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2']
)
assert o.output_type == u'error'
assert o.ename == u'NameError'
assert o.evalue == u'Name not found'
assert o.traceback == [u'frame 0', u'frame 1', u'frame 2']
def test_code_cell_with_outputs():
cell = new_code_cell(execution_count=10, outputs=[
new_output('display_data', mimebundle),
new_output('stream', text='hello'),
new_output('execute_result', mimebundle, execution_count=10),
])
assert cell.execution_count == 10
assert len(cell.outputs) == 3
er = cell.outputs[-1]
assert er.execution_count == 10
assert er['output_type'] == 'execute_result'
def test_stream():
output = new_output('stream', name='stderr', text='hello there')
assert output.name == 'stderr'
assert output.text == 'hello there'

View file

@ -0,0 +1,105 @@
"""Tests for nbformat validation"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import io
import os
import pytest
from nbformat.validator import validate, ValidationError
from ..nbjson import reads
from ..nbbase import (
nbformat,
new_code_cell, new_markdown_cell, new_notebook,
new_output, new_raw_cell,
)
def validate4(obj, ref=None):
return validate(obj, ref, version=nbformat)
def test_valid_code_cell():
cell = new_code_cell()
validate4(cell, 'code_cell')
def test_invalid_code_cell():
cell = new_code_cell()
cell['source'] = 5
with pytest.raises(ValidationError):
validate4(cell, 'code_cell')
cell = new_code_cell()
del cell['metadata']
with pytest.raises(ValidationError):
validate4(cell, 'code_cell')
cell = new_code_cell()
del cell['source']
with pytest.raises(ValidationError):
validate4(cell, 'code_cell')
cell = new_code_cell()
del cell['cell_type']
with pytest.raises(ValidationError):
validate4(cell, 'code_cell')
def test_invalid_markdown_cell():
cell = new_markdown_cell()
cell['source'] = 5
with pytest.raises(ValidationError):
validate4(cell, 'markdown_cell')
cell = new_markdown_cell()
del cell['metadata']
with pytest.raises(ValidationError):
validate4(cell, 'markdown_cell')
cell = new_markdown_cell()
del cell['source']
with pytest.raises(ValidationError):
validate4(cell, 'markdown_cell')
cell = new_markdown_cell()
del cell['cell_type']
with pytest.raises(ValidationError):
validate4(cell, 'markdown_cell')
def test_invalid_raw_cell():
cell = new_raw_cell()
cell['source'] = 5
with pytest.raises(ValidationError):
validate4(cell, 'raw_cell')
cell = new_raw_cell()
del cell['metadata']
with pytest.raises(ValidationError):
validate4(cell, 'raw_cell')
cell = new_raw_cell()
del cell['source']
with pytest.raises(ValidationError):
validate4(cell, 'raw_cell')
cell = new_raw_cell()
del cell['cell_type']
with pytest.raises(ValidationError):
validate4(cell, 'raw_cell')
def test_sample_notebook():
here = os.path.dirname(__file__)
with io.open(os.path.join(here, os.pardir, os.pardir, 'tests', "test4.ipynb"), encoding='utf-8') as f:
nb = reads(f.read())
validate4(nb)