nbbase.py
137 lines
| 3.6 KiB
| text/x-python
|
PythonLexer
MinRK
|
r18575 | """Python API for composing notebook elements | ||
MinRK
|
r18568 | |||
The Python representation of a notebook is a nested structure of | ||||
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. | ||||
""" | ||||
# Copyright (c) IPython Development Team. | ||||
# Distributed under the terms of the Modified BSD License. | ||||
MinRK
|
r18606 | from ..notebooknode import from_dict, NotebookNode | ||
MinRK
|
r18568 | |||
# Change this when incrementing the nbformat version | ||||
MinRK
|
r18573 | nbformat = 4 | ||
MinRK
|
r18568 | nbformat_minor = 0 | ||
MinRK
|
r18574 | nbformat_schema = 'nbformat.v4.schema.json' | ||
MinRK
|
r18568 | |||
MinRK
|
r18582 | |||
MinRK
|
r18575 | def validate(node, ref=None): | ||
"""validate a v4 node""" | ||||
MinRK
|
r18603 | from .. import validate | ||
MinRK
|
r18575 | return validate(node, ref=ref, version=nbformat) | ||
MinRK
|
r18582 | |||
MinRK
|
r18589 | def new_output(output_type, data=None, **kwargs): | ||
MinRK
|
r18575 | """Create a new output, to go in the ``cell.outputs`` list of a code cell.""" | ||
MinRK
|
r18580 | output = NotebookNode(output_type=output_type) | ||
MinRK
|
r18589 | |||
MinRK
|
r18575 | # populate defaults: | ||
if output_type == 'stream': | ||||
MinRK
|
r18602 | output.name = u'stdout' | ||
output.text = u'' | ||||
MinRK
|
r18589 | elif output_type in {'execute_result', 'display_data'}: | ||
MinRK
|
r18602 | output.metadata = NotebookNode() | ||
output.data = NotebookNode() | ||||
# load from args: | ||||
output.update(from_dict(kwargs)) | ||||
if data is not None: | ||||
output.data = from_dict(data) | ||||
# validate | ||||
MinRK
|
r18575 | validate(output, output_type) | ||
return output | ||||
MinRK
|
r18582 | |||
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'], | ||||
MinRK
|
r18589 | data=content['data'], | ||
MinRK
|
r18587 | execution_count=content['execution_count'], | ||
MinRK
|
r18582 | ) | ||
elif msg_type == 'stream': | ||||
return new_output(output_type=msg_type, | ||||
name=content['name'], | ||||
MinRK
|
r18588 | text=content['text'], | ||
MinRK
|
r18582 | ) | ||
elif msg_type == 'display_data': | ||||
return new_output(output_type=msg_type, | ||||
metadata=content['metadata'], | ||||
MinRK
|
r18589 | data=content['data'], | ||
MinRK
|
r18582 | ) | ||
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) | ||||
MinRK
|
r18575 | def new_code_cell(source='', **kwargs): | ||
"""Create a new code cell""" | ||||
MinRK
|
r18602 | cell = NotebookNode( | ||
cell_type='code', | ||||
metadata=NotebookNode(), | ||||
execution_count=None, | ||||
source=source, | ||||
outputs=[], | ||||
) | ||||
MinRK
|
r18580 | cell.update(from_dict(kwargs)) | ||
MinRK
|
r18575 | |||
validate(cell, 'code_cell') | ||||
return cell | ||||
def new_markdown_cell(source='', **kwargs): | ||||
"""Create a new markdown cell""" | ||||
MinRK
|
r18602 | cell = NotebookNode( | ||
cell_type='markdown', | ||||
source=source, | ||||
metadata=NotebookNode(), | ||||
) | ||||
MinRK
|
r18580 | cell.update(from_dict(kwargs)) | ||
MinRK
|
r18575 | |||
validate(cell, 'markdown_cell') | ||||
return cell | ||||
def new_raw_cell(source='', **kwargs): | ||||
"""Create a new raw cell""" | ||||
MinRK
|
r18602 | cell = NotebookNode( | ||
cell_type='raw', | ||||
source=source, | ||||
metadata=NotebookNode(), | ||||
) | ||||
MinRK
|
r18580 | cell.update(from_dict(kwargs)) | ||
MinRK
|
r18575 | |||
validate(cell, 'raw_cell') | ||||
return cell | ||||
def new_notebook(**kwargs): | ||||
"""Create a new notebook""" | ||||
MinRK
|
r18602 | nb = NotebookNode( | ||
nbformat=nbformat, | ||||
nbformat_minor=nbformat_minor, | ||||
metadata=NotebookNode(), | ||||
cells=[], | ||||
) | ||||
nb.update(from_dict(kwargs)) | ||||
MinRK
|
r18575 | validate(nb) | ||
return nb | ||||