##// END OF EJS Templates
Merge pull request #6045 from minrk/nbformat4...
Merge pull request #6045 from minrk/nbformat4 nbformat v4

File last commit:

r18606:776d1fa8
r18617:482c7bd6 merge
Show More
nbbase.py
137 lines | 3.6 KiB | text/x-python | PythonLexer
MinRK
no longer need separate v4.compose...
r18575 """Python API for composing notebook elements
MinRK
copy nbformat.v3 to v4
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
move NotebookNode to top-level...
r18606 from ..notebooknode import from_dict, NotebookNode
MinRK
copy nbformat.v3 to v4
r18568
# Change this when incrementing the nbformat version
MinRK
first complete pass on v4...
r18573 nbformat = 4
MinRK
copy nbformat.v3 to v4
r18568 nbformat_minor = 0
MinRK
Use Draft4 JSON Schema for v4
r18574 nbformat_schema = 'nbformat.v4.schema.json'
MinRK
copy nbformat.v3 to v4
r18568
MinRK
add nbformat.output_from_msg...
r18582
MinRK
no longer need separate v4.compose...
r18575 def validate(node, ref=None):
"""validate a v4 node"""
MinRK
Add top-level IPython.nbformat API...
r18603 from .. import validate
MinRK
no longer need separate v4.compose...
r18575 return validate(node, ref=ref, version=nbformat)
MinRK
add nbformat.output_from_msg...
r18582
MinRK
move mime-bundle data to rich output.data...
r18589 def new_output(output_type, data=None, **kwargs):
MinRK
no longer need separate v4.compose...
r18575 """Create a new output, to go in the ``cell.outputs`` list of a code cell."""
MinRK
update nbconvert to nbformat 4
r18580 output = NotebookNode(output_type=output_type)
MinRK
move mime-bundle data to rich output.data...
r18589
MinRK
no longer need separate v4.compose...
r18575 # populate defaults:
if output_type == 'stream':
MinRK
address review from takluyver...
r18602 output.name = u'stdout'
output.text = u''
MinRK
move mime-bundle data to rich output.data...
r18589 elif output_type in {'execute_result', 'display_data'}:
MinRK
address review from takluyver...
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
no longer need separate v4.compose...
r18575 validate(output, output_type)
return output
MinRK
add nbformat.output_from_msg...
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
move mime-bundle data to rich output.data...
r18589 data=content['data'],
MinRK
s/prompt_number/execution_count in nbformat 4
r18587 execution_count=content['execution_count'],
MinRK
add nbformat.output_from_msg...
r18582 )
elif msg_type == 'stream':
return new_output(output_type=msg_type,
name=content['name'],
MinRK
msgspec: stream.data -> stream.text
r18588 text=content['text'],
MinRK
add nbformat.output_from_msg...
r18582 )
elif msg_type == 'display_data':
return new_output(output_type=msg_type,
metadata=content['metadata'],
MinRK
move mime-bundle data to rich output.data...
r18589 data=content['data'],
MinRK
add nbformat.output_from_msg...
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
no longer need separate v4.compose...
r18575 def new_code_cell(source='', **kwargs):
"""Create a new code cell"""
MinRK
address review from takluyver...
r18602 cell = NotebookNode(
cell_type='code',
metadata=NotebookNode(),
execution_count=None,
source=source,
outputs=[],
)
MinRK
update nbconvert to nbformat 4
r18580 cell.update(from_dict(kwargs))
MinRK
no longer need separate v4.compose...
r18575
validate(cell, 'code_cell')
return cell
def new_markdown_cell(source='', **kwargs):
"""Create a new markdown cell"""
MinRK
address review from takluyver...
r18602 cell = NotebookNode(
cell_type='markdown',
source=source,
metadata=NotebookNode(),
)
MinRK
update nbconvert to nbformat 4
r18580 cell.update(from_dict(kwargs))
MinRK
no longer need separate v4.compose...
r18575
validate(cell, 'markdown_cell')
return cell
def new_raw_cell(source='', **kwargs):
"""Create a new raw cell"""
MinRK
address review from takluyver...
r18602 cell = NotebookNode(
cell_type='raw',
source=source,
metadata=NotebookNode(),
)
MinRK
update nbconvert to nbformat 4
r18580 cell.update(from_dict(kwargs))
MinRK
no longer need separate v4.compose...
r18575
validate(cell, 'raw_cell')
return cell
def new_notebook(**kwargs):
"""Create a new notebook"""
MinRK
address review from takluyver...
r18602 nb = NotebookNode(
nbformat=nbformat,
nbformat_minor=nbformat_minor,
metadata=NotebookNode(),
cells=[],
)
nb.update(from_dict(kwargs))
MinRK
no longer need separate v4.compose...
r18575 validate(nb)
return nb