nbbase.py
204 lines
| 6.5 KiB
| text/x-python
|
PythonLexer
Brian E. Granger
|
r4609 | """The basic dict based notebook format. | ||
Bernardo B. Marques
|
r4872 | The Python representation of a notebook is a nested structure of | ||
Brian E. Granger
|
r4609 | dictionary subclasses that support attribute access | ||
(IPython.utils.ipstruct.Struct). The functions in this module are merely | ||||
helpers to build the structs in the right form. | ||||
""" | ||||
MinRK
|
r16235 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Brian E. Granger
|
r4392 | |||
Brian E. Granger
|
r4401 | import pprint | ||
Brian E. Granger
|
r4392 | import uuid | ||
Brian E. Granger
|
r4401 | from IPython.utils.ipstruct import Struct | ||
Thomas Kluyver
|
r13353 | from IPython.utils.py3compat import cast_unicode, unicode_type | ||
Brian E. Granger
|
r4401 | |||
Brian E. Granger
|
r4609 | #----------------------------------------------------------------------------- | ||
# Code | ||||
#----------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r4401 | |||
Brian Granger
|
r6048 | # Change this when incrementing the nbformat version | ||
MinRK
|
r7545 | nbformat = 3 | ||
nbformat_minor = 0 | ||||
Brian Granger
|
r6048 | |||
Brian E. Granger
|
r4401 | class NotebookNode(Struct): | ||
pass | ||||
Brian E. Granger
|
r4392 | |||
Brian E. Granger
|
r4406 | 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 | ||||
MinRK
|
r16235 | def new_output(output_type, output_text=None, output_png=None, | ||
Bernardo B. Marques
|
r4872 | output_html=None, output_svg=None, output_latex=None, output_json=None, | ||
Brian E. Granger
|
r4540 | output_javascript=None, output_jpeg=None, prompt_number=None, | ||
MinRK
|
r10466 | ename=None, evalue=None, traceback=None, stream=None, metadata=None): | ||
Thomas Kluyver
|
r13919 | """Create a new output, to go in the ``cell.outputs`` list of a code cell. | ||
""" | ||||
Brian E. Granger
|
r4401 | output = NotebookNode() | ||
MinRK
|
r16235 | output.output_type = unicode_type(output_type) | ||
Brian E. Granger
|
r4540 | |||
MinRK
|
r10466 | if metadata is None: | ||
metadata = {} | ||||
if not isinstance(metadata, dict): | ||||
raise TypeError("metadata must be dict") | ||||
output.metadata = metadata | ||||
Brian E. Granger
|
r4540 | if output_type != 'pyerr': | ||
if output_text is not None: | ||||
MinRK
|
r12389 | output.text = cast_unicode(output_text) | ||
Brian E. Granger
|
r4540 | if output_png is not None: | ||
MinRK
|
r12389 | output.png = cast_unicode(output_png) | ||
Brian E. Granger
|
r4540 | if output_jpeg is not None: | ||
MinRK
|
r12389 | output.jpeg = cast_unicode(output_jpeg) | ||
Brian E. Granger
|
r4540 | if output_html is not None: | ||
MinRK
|
r12389 | output.html = cast_unicode(output_html) | ||
Brian E. Granger
|
r4540 | if output_svg is not None: | ||
MinRK
|
r12389 | output.svg = cast_unicode(output_svg) | ||
Brian E. Granger
|
r4540 | if output_latex is not None: | ||
MinRK
|
r12389 | output.latex = cast_unicode(output_latex) | ||
Brian E. Granger
|
r4540 | if output_json is not None: | ||
MinRK
|
r12389 | output.json = cast_unicode(output_json) | ||
Brian E. Granger
|
r4540 | if output_javascript is not None: | ||
MinRK
|
r12389 | output.javascript = cast_unicode(output_javascript) | ||
Brian E. Granger
|
r4540 | |||
if output_type == u'pyout': | ||||
if prompt_number is not None: | ||||
output.prompt_number = int(prompt_number) | ||||
if output_type == u'pyerr': | ||||
Maxim Grechkin
|
r10233 | if ename is not None: | ||
MinRK
|
r12389 | output.ename = cast_unicode(ename) | ||
Brian E. Granger
|
r4540 | if evalue is not None: | ||
MinRK
|
r12389 | output.evalue = cast_unicode(evalue) | ||
Brian E. Granger
|
r4540 | if traceback is not None: | ||
MinRK
|
r12389 | output.traceback = [cast_unicode(frame) for frame in list(traceback)] | ||
Brian E. Granger
|
r4540 | |||
Paul Ivanov
|
r10183 | if output_type == u'stream': | ||
MinRK
|
r12389 | output.stream = 'stdout' if stream is None else cast_unicode(stream) | ||
MinRK
|
r10466 | |||
Brian E. Granger
|
r4402 | return output | ||
Brian E. Granger
|
r4392 | |||
Brian E. Granger
|
r4533 | def new_code_cell(input=None, prompt_number=None, outputs=None, | ||
MinRK
|
r7523 | language=u'python', collapsed=False, metadata=None): | ||
Brian E. Granger
|
r4402 | """Create a new code cell with input and output""" | ||
cell = NotebookNode() | ||||
cell.cell_type = u'code' | ||||
if language is not None: | ||||
MinRK
|
r12389 | cell.language = cast_unicode(language) | ||
Brian E. Granger
|
r4402 | if input is not None: | ||
MinRK
|
r12389 | cell.input = cast_unicode(input) | ||
Brian E. Granger
|
r4402 | if prompt_number is not None: | ||
cell.prompt_number = int(prompt_number) | ||||
if outputs is None: | ||||
cell.outputs = [] | ||||
else: | ||||
cell.outputs = outputs | ||||
Brian E. Granger
|
r4533 | if collapsed is not None: | ||
Brian E. Granger
|
r4536 | cell.collapsed = bool(collapsed) | ||
MinRK
|
r7523 | cell.metadata = NotebookNode(metadata or {}) | ||
Brian E. Granger
|
r4402 | |||
return cell | ||||
MinRK
|
r7523 | def new_text_cell(cell_type, source=None, rendered=None, metadata=None): | ||
Brian E. Granger
|
r4392 | """Create a new text cell.""" | ||
Brian E. Granger
|
r4401 | cell = NotebookNode() | ||
MinRK
|
r6477 | # VERSIONHACK: plaintext -> raw | ||
# handle never-released plaintext name for raw cells | ||||
MinRK
|
r6255 | if cell_type == 'plaintext': | ||
cell_type = 'raw' | ||||
Brian E. Granger
|
r4498 | if source is not None: | ||
MinRK
|
r12389 | cell.source = cast_unicode(source) | ||
Brian E. Granger
|
r4511 | if rendered is not None: | ||
MinRK
|
r12389 | cell.rendered = cast_unicode(rendered) | ||
MinRK
|
r7523 | cell.metadata = NotebookNode(metadata or {}) | ||
Brian E. Granger
|
r4511 | cell.cell_type = cell_type | ||
Brian E. Granger
|
r4392 | return cell | ||
MinRK
|
r7523 | def new_heading_cell(source=None, rendered=None, level=1, metadata=None): | ||
Brian Granger
|
r6016 | """Create a new section cell with a given integer level.""" | ||
cell = NotebookNode() | ||||
cell.cell_type = u'heading' | ||||
if source is not None: | ||||
MinRK
|
r12389 | cell.source = cast_unicode(source) | ||
Brian Granger
|
r6020 | if rendered is not None: | ||
MinRK
|
r12389 | cell.rendered = cast_unicode(rendered) | ||
Brian Granger
|
r6016 | cell.level = int(level) | ||
MinRK
|
r7523 | cell.metadata = NotebookNode(metadata or {}) | ||
Brian Granger
|
r6016 | return cell | ||
MinRK
|
r7523 | def new_worksheet(name=None, cells=None, metadata=None): | ||
Brian E. Granger
|
r4392 | """Create a worksheet by name with with a list of cells.""" | ||
Brian E. Granger
|
r4401 | ws = NotebookNode() | ||
Brian E. Granger
|
r4392 | if name is not None: | ||
MinRK
|
r12389 | ws.name = cast_unicode(name) | ||
Brian E. Granger
|
r4392 | if cells is None: | ||
Brian E. Granger
|
r4401 | ws.cells = [] | ||
Brian E. Granger
|
r4392 | else: | ||
Brian E. Granger
|
r4401 | ws.cells = list(cells) | ||
MinRK
|
r7523 | ws.metadata = NotebookNode(metadata or {}) | ||
Brian E. Granger
|
r4392 | return ws | ||
MinRK
|
r6207 | def new_notebook(name=None, metadata=None, worksheets=None): | ||
Brian E. Granger
|
r4392 | """Create a notebook by name, id and a list of worksheets.""" | ||
Brian E. Granger
|
r4401 | nb = NotebookNode() | ||
Brian Granger
|
r6048 | nb.nbformat = nbformat | ||
MinRK
|
r7545 | nb.nbformat_minor = nbformat_minor | ||
Brian E. Granger
|
r4392 | if worksheets is None: | ||
Brian E. Granger
|
r4401 | nb.worksheets = [] | ||
Brian E. Granger
|
r4392 | else: | ||
Brian E. Granger
|
r4401 | nb.worksheets = list(worksheets) | ||
Brian E. Granger
|
r4637 | if metadata is None: | ||
nb.metadata = new_metadata() | ||||
else: | ||||
nb.metadata = NotebookNode(metadata) | ||||
MinRK
|
r6207 | if name is not None: | ||
MinRK
|
r12389 | nb.metadata.name = cast_unicode(name) | ||
Brian E. Granger
|
r4637 | 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: | ||||
MinRK
|
r12389 | metadata.name = cast_unicode(name) | ||
Brian E. Granger
|
r4637 | if authors is not None: | ||
metadata.authors = list(authors) | ||||
Brian E. Granger
|
r4536 | if created is not None: | ||
MinRK
|
r12389 | metadata.created = cast_unicode(created) | ||
Brian E. Granger
|
r4637 | if modified is not None: | ||
MinRK
|
r12389 | metadata.modified = cast_unicode(modified) | ||
Brian E. Granger
|
r4536 | if license is not None: | ||
MinRK
|
r12389 | metadata.license = cast_unicode(license) | ||
Brian E. Granger
|
r4637 | if gistid is not None: | ||
MinRK
|
r12389 | metadata.gistid = cast_unicode(gistid) | ||
Brian E. Granger
|
r4637 | return metadata | ||
def new_author(name=None, email=None, affiliation=None, url=None): | ||||
"""Create a new author.""" | ||||
author = NotebookNode() | ||||
if name is not None: | ||||
MinRK
|
r12389 | author.name = cast_unicode(name) | ||
Brian E. Granger
|
r4637 | if email is not None: | ||
MinRK
|
r12389 | author.email = cast_unicode(email) | ||
Brian E. Granger
|
r4637 | if affiliation is not None: | ||
MinRK
|
r12389 | author.affiliation = cast_unicode(affiliation) | ||
Brian E. Granger
|
r4637 | if url is not None: | ||
MinRK
|
r12389 | author.url = cast_unicode(url) | ||
Brian E. Granger
|
r4637 | return author | ||
Brian E. Granger
|
r4392 | |||