##// END OF EJS Templates
use \0 instead of 'wake up' to finish output...
use \0 instead of 'wake up' to finish output since we are now seeing the captured output, we don't need `wake up` at the end of all of our test outputs.

File last commit:

r18590:be819bae
r20318:d5648dc2
Show More
nbbase.py
202 lines | 6.3 KiB | text/x-python | PythonLexer
Brian E. Granger
More review changes....
r4609 """The basic dict based notebook format.
Bernardo B. Marques
remove all trailling spaces
r4872 The Python representation of a notebook is a nested structure of
Brian E. Granger
More review changes....
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
output_type should not be optional in new_output...
r16214 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian E. Granger
Adding nbformat subpackage.
r4392
Brian E. Granger
Initial draft of more formal notebook format....
r4401 import pprint
Brian E. Granger
Adding nbformat subpackage.
r4392 import uuid
Brian E. Granger
Initial draft of more formal notebook format....
r4401 from IPython.utils.ipstruct import Struct
Thomas Kluyver
Replace references to unicode and basestring
r13353 from IPython.utils.py3compat import cast_unicode, unicode_type
Brian E. Granger
Initial draft of more formal notebook format....
r4401
Brian E. Granger
More review changes....
r4609 #-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
Brian E. Granger
Initial draft of more formal notebook format....
r4401
Brian Granger
Fixing minor issues with nbformat....
r6048 # Change this when incrementing the nbformat version
MinRK
add nbformat_minor for minor revisions to nbformat
r7545 nbformat = 3
nbformat_minor = 0
MinRK
Use Draft4 JSON Schema for both v3 and v4...
r18243 nbformat_schema = 'nbformat.v3.schema.json'
Brian Granger
Fixing minor issues with nbformat....
r6048
Brian E. Granger
Initial draft of more formal notebook format....
r4401 class NotebookNode(Struct):
pass
Brian E. Granger
Adding nbformat subpackage.
r4392
Brian E. Granger
Full versioning added to nbformat.
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
output_type should not be optional in new_output...
r16214 def new_output(output_type, output_text=None, output_png=None,
Bernardo B. Marques
remove all trailling spaces
r4872 output_html=None, output_svg=None, output_latex=None, output_json=None,
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 output_javascript=None, output_jpeg=None, prompt_number=None,
MinRK
add metadata to nbformat Python API
r10466 ename=None, evalue=None, traceback=None, stream=None, metadata=None):
Thomas Kluyver
Serve nbconvert output as zip when it has multiple files
r13919 """Create a new output, to go in the ``cell.outputs`` list of a code cell.
"""
Brian E. Granger
Initial draft of more formal notebook format....
r4401 output = NotebookNode()
MinRK
output_type should not be optional in new_output...
r16214 output.output_type = unicode_type(output_type)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540
MinRK
add metadata to nbformat Python API
r10466 if metadata is None:
metadata = {}
if not isinstance(metadata, dict):
raise TypeError("metadata must be dict")
MinRK
only rich display outputs have metadata...
r18590
if output_type in {u'pyout', 'display_data'}:
output.metadata = metadata
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if output_type != 'pyerr':
if output_text is not None:
MinRK
use cast_unicode in nbformat
r12389 output.text = cast_unicode(output_text)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if output_png is not None:
MinRK
use cast_unicode in nbformat
r12389 output.png = cast_unicode(output_png)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if output_jpeg is not None:
MinRK
use cast_unicode in nbformat
r12389 output.jpeg = cast_unicode(output_jpeg)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if output_html is not None:
MinRK
use cast_unicode in nbformat
r12389 output.html = cast_unicode(output_html)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if output_svg is not None:
MinRK
use cast_unicode in nbformat
r12389 output.svg = cast_unicode(output_svg)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if output_latex is not None:
MinRK
use cast_unicode in nbformat
r12389 output.latex = cast_unicode(output_latex)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if output_json is not None:
MinRK
use cast_unicode in nbformat
r12389 output.json = cast_unicode(output_json)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if output_javascript is not None:
MinRK
use cast_unicode in nbformat
r12389 output.javascript = cast_unicode(output_javascript)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
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
Notebook actually gets and holds ename, not etype
r10233 if ename is not None:
MinRK
use cast_unicode in nbformat
r12389 output.ename = cast_unicode(ename)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if evalue is not None:
MinRK
use cast_unicode in nbformat
r12389 output.evalue = cast_unicode(evalue)
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if traceback is not None:
MinRK
use cast_unicode in nbformat
r12389 output.traceback = [cast_unicode(frame) for frame in list(traceback)]
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540
Paul Ivanov
adding stream kwarg to current.new_output...
r10183 if output_type == u'stream':
MinRK
use cast_unicode in nbformat
r12389 output.stream = 'stdout' if stream is None else cast_unicode(stream)
MinRK
add metadata to nbformat Python API
r10466
Brian E. Granger
Updates to basic notebook format....
r4402 return output
Brian E. Granger
Adding nbformat subpackage.
r4392
Brian E. Granger
Added collapsed field to the code cell.
r4533 def new_code_cell(input=None, prompt_number=None, outputs=None,
MinRK
add empty metadata field on cells/worksheets...
r7523 language=u'python', collapsed=False, metadata=None):
Brian E. Granger
Updates to basic notebook format....
r4402 """Create a new code cell with input and output"""
cell = NotebookNode()
cell.cell_type = u'code'
if language is not None:
MinRK
use cast_unicode in nbformat
r12389 cell.language = cast_unicode(language)
Brian E. Granger
Updates to basic notebook format....
r4402 if input is not None:
MinRK
use cast_unicode in nbformat
r12389 cell.input = cast_unicode(input)
Brian E. Granger
Updates to basic notebook format....
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
Added collapsed field to the code cell.
r4533 if collapsed is not None:
Brian E. Granger
New .py notebook format implemented.
r4536 cell.collapsed = bool(collapsed)
MinRK
add empty metadata field on cells/worksheets...
r7523 cell.metadata = NotebookNode(metadata or {})
Brian E. Granger
Updates to basic notebook format....
r4402
return cell
MinRK
add empty metadata field on cells/worksheets...
r7523 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
Brian E. Granger
Adding nbformat subpackage.
r4392 """Create a new text cell."""
Brian E. Granger
Initial draft of more formal notebook format....
r4401 cell = NotebookNode()
MinRK
add VERSIONHACK markers for never-released plaintext handling
r6477 # VERSIONHACK: plaintext -> raw
# handle never-released plaintext name for raw cells
MinRK
interpret 'plaintext' cells with their new name 'raw'
r6255 if cell_type == 'plaintext':
cell_type = 'raw'
Brian E. Granger
Starting to rename text cell to html cell.
r4498 if source is not None:
MinRK
use cast_unicode in nbformat
r12389 cell.source = cast_unicode(source)
MinRK
add empty metadata field on cells/worksheets...
r7523 cell.metadata = NotebookNode(metadata or {})
Brian E. Granger
Markdown cells are now saved and restored in notebooks.
r4511 cell.cell_type = cell_type
Brian E. Granger
Adding nbformat subpackage.
r4392 return cell
MinRK
fix some validation bugs in v3...
r18244 def new_heading_cell(source=None, level=1, rendered=None, metadata=None):
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 """Create a new section cell with a given integer level."""
cell = NotebookNode()
cell.cell_type = u'heading'
if source is not None:
MinRK
use cast_unicode in nbformat
r12389 cell.source = cast_unicode(source)
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 cell.level = int(level)
MinRK
add empty metadata field on cells/worksheets...
r7523 cell.metadata = NotebookNode(metadata or {})
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 return cell
MinRK
add empty metadata field on cells/worksheets...
r7523 def new_worksheet(name=None, cells=None, metadata=None):
Brian E. Granger
Adding nbformat subpackage.
r4392 """Create a worksheet by name with with a list of cells."""
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ws = NotebookNode()
Brian E. Granger
Adding nbformat subpackage.
r4392 if cells is None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ws.cells = []
Brian E. Granger
Adding nbformat subpackage.
r4392 else:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ws.cells = list(cells)
MinRK
add empty metadata field on cells/worksheets...
r7523 ws.metadata = NotebookNode(metadata or {})
Brian E. Granger
Adding nbformat subpackage.
r4392 return ws
MinRK
allow name as kwarg to new_notebook...
r6207 def new_notebook(name=None, metadata=None, worksheets=None):
Brian E. Granger
Adding nbformat subpackage.
r4392 """Create a notebook by name, id and a list of worksheets."""
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb = NotebookNode()
Brian Granger
Fixing minor issues with nbformat....
r6048 nb.nbformat = nbformat
MinRK
add nbformat_minor for minor revisions to nbformat
r7545 nb.nbformat_minor = nbformat_minor
Brian E. Granger
Adding nbformat subpackage.
r4392 if worksheets is None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb.worksheets = []
Brian E. Granger
Adding nbformat subpackage.
r4392 else:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb.worksheets = list(worksheets)
Brian E. Granger
Implemented metadata for notebook format.
r4637 if metadata is None:
nb.metadata = new_metadata()
else:
nb.metadata = NotebookNode(metadata)
MinRK
allow name as kwarg to new_notebook...
r6207 if name is not None:
MinRK
use cast_unicode in nbformat
r12389 nb.metadata.name = cast_unicode(name)
Brian E. Granger
Implemented metadata for notebook format.
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
use cast_unicode in nbformat
r12389 metadata.name = cast_unicode(name)
Brian E. Granger
Implemented metadata for notebook format.
r4637 if authors is not None:
metadata.authors = list(authors)
Brian E. Granger
New .py notebook format implemented.
r4536 if created is not None:
MinRK
use cast_unicode in nbformat
r12389 metadata.created = cast_unicode(created)
Brian E. Granger
Implemented metadata for notebook format.
r4637 if modified is not None:
MinRK
use cast_unicode in nbformat
r12389 metadata.modified = cast_unicode(modified)
Brian E. Granger
New .py notebook format implemented.
r4536 if license is not None:
MinRK
use cast_unicode in nbformat
r12389 metadata.license = cast_unicode(license)
Brian E. Granger
Implemented metadata for notebook format.
r4637 if gistid is not None:
MinRK
use cast_unicode in nbformat
r12389 metadata.gistid = cast_unicode(gistid)
Brian E. Granger
Implemented metadata for notebook format.
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
use cast_unicode in nbformat
r12389 author.name = cast_unicode(name)
Brian E. Granger
Implemented metadata for notebook format.
r4637 if email is not None:
MinRK
use cast_unicode in nbformat
r12389 author.email = cast_unicode(email)
Brian E. Granger
Implemented metadata for notebook format.
r4637 if affiliation is not None:
MinRK
use cast_unicode in nbformat
r12389 author.affiliation = cast_unicode(affiliation)
Brian E. Granger
Implemented metadata for notebook format.
r4637 if url is not None:
MinRK
use cast_unicode in nbformat
r12389 author.url = cast_unicode(url)
Brian E. Granger
Implemented metadata for notebook format.
r4637 return author
Brian E. Granger
Adding nbformat subpackage.
r4392