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,20 @@
"""The main module for the v1 notebook format."""
# -----------------------------------------------------------------------------
# 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
# -----------------------------------------------------------------------------
from .convert import upgrade
from .nbbase import NotebookNode, new_code_cell, new_notebook, new_text_cell
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

View File

@ -0,0 +1,16 @@
"""Convert notebook to the v1 format."""
# -----------------------------------------------------------------------------
# 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.
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Code
# -----------------------------------------------------------------------------
def upgrade(nb, orig_version=None):
raise ValueError("Cannot convert to v1 notebook format")

View File

@ -0,0 +1,69 @@
"""The basic dict based notebook format.
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
# -----------------------------------------------------------------------------
from .._struct import Struct
# -----------------------------------------------------------------------------
# Code
# -----------------------------------------------------------------------------
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 new_code_cell(code=None, prompt_number=None):
"""Create a new code cell with input and output"""
cell = NotebookNode()
cell.cell_type = "code"
if code is not None:
cell.code = str(code)
if prompt_number is not None:
cell.prompt_number = int(prompt_number)
return cell
def new_text_cell(text=None):
"""Create a new text cell."""
cell = NotebookNode()
if text is not None:
cell.text = str(text)
cell.cell_type = "text"
return cell
def new_notebook(cells=None):
"""Create a notebook by name, id and a list of worksheets."""
nb = NotebookNode()
if cells is not None:
nb.cells = cells
else:
nb.cells = []
return nb

View File

@ -0,0 +1,52 @@
"""Read and write notebooks in JSON format.
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 json
from .nbbase import from_dict
from .rwbase import NotebookReader, NotebookWriter
# -----------------------------------------------------------------------------
# Code
# -----------------------------------------------------------------------------
class JSONReader(NotebookReader):
def reads(self, s, **kwargs):
nb = json.loads(s, **kwargs)
return self.to_notebook(nb, **kwargs)
def to_notebook(self, d, **kwargs):
"""Convert from a raw JSON dict to a nested NotebookNode structure."""
return from_dict(d)
class JSONWriter(NotebookWriter):
def writes(self, nb, **kwargs):
kwargs["indent"] = 4
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,41 @@
"""Base classes and function for readers and writers.
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
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Code
# -----------------------------------------------------------------------------
class NotebookReader:
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"""
return self.reads(fp.read(), **kwargs)
class NotebookWriter:
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"""
return fp.write(self.writes(nb, **kwargs))