mirror of
https://github.com/aykhans/AzSuicideDataVisualization.git
synced 2025-07-02 14:27:31 +00:00
first commit
This commit is contained in:
99
.venv/Lib/site-packages/nbformat/v3/__init__.py
Normal file
99
.venv/Lib/site-packages/nbformat/v3/__init__.py
Normal file
@ -0,0 +1,99 @@
|
||||
"""The main API for the v3 notebook format.
|
||||
"""
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
__all__ = [
|
||||
"NotebookNode",
|
||||
"new_code_cell",
|
||||
"new_text_cell",
|
||||
"new_notebook",
|
||||
"new_output",
|
||||
"new_worksheet",
|
||||
"new_metadata",
|
||||
"new_author",
|
||||
"new_heading_cell",
|
||||
"nbformat",
|
||||
"nbformat_minor",
|
||||
"nbformat_schema",
|
||||
"reads_json",
|
||||
"writes_json",
|
||||
"read_json",
|
||||
"write_json",
|
||||
"to_notebook_json",
|
||||
"reads_py",
|
||||
"writes_py",
|
||||
"read_py",
|
||||
"write_py",
|
||||
"to_notebook_py",
|
||||
"downgrade",
|
||||
"upgrade",
|
||||
"parse_filename",
|
||||
]
|
||||
|
||||
import os
|
||||
|
||||
from .convert import downgrade, upgrade
|
||||
from .nbbase import (
|
||||
NotebookNode,
|
||||
nbformat,
|
||||
nbformat_minor,
|
||||
nbformat_schema,
|
||||
new_author,
|
||||
new_code_cell,
|
||||
new_heading_cell,
|
||||
new_metadata,
|
||||
new_notebook,
|
||||
new_output,
|
||||
new_text_cell,
|
||||
new_worksheet,
|
||||
)
|
||||
from .nbjson import reads as read_json
|
||||
from .nbjson import reads as reads_json
|
||||
from .nbjson import to_notebook as to_notebook_json
|
||||
from .nbjson import writes as write_json
|
||||
from .nbjson import writes as writes_json
|
||||
from .nbpy import reads as read_py
|
||||
from .nbpy import reads as reads_py
|
||||
from .nbpy import to_notebook as to_notebook_py
|
||||
from .nbpy import writes as write_py
|
||||
from .nbpy import writes as writes_py
|
||||
|
||||
|
||||
def parse_filename(fname):
|
||||
"""Parse a notebook filename.
|
||||
|
||||
This function takes a notebook filename and returns the notebook
|
||||
format (json/py) and the notebook name. This logic can be
|
||||
summarized as follows:
|
||||
|
||||
* notebook.ipynb -> (notebook.ipynb, notebook, json)
|
||||
* notebook.json -> (notebook.json, notebook, json)
|
||||
* notebook.py -> (notebook.py, notebook, py)
|
||||
* notebook -> (notebook.ipynb, notebook, json)
|
||||
|
||||
Parameters
|
||||
----------
|
||||
fname : unicode
|
||||
The notebook filename. The filename can use a specific filename
|
||||
extention (.ipynb, .json, .py) or none, in which case .ipynb will
|
||||
be assumed.
|
||||
|
||||
Returns
|
||||
-------
|
||||
(fname, name, format) : (unicode, unicode, unicode)
|
||||
The filename, notebook name and format.
|
||||
"""
|
||||
basename, ext = os.path.splitext(fname)
|
||||
if ext == ".ipynb":
|
||||
format = "json"
|
||||
elif ext == ".json":
|
||||
format = "json"
|
||||
elif ext == ".py":
|
||||
format = "py"
|
||||
else:
|
||||
basename = fname
|
||||
fname = fname + ".ipynb"
|
||||
format = "json"
|
||||
return fname, basename, format
|
90
.venv/Lib/site-packages/nbformat/v3/convert.py
Normal file
90
.venv/Lib/site-packages/nbformat/v3/convert.py
Normal file
@ -0,0 +1,90 @@
|
||||
"""Code for converting notebooks to and from the v2 format."""
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
from .nbbase import nbformat, nbformat_minor
|
||||
|
||||
|
||||
def _unbytes(obj):
|
||||
"""There should be no bytes objects in a notebook
|
||||
|
||||
v2 stores png/jpeg as b64 ascii bytes
|
||||
"""
|
||||
if isinstance(obj, dict):
|
||||
for k, v in obj.items():
|
||||
obj[k] = _unbytes(v)
|
||||
elif isinstance(obj, list):
|
||||
for i, v in enumerate(obj):
|
||||
obj[i] = _unbytes(v)
|
||||
elif isinstance(obj, bytes):
|
||||
# only valid bytes are b64-encoded ascii
|
||||
obj = obj.decode("ascii")
|
||||
return obj
|
||||
|
||||
|
||||
def upgrade(nb, from_version=2, from_minor=0):
|
||||
"""Convert a notebook to v3.
|
||||
|
||||
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 == 2:
|
||||
# Mark the original nbformat so consumers know it has been converted.
|
||||
nb.nbformat = nbformat
|
||||
nb.nbformat_minor = nbformat_minor
|
||||
|
||||
nb.orig_nbformat = 2
|
||||
nb = _unbytes(nb)
|
||||
for ws in nb["worksheets"]:
|
||||
for cell in ws["cells"]:
|
||||
cell.setdefault("metadata", {})
|
||||
return nb
|
||||
elif from_version == 3:
|
||||
if from_minor != nbformat_minor:
|
||||
nb.orig_nbformat_minor = from_minor
|
||||
nb.nbformat_minor = nbformat_minor
|
||||
return nb
|
||||
else:
|
||||
raise ValueError(
|
||||
"Cannot convert a notebook directly from v%s to v3. "
|
||||
"Try using the nbformat.convert module." % from_version
|
||||
)
|
||||
|
||||
|
||||
def heading_to_md(cell):
|
||||
"""turn heading cell into corresponding markdown"""
|
||||
cell.cell_type = "markdown"
|
||||
level = cell.pop("level", 1)
|
||||
cell.source = "#" * level + " " + cell.source
|
||||
|
||||
|
||||
def raw_to_md(cell):
|
||||
"""let raw passthrough as markdown"""
|
||||
cell.cell_type = "markdown"
|
||||
|
||||
|
||||
def downgrade(nb):
|
||||
"""Convert a v3 notebook to v2.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
nb : NotebookNode
|
||||
The Python representation of the notebook to convert.
|
||||
"""
|
||||
if nb.nbformat != 3:
|
||||
return nb
|
||||
nb.nbformat = 2
|
||||
for ws in nb.worksheets:
|
||||
for cell in ws.cells:
|
||||
if cell.cell_type == "heading":
|
||||
heading_to_md(cell)
|
||||
elif cell.cell_type == "raw":
|
||||
raw_to_md(cell)
|
||||
return nb
|
237
.venv/Lib/site-packages/nbformat/v3/nbbase.py
Normal file
237
.venv/Lib/site-packages/nbformat/v3/nbbase.py
Normal file
@ -0,0 +1,237 @@
|
||||
"""The basic dict based notebook format.
|
||||
|
||||
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.
|
||||
|
||||
import warnings
|
||||
|
||||
from .._struct import Struct
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Code
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
# Change this when incrementing the nbformat version
|
||||
nbformat = 3
|
||||
nbformat_minor = 0
|
||||
nbformat_schema = {(3, 0): "nbformat.v3.schema.json"}
|
||||
|
||||
|
||||
class NotebookNode(Struct):
|
||||
pass
|
||||
|
||||
|
||||
def from_dict(d):
|
||||
if isinstance(d, dict):
|
||||
newd = NotebookNode()
|
||||
for k, v in d.items():
|
||||
newd[k] = from_dict(v)
|
||||
return newd
|
||||
elif isinstance(d, (tuple, list)):
|
||||
return [from_dict(i) for i in d]
|
||||
else:
|
||||
return d
|
||||
|
||||
|
||||
def str_passthrough(obj):
|
||||
"""
|
||||
Used to be cast_unicode, add this temporarily to make sure no further breakage.
|
||||
"""
|
||||
assert isinstance(obj, str)
|
||||
return obj
|
||||
|
||||
|
||||
def cast_str(obj):
|
||||
if isinstance(obj, bytes):
|
||||
# really this should never happend, it should
|
||||
# have been base64 encoded before.
|
||||
warnings.warn(
|
||||
"A notebook got bytes instead of likely base64 encoded values."
|
||||
"The content will likely be corrupted.",
|
||||
UserWarning,
|
||||
stacklevel=3,
|
||||
)
|
||||
return obj.decode("ascii", "replace")
|
||||
else:
|
||||
assert isinstance(obj, str)
|
||||
return obj
|
||||
|
||||
|
||||
def new_output(
|
||||
output_type,
|
||||
output_text=None,
|
||||
output_png=None,
|
||||
output_html=None,
|
||||
output_svg=None,
|
||||
output_latex=None,
|
||||
output_json=None,
|
||||
output_javascript=None,
|
||||
output_jpeg=None,
|
||||
prompt_number=None,
|
||||
ename=None,
|
||||
evalue=None,
|
||||
traceback=None,
|
||||
stream=None,
|
||||
metadata=None,
|
||||
):
|
||||
"""Create a new output, to go in the ``cell.outputs`` list of a code cell."""
|
||||
output = NotebookNode()
|
||||
output.output_type = str(output_type)
|
||||
|
||||
if metadata is None:
|
||||
metadata = {}
|
||||
if not isinstance(metadata, dict):
|
||||
raise TypeError("metadata must be dict")
|
||||
|
||||
if output_type in {"pyout", "display_data"}:
|
||||
output.metadata = metadata
|
||||
|
||||
if output_type != "pyerr":
|
||||
if output_text is not None:
|
||||
output.text = str_passthrough(output_text)
|
||||
if output_png is not None:
|
||||
output.png = cast_str(output_png)
|
||||
if output_jpeg is not None:
|
||||
output.jpeg = cast_str(output_jpeg)
|
||||
if output_html is not None:
|
||||
output.html = str_passthrough(output_html)
|
||||
if output_svg is not None:
|
||||
output.svg = str_passthrough(output_svg)
|
||||
if output_latex is not None:
|
||||
output.latex = str_passthrough(output_latex)
|
||||
if output_json is not None:
|
||||
output.json = str_passthrough(output_json)
|
||||
if output_javascript is not None:
|
||||
output.javascript = str_passthrough(output_javascript)
|
||||
|
||||
if output_type == "pyout":
|
||||
if prompt_number is not None:
|
||||
output.prompt_number = int(prompt_number)
|
||||
|
||||
if output_type == "pyerr":
|
||||
if ename is not None:
|
||||
output.ename = str_passthrough(ename)
|
||||
if evalue is not None:
|
||||
output.evalue = str_passthrough(evalue)
|
||||
if traceback is not None:
|
||||
output.traceback = [str_passthrough(frame) for frame in list(traceback)]
|
||||
|
||||
if output_type == "stream":
|
||||
output.stream = "stdout" if stream is None else str_passthrough(stream)
|
||||
|
||||
return output
|
||||
|
||||
|
||||
def new_code_cell(
|
||||
input=None, prompt_number=None, outputs=None, language="python", collapsed=False, metadata=None
|
||||
):
|
||||
"""Create a new code cell with input and output"""
|
||||
cell = NotebookNode()
|
||||
cell.cell_type = "code"
|
||||
if language is not None:
|
||||
cell.language = str_passthrough(language)
|
||||
if input is not None:
|
||||
cell.input = str_passthrough(input)
|
||||
if prompt_number is not None:
|
||||
cell.prompt_number = int(prompt_number)
|
||||
if outputs is None:
|
||||
cell.outputs = []
|
||||
else:
|
||||
cell.outputs = outputs
|
||||
if collapsed is not None:
|
||||
cell.collapsed = bool(collapsed)
|
||||
cell.metadata = NotebookNode(metadata or {})
|
||||
|
||||
return cell
|
||||
|
||||
|
||||
def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
|
||||
"""Create a new text cell."""
|
||||
cell = NotebookNode()
|
||||
# VERSIONHACK: plaintext -> raw
|
||||
# handle never-released plaintext name for raw cells
|
||||
if cell_type == "plaintext":
|
||||
cell_type = "raw"
|
||||
if source is not None:
|
||||
cell.source = str_passthrough(source)
|
||||
cell.metadata = NotebookNode(metadata or {})
|
||||
cell.cell_type = cell_type
|
||||
return cell
|
||||
|
||||
|
||||
def new_heading_cell(source=None, level=1, rendered=None, metadata=None):
|
||||
"""Create a new section cell with a given integer level."""
|
||||
cell = NotebookNode()
|
||||
cell.cell_type = "heading"
|
||||
if source is not None:
|
||||
cell.source = str_passthrough(source)
|
||||
cell.level = int(level)
|
||||
cell.metadata = NotebookNode(metadata or {})
|
||||
return cell
|
||||
|
||||
|
||||
def new_worksheet(name=None, cells=None, metadata=None):
|
||||
"""Create a worksheet by name with with a list of cells."""
|
||||
ws = NotebookNode()
|
||||
if cells is None:
|
||||
ws.cells = []
|
||||
else:
|
||||
ws.cells = list(cells)
|
||||
ws.metadata = NotebookNode(metadata or {})
|
||||
return ws
|
||||
|
||||
|
||||
def new_notebook(name=None, metadata=None, worksheets=None):
|
||||
"""Create a notebook by name, id and a list of worksheets."""
|
||||
nb = NotebookNode()
|
||||
nb.nbformat = nbformat
|
||||
nb.nbformat_minor = nbformat_minor
|
||||
if worksheets is None:
|
||||
nb.worksheets = []
|
||||
else:
|
||||
nb.worksheets = list(worksheets)
|
||||
if metadata is None:
|
||||
nb.metadata = new_metadata()
|
||||
else:
|
||||
nb.metadata = NotebookNode(metadata)
|
||||
if name is not None:
|
||||
nb.metadata.name = str_passthrough(name)
|
||||
return nb
|
||||
|
||||
|
||||
def new_metadata(name=None, authors=None, license=None, created=None, modified=None, gistid=None):
|
||||
"""Create a new metadata node."""
|
||||
metadata = NotebookNode()
|
||||
if name is not None:
|
||||
metadata.name = str_passthrough(name)
|
||||
if authors is not None:
|
||||
metadata.authors = list(authors)
|
||||
if created is not None:
|
||||
metadata.created = str_passthrough(created)
|
||||
if modified is not None:
|
||||
metadata.modified = str_passthrough(modified)
|
||||
if license is not None:
|
||||
metadata.license = str_passthrough(license)
|
||||
if gistid is not None:
|
||||
metadata.gistid = str_passthrough(gistid)
|
||||
return metadata
|
||||
|
||||
|
||||
def new_author(name=None, email=None, affiliation=None, url=None):
|
||||
"""Create a new author."""
|
||||
author = NotebookNode()
|
||||
if name is not None:
|
||||
author.name = str_passthrough(name)
|
||||
if email is not None:
|
||||
author.email = str_passthrough(email)
|
||||
if affiliation is not None:
|
||||
author.affiliation = str_passthrough(affiliation)
|
||||
if url is not None:
|
||||
author.url = str_passthrough(url)
|
||||
return author
|
367
.venv/Lib/site-packages/nbformat/v3/nbformat.v3.schema.json
Normal file
367
.venv/Lib/site-packages/nbformat/v3/nbformat.v3.schema.json
Normal file
@ -0,0 +1,367 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "IPython Notebook v3.0 JSON schema.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["metadata", "nbformat_minor", "nbformat", "worksheets"],
|
||||
"properties": {
|
||||
"metadata": {
|
||||
"description": "Notebook root-level metadata.",
|
||||
"type": "object",
|
||||
"additionalProperties": true,
|
||||
"properties": {
|
||||
"kernel_info": {
|
||||
"description": "Kernel information.",
|
||||
"type": "object",
|
||||
"required": ["name", "language"],
|
||||
"properties": {
|
||||
"name": {
|
||||
"description": "Name of the kernel specification.",
|
||||
"type": "string"
|
||||
},
|
||||
"language": {
|
||||
"description": "The programming language which this kernel runs.",
|
||||
"type": "string"
|
||||
},
|
||||
"codemirror_mode": {
|
||||
"description": "The codemirror mode to use for code in this language.",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"signature": {
|
||||
"description": "Hash of the notebook.",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"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": 3,
|
||||
"maximum": 3
|
||||
},
|
||||
"orig_nbformat": {
|
||||
"description": "Original notebook format (major number) before converting the notebook between versions.",
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
},
|
||||
"orig_nbformat_minor": {
|
||||
"description": "Original notebook format (minor number) before converting the notebook between versions.",
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
},
|
||||
"worksheets": {
|
||||
"description": "Array of worksheets",
|
||||
"type": "array",
|
||||
"items": { "$ref": "#/definitions/worksheet" }
|
||||
}
|
||||
},
|
||||
|
||||
"definitions": {
|
||||
"worksheet": {
|
||||
"additionalProperties": false,
|
||||
"required": ["cells"],
|
||||
"properties": {
|
||||
"cells": {
|
||||
"description": "Array of cells of the current notebook.",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"oneOf": [
|
||||
{ "$ref": "#/definitions/raw_cell" },
|
||||
{ "$ref": "#/definitions/markdown_cell" },
|
||||
{ "$ref": "#/definitions/heading_cell" },
|
||||
{ "$ref": "#/definitions/code_cell" }
|
||||
]
|
||||
}
|
||||
},
|
||||
"metadata": {
|
||||
"type": "object",
|
||||
"description": "metadata of the current worksheet"
|
||||
}
|
||||
}
|
||||
},
|
||||
"raw_cell": {
|
||||
"description": "Notebook raw nbconvert cell.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["cell_type", "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" }
|
||||
}
|
||||
},
|
||||
"source": { "$ref": "#/definitions/misc/source" }
|
||||
}
|
||||
},
|
||||
|
||||
"markdown_cell": {
|
||||
"description": "Notebook markdown cell.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["cell_type", "source"],
|
||||
"properties": {
|
||||
"cell_type": {
|
||||
"description": "String identifying the type of cell.",
|
||||
"enum": ["markdown", "html"]
|
||||
},
|
||||
"metadata": {
|
||||
"description": "Cell-level metadata.",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": { "$ref": "#/definitions/misc/metadata_name" },
|
||||
"tags": { "$ref": "#/definitions/misc/metadata_tags" }
|
||||
},
|
||||
"additionalProperties": true
|
||||
},
|
||||
"source": { "$ref": "#/definitions/misc/source" }
|
||||
}
|
||||
},
|
||||
|
||||
"heading_cell": {
|
||||
"description": "Notebook heading cell.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["cell_type", "source", "level"],
|
||||
"properties": {
|
||||
"cell_type": {
|
||||
"description": "String identifying the type of cell.",
|
||||
"enum": ["heading"]
|
||||
},
|
||||
"metadata": {
|
||||
"description": "Cell-level metadata.",
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
},
|
||||
"source": { "$ref": "#/definitions/misc/source" },
|
||||
"level": {
|
||||
"description": "Level of heading cells.",
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"code_cell": {
|
||||
"description": "Notebook code cell.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["cell_type", "input", "outputs", "language"],
|
||||
"properties": {
|
||||
"cell_type": {
|
||||
"description": "String identifying the type of cell.",
|
||||
"enum": ["code"]
|
||||
},
|
||||
"language": {
|
||||
"description": "The cell's language (always Python)",
|
||||
"type": "string"
|
||||
},
|
||||
"collapsed": {
|
||||
"description": "Whether the cell is collapsed/expanded.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"metadata": {
|
||||
"description": "Cell-level metadata.",
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
},
|
||||
"input": { "$ref": "#/definitions/misc/source" },
|
||||
"outputs": {
|
||||
"description": "Execution, display, or stream outputs.",
|
||||
"type": "array",
|
||||
"items": { "$ref": "#/definitions/output" }
|
||||
},
|
||||
"prompt_number": {
|
||||
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
|
||||
"type": ["integer", "null"],
|
||||
"minimum": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
"output": {
|
||||
"type": "object",
|
||||
"oneOf": [
|
||||
{ "$ref": "#/definitions/pyout" },
|
||||
{ "$ref": "#/definitions/display_data" },
|
||||
{ "$ref": "#/definitions/stream" },
|
||||
{ "$ref": "#/definitions/pyerr" }
|
||||
]
|
||||
},
|
||||
"pyout": {
|
||||
"description": "Result of executing a code cell.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["output_type", "prompt_number"],
|
||||
"properties": {
|
||||
"output_type": {
|
||||
"description": "Type of cell output.",
|
||||
"enum": ["pyout"]
|
||||
},
|
||||
"prompt_number": {
|
||||
"description": "A result's prompt number.",
|
||||
"type": ["integer"],
|
||||
"minimum": 0
|
||||
},
|
||||
"text": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"latex": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"png": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"jpeg": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"svg": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"html": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"javascript": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"json": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"pdf": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"metadata": { "$ref": "#/definitions/misc/output_metadata" }
|
||||
},
|
||||
"patternProperties": {
|
||||
"^[a-zA-Z0-9]+/[a-zA-Z0-9\\-\\+\\.]+$": {
|
||||
"description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
|
||||
"$ref": "#/definitions/misc/multiline_string"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"display_data": {
|
||||
"description": "Data displayed as a result of code cell execution.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["output_type"],
|
||||
"properties": {
|
||||
"output_type": {
|
||||
"description": "Type of cell output.",
|
||||
"enum": ["display_data"]
|
||||
},
|
||||
"text": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"latex": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"png": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"jpeg": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"svg": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"html": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"javascript": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"json": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"pdf": { "$ref": "#/definitions/misc/multiline_string" },
|
||||
"metadata": { "$ref": "#/definitions/misc/output_metadata" }
|
||||
},
|
||||
"patternProperties": {
|
||||
"[a-zA-Z0-9]+/[a-zA-Z0-9\\-\\+\\.]+$": {
|
||||
"description": "mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
|
||||
"$ref": "#/definitions/misc/multiline_string"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"stream": {
|
||||
"description": "Stream output from a code cell.",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["output_type", "stream", "text"],
|
||||
"properties": {
|
||||
"output_type": {
|
||||
"description": "Type of cell output.",
|
||||
"enum": ["stream"]
|
||||
},
|
||||
"stream": {
|
||||
"description": "The stream type/destination.",
|
||||
"type": "string"
|
||||
},
|
||||
"text": {
|
||||
"description": "The stream's text output, represented as an array of strings.",
|
||||
"$ref": "#/definitions/misc/multiline_string"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"pyerr": {
|
||||
"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": ["pyerr"]
|
||||
},
|
||||
"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" }
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"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": "^[^,]+$"
|
||||
}
|
||||
},
|
||||
"source": {
|
||||
"description": "Contents of the cell, represented as an array of lines.",
|
||||
"$ref": "#/definitions/misc/multiline_string"
|
||||
},
|
||||
"prompt_number": {
|
||||
"description": "The code cell's prompt number. Will be null if the cell has not been run.",
|
||||
"type": ["integer", "null"],
|
||||
"minimum": 0
|
||||
},
|
||||
"mimetype": {
|
||||
"patternProperties": {
|
||||
"^[a-zA-Z0-9\\-\\+]+/[a-zA-Z0-9\\-\\+]+": {
|
||||
"description": "The cell's mimetype output (e.g. text/plain), represented as either an array of strings or a string.",
|
||||
"$ref": "#/definitions/misc/multiline_string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"output_metadata": {
|
||||
"description": "Cell output metadata.",
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
},
|
||||
"multiline_string": {
|
||||
"oneOf": [
|
||||
{ "type": "string" },
|
||||
{
|
||||
"type": "array",
|
||||
"items": { "type": "string" }
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
59
.venv/Lib/site-packages/nbformat/v3/nbjson.py
Normal file
59
.venv/Lib/site-packages/nbformat/v3/nbjson.py
Normal file
@ -0,0 +1,59 @@
|
||||
"""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 .nbbase 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):
|
||||
nb = json.loads(s, **kwargs)
|
||||
nb = self.to_notebook(nb, **kwargs)
|
||||
nb = strip_transient(nb)
|
||||
return nb
|
||||
|
||||
def to_notebook(self, d, **kwargs):
|
||||
return rejoin_lines(from_dict(d))
|
||||
|
||||
|
||||
class JSONWriter(NotebookWriter):
|
||||
def writes(self, nb, **kwargs):
|
||||
kwargs["cls"] = BytesEncoder
|
||||
kwargs["indent"] = 1
|
||||
kwargs["sort_keys"] = True
|
||||
kwargs["separators"] = (",", ": ")
|
||||
nb = copy.deepcopy(nb)
|
||||
nb = strip_transient(nb)
|
||||
if kwargs.pop("split_lines", True):
|
||||
nb = split_lines(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
|
212
.venv/Lib/site-packages/nbformat/v3/nbpy.py
Normal file
212
.venv/Lib/site-packages/nbformat/v3/nbpy.py
Normal file
@ -0,0 +1,212 @@
|
||||
"""Read and write notebooks as regular .py files.
|
||||
|
||||
Authors:
|
||||
|
||||
* Brian Granger
|
||||
"""
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Copyright (C) 2008-2011 The IPython Development Team
|
||||
#
|
||||
# Distributed under the terms of the BSD License. The full license is in
|
||||
# the file COPYING, distributed as part of this software.
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Imports
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
import re
|
||||
from typing import List
|
||||
|
||||
from .nbbase import (
|
||||
nbformat,
|
||||
nbformat_minor,
|
||||
new_code_cell,
|
||||
new_heading_cell,
|
||||
new_notebook,
|
||||
new_text_cell,
|
||||
new_worksheet,
|
||||
)
|
||||
from .rwbase import NotebookReader, NotebookWriter
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Code
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
_encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)")
|
||||
|
||||
|
||||
class PyReaderError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class PyReader(NotebookReader):
|
||||
def reads(self, s, **kwargs):
|
||||
return self.to_notebook(s, **kwargs)
|
||||
|
||||
def to_notebook(self, s, **kwargs):
|
||||
lines = s.splitlines()
|
||||
cells = []
|
||||
cell_lines: List[str] = []
|
||||
kwargs = {}
|
||||
state = "codecell"
|
||||
for line in lines:
|
||||
if line.startswith("# <nbformat>") or _encoding_declaration_re.match(line):
|
||||
pass
|
||||
elif line.startswith("# <codecell>"):
|
||||
cell = self.new_cell(state, cell_lines, **kwargs)
|
||||
if cell is not None:
|
||||
cells.append(cell)
|
||||
state = "codecell"
|
||||
cell_lines = []
|
||||
kwargs = {}
|
||||
elif line.startswith("# <htmlcell>"):
|
||||
cell = self.new_cell(state, cell_lines, **kwargs)
|
||||
if cell is not None:
|
||||
cells.append(cell)
|
||||
state = "htmlcell"
|
||||
cell_lines = []
|
||||
kwargs = {}
|
||||
elif line.startswith("# <markdowncell>"):
|
||||
cell = self.new_cell(state, cell_lines, **kwargs)
|
||||
if cell is not None:
|
||||
cells.append(cell)
|
||||
state = "markdowncell"
|
||||
cell_lines = []
|
||||
kwargs = {}
|
||||
# VERSIONHACK: plaintext -> raw
|
||||
elif line.startswith("# <rawcell>") or line.startswith("# <plaintextcell>"):
|
||||
cell = self.new_cell(state, cell_lines, **kwargs)
|
||||
if cell is not None:
|
||||
cells.append(cell)
|
||||
state = "rawcell"
|
||||
cell_lines = []
|
||||
kwargs = {}
|
||||
elif line.startswith("# <headingcell"):
|
||||
cell = self.new_cell(state, cell_lines, **kwargs)
|
||||
if cell is not None:
|
||||
cells.append(cell)
|
||||
cell_lines = []
|
||||
m = re.match(r"# <headingcell level=(?P<level>\d)>", line)
|
||||
if m is not None:
|
||||
state = "headingcell"
|
||||
kwargs = {}
|
||||
kwargs["level"] = int(m.group("level"))
|
||||
else:
|
||||
state = "codecell"
|
||||
kwargs = {}
|
||||
cell_lines = []
|
||||
else:
|
||||
cell_lines.append(line)
|
||||
if cell_lines and state == "codecell":
|
||||
cell = self.new_cell(state, cell_lines)
|
||||
if cell is not None:
|
||||
cells.append(cell)
|
||||
ws = new_worksheet(cells=cells)
|
||||
nb = new_notebook(worksheets=[ws])
|
||||
return nb
|
||||
|
||||
def new_cell(self, state, lines, **kwargs):
|
||||
if state == "codecell":
|
||||
input = "\n".join(lines)
|
||||
input = input.strip("\n")
|
||||
if input:
|
||||
return new_code_cell(input=input)
|
||||
elif state == "htmlcell":
|
||||
text = self._remove_comments(lines)
|
||||
if text:
|
||||
return new_text_cell("html", source=text)
|
||||
elif state == "markdowncell":
|
||||
text = self._remove_comments(lines)
|
||||
if text:
|
||||
return new_text_cell("markdown", source=text)
|
||||
elif state == "rawcell":
|
||||
text = self._remove_comments(lines)
|
||||
if text:
|
||||
return new_text_cell("raw", source=text)
|
||||
elif state == "headingcell":
|
||||
text = self._remove_comments(lines)
|
||||
level = kwargs.get("level", 1)
|
||||
if text:
|
||||
return new_heading_cell(source=text, level=level)
|
||||
|
||||
def _remove_comments(self, lines):
|
||||
new_lines = []
|
||||
for line in lines:
|
||||
if line.startswith("#"):
|
||||
new_lines.append(line[2:])
|
||||
else:
|
||||
new_lines.append(line)
|
||||
text = "\n".join(new_lines)
|
||||
text = text.strip("\n")
|
||||
return text
|
||||
|
||||
def split_lines_into_blocks(self, lines):
|
||||
if len(lines) == 1:
|
||||
yield lines[0]
|
||||
raise StopIteration()
|
||||
import ast
|
||||
|
||||
source = "\n".join(lines)
|
||||
code = ast.parse(source)
|
||||
starts = [x.lineno - 1 for x in code.body]
|
||||
for i in range(len(starts) - 1):
|
||||
yield "\n".join(lines[starts[i] : starts[i + 1]]).strip("\n")
|
||||
yield "\n".join(lines[starts[-1] :]).strip("\n")
|
||||
|
||||
|
||||
class PyWriter(NotebookWriter):
|
||||
def writes(self, nb, **kwargs):
|
||||
lines = ["# -*- coding: utf-8 -*-"]
|
||||
lines.extend(
|
||||
[
|
||||
"# <nbformat>%i.%i</nbformat>" % (nbformat, nbformat_minor),
|
||||
"",
|
||||
]
|
||||
)
|
||||
for ws in nb.worksheets:
|
||||
for cell in ws.cells:
|
||||
if cell.cell_type == "code":
|
||||
input = cell.get("input")
|
||||
if input is not None:
|
||||
lines.extend(["# <codecell>", ""])
|
||||
lines.extend(input.splitlines())
|
||||
lines.append("")
|
||||
elif cell.cell_type == "html":
|
||||
input = cell.get("source")
|
||||
if input is not None:
|
||||
lines.extend(["# <htmlcell>", ""])
|
||||
lines.extend(["# " + line for line in input.splitlines()])
|
||||
lines.append("")
|
||||
elif cell.cell_type == "markdown":
|
||||
input = cell.get("source")
|
||||
if input is not None:
|
||||
lines.extend(["# <markdowncell>", ""])
|
||||
lines.extend(["# " + line for line in input.splitlines()])
|
||||
lines.append("")
|
||||
elif cell.cell_type == "raw":
|
||||
input = cell.get("source")
|
||||
if input is not None:
|
||||
lines.extend(["# <rawcell>", ""])
|
||||
lines.extend(["# " + line for line in input.splitlines()])
|
||||
lines.append("")
|
||||
elif cell.cell_type == "heading":
|
||||
input = cell.get("source")
|
||||
level = cell.get("level", 1)
|
||||
if input is not None:
|
||||
lines.extend(["# <headingcell level=%s>" % level, ""])
|
||||
lines.extend(["# " + line for line in input.splitlines()])
|
||||
lines.append("")
|
||||
lines.append("")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
_reader = PyReader()
|
||||
_writer = PyWriter()
|
||||
|
||||
reads = _reader.reads
|
||||
read = _reader.read
|
||||
to_notebook = _reader.to_notebook
|
||||
write = _writer.write
|
||||
writes = _writer.writes
|
183
.venv/Lib/site-packages/nbformat/v3/rwbase.py
Normal file
183
.venv/Lib/site-packages/nbformat/v3/rwbase.py
Normal file
@ -0,0 +1,183 @@
|
||||
"""Base classes and utilities for readers and writers."""
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
|
||||
from base64 import decodebytes, encodebytes
|
||||
|
||||
|
||||
def restore_bytes(nb):
|
||||
"""Restore bytes of image data from unicode-only formats.
|
||||
|
||||
Base64 encoding is handled elsewhere. Bytes objects in the notebook are
|
||||
always b64-encoded. We DO NOT encode/decode around file formats.
|
||||
|
||||
Note: this is never used
|
||||
"""
|
||||
for ws in nb.worksheets:
|
||||
for cell in ws.cells:
|
||||
if cell.cell_type == "code":
|
||||
for output in cell.outputs:
|
||||
if "png" in output:
|
||||
output.png = output.png.encode("ascii", "replace")
|
||||
if "jpeg" in output:
|
||||
output.jpeg = output.jpeg.encode("ascii", "replace")
|
||||
return nb
|
||||
|
||||
|
||||
# output keys that are likely to have multiline values
|
||||
_multiline_outputs = ["text", "html", "svg", "latex", "javascript", "json"]
|
||||
|
||||
|
||||
# FIXME: workaround for old splitlines()
|
||||
def _join_lines(lines):
|
||||
"""join lines that have been written by splitlines()
|
||||
|
||||
Has logic to protect against `splitlines()`, which
|
||||
should have been `splitlines(True)`
|
||||
"""
|
||||
if lines and lines[0].endswith(("\n", "\r")):
|
||||
# created by splitlines(True)
|
||||
return "".join(lines)
|
||||
else:
|
||||
# created by splitlines()
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
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 ws in nb.worksheets:
|
||||
for cell in ws.cells:
|
||||
if cell.cell_type == "code":
|
||||
if "input" in cell and isinstance(cell.input, list):
|
||||
cell.input = _join_lines(cell.input)
|
||||
for output in cell.outputs:
|
||||
for key in _multiline_outputs:
|
||||
item = output.get(key, None)
|
||||
if isinstance(item, list):
|
||||
output[key] = _join_lines(item)
|
||||
else: # text, heading cell
|
||||
for key in ["source", "rendered"]:
|
||||
item = cell.get(key, None)
|
||||
if isinstance(item, list):
|
||||
cell[key] = _join_lines(item)
|
||||
return nb
|
||||
|
||||
|
||||
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 ws in nb.worksheets:
|
||||
for cell in ws.cells:
|
||||
if cell.cell_type == "code":
|
||||
if "input" in cell and isinstance(cell.input, str):
|
||||
cell.input = cell.input.splitlines(True)
|
||||
for output in cell.outputs:
|
||||
for key in _multiline_outputs:
|
||||
item = output.get(key, None)
|
||||
if isinstance(item, str):
|
||||
output[key] = item.splitlines(True)
|
||||
else: # text, heading cell
|
||||
for key in ["source", "rendered"]:
|
||||
item = cell.get(key, None)
|
||||
if isinstance(item, str):
|
||||
cell[key] = item.splitlines(True)
|
||||
return nb
|
||||
|
||||
|
||||
# b64 encode/decode are never actually used, because all bytes objects in
|
||||
# the notebook are already b64-encoded, and we don't need/want to double-encode
|
||||
|
||||
|
||||
def base64_decode(nb):
|
||||
"""Restore all bytes objects in the notebook from base64-encoded strings.
|
||||
|
||||
Note: This is never used
|
||||
"""
|
||||
for ws in nb.worksheets:
|
||||
for cell in ws.cells:
|
||||
if cell.cell_type == "code":
|
||||
for output in cell.outputs:
|
||||
if "png" in output:
|
||||
if isinstance(output.png, str):
|
||||
output.png = output.png.encode("ascii")
|
||||
output.png = decodebytes(output.png)
|
||||
if "jpeg" in output:
|
||||
if isinstance(output.jpeg, str):
|
||||
output.jpeg = output.jpeg.encode("ascii")
|
||||
output.jpeg = decodebytes(output.jpeg)
|
||||
return nb
|
||||
|
||||
|
||||
def base64_encode(nb):
|
||||
"""Base64 encode all bytes objects in the notebook.
|
||||
|
||||
These will be b64-encoded unicode strings
|
||||
|
||||
Note: This is never used
|
||||
"""
|
||||
for ws in nb.worksheets:
|
||||
for cell in ws.cells:
|
||||
if cell.cell_type == "code":
|
||||
for output in cell.outputs:
|
||||
if "png" in output:
|
||||
output.png = encodebytes(output.png).decode("ascii")
|
||||
if "jpeg" in output:
|
||||
output.jpeg = encodebytes(output.jpeg).decode("ascii")
|
||||
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.pop("orig_nbformat", None)
|
||||
nb.pop("orig_nbformat_minor", None)
|
||||
for ws in nb["worksheets"]:
|
||||
for cell in ws["cells"]:
|
||||
cell.get("metadata", {}).pop("trusted", None)
|
||||
# strip cell.trusted even though it shouldn't be used,
|
||||
# since it's where the transient value used to be stored.
|
||||
cell.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("loads 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("loads 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)
|
Reference in New Issue
Block a user