first commit

This commit is contained in:
Ayxan
2022-05-23 00:16:32 +04:00
commit d660f2a4ca
24786 changed files with 4428337 additions and 0 deletions

View File

@ -0,0 +1,39 @@
"""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_notebook,
new_output,
new_raw_cell,
output_from_msg,
)
from .nbjson import reads, to_notebook, writes
reads_json = reads
writes_json = writes
to_notebook_json = to_notebook
from .convert import downgrade, upgrade

View File

@ -0,0 +1,293 @@
"""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 traitlets.log import get_logger
from nbformat import v3
from .. import validator
from .nbbase import NotebookNode, nbformat, nbformat_minor, random_cell_id
def _warn_if_invalid(nb, version):
"""Log validation errors, if there are any."""
from nbformat import ValidationError, validate
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=None, from_minor=None):
"""Convert a notebook to latest 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 not from_version:
from_version = nb["nbformat"]
if not from_minor:
if "nbformat_minor" not in nb:
raise validator.ValidationError(
"The notebook does not include the nbformat minor which is needed"
)
from_minor = nb["nbformat_minor"]
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:
if from_minor == nbformat_minor:
return nb
# other versions migration code e.g.
# if from_minor < 3:
# if from_minor < 4:
if from_minor < 5:
for cell in nb.cells:
cell.id = random_cell_id()
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())
cell.id = random_cell_id()
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 = "{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("#"):
match = re.match(r"(#+)\s*(.*)", source)
assert match is not None
prefix, text = match.groups()
cell.cell_type = "heading"
cell.source = text
cell.level = len(prefix)
cell.pop("id", None)
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,173 @@
"""Python API for composing notebook elements
The Python representation of a notebook is a nested structure of
dictionary subclasses that support attribute access.
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 ..corpus.words import generate_corpus_id as random_cell_id
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 = 5
# 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",
(4, 5): "nbformat.v4.5.schema.json",
}
def validate(node, ref=None):
"""validate a v4 node"""
from .. import validate as validate_orig
return validate_orig(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 = "stdout"
output.text = ""
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(
id=random_cell_id(),
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(
id=random_cell_id(),
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(
id=random_cell_id(),
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,383 @@
{
"$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,383 @@
{
"$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,400 @@
{
"$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,431 @@
{
"$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,459 @@
{
"$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,471 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Jupyter Notebook v4.5 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": 5
},
"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_id": {
"description": "A string field representing the identifier of this particular cell.",
"type": "string",
"pattern": "^[a-zA-Z0-9-_]+$",
"minLength": 1,
"maxLength": 64
},
"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": ["id", "cell_type", "metadata", "source"],
"properties": {
"id": { "$ref": "#/definitions/cell_id" },
"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": ["id", "cell_type", "metadata", "source"],
"properties": {
"id": { "$ref": "#/definitions/cell_id" },
"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": [
"id",
"cell_type",
"metadata",
"source",
"outputs",
"execution_count"
],
"properties": {
"id": { "$ref": "#/definitions/cell_id" },
"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,471 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Jupyter Notebook v4.5 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": 5
},
"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_id": {
"description": "A string field representing the identifier of this particular cell.",
"type": "string",
"pattern": "^[a-zA-Z0-9-_]+$",
"minLength": 1,
"maxLength": 64
},
"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": ["id", "cell_type", "metadata", "source"],
"properties": {
"id": { "$ref": "#/definitions/cell_id" },
"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": ["id", "cell_type", "metadata", "source"],
"properties": {
"id": { "$ref": "#/definitions/cell_id" },
"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": [
"id",
"cell_type",
"metadata",
"source",
"outputs",
"execution_count"
],
"properties": {
"id": { "$ref": "#/definitions/cell_id" },
"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,69 @@
"""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,132 @@
"""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 _, 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 _, 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:
"""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:
"""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)