Show More
@@ -13,7 +13,7 b' except ImportError:' | |||||
13 |
|
13 | |||
14 | from IPython.utils.traitlets import List, Unicode |
|
14 | from IPython.utils.traitlets import List, Unicode | |
15 |
|
15 | |||
16 |
from IPython.nbformat.current import reads, writes, |
|
16 | from IPython.nbformat.current import reads, writes, output_from_msg | |
17 | from .base import Preprocessor |
|
17 | from .base import Preprocessor | |
18 | from IPython.utils.traitlets import Integer |
|
18 | from IPython.utils.traitlets import Integer | |
19 |
|
19 | |||
@@ -96,39 +96,19 b' class ExecutePreprocessor(Preprocessor):' | |||||
96 | break |
|
96 | break | |
97 | else: |
|
97 | else: | |
98 | continue |
|
98 | continue | |
99 |
elif msg_type |
|
99 | elif msg_type == 'execute_input': | |
100 | continue |
|
100 | continue | |
101 | elif msg_type == 'clear_output': |
|
101 | elif msg_type == 'clear_output': | |
102 | outs = [] |
|
102 | outs = [] | |
103 | continue |
|
103 | continue | |
104 |
|
104 | elif msg_type == 'execute_result': | ||
105 | # set the prompt number for the input and the output |
|
|||
106 | if msg_type == 'execute_result': |
|
|||
107 | cell['prompt_number'] = content['execution_count'] |
|
105 | cell['prompt_number'] = content['execution_count'] | |
108 | out = new_output(output_type=msg_type, |
|
|||
109 | metadata=content['metadata'], |
|
|||
110 | mime_bundle=content['data'], |
|
|||
111 | prompt_number=content['execution_count'], |
|
|||
112 | ) |
|
|||
113 |
|
106 | |||
114 | elif msg_type == 'stream': |
|
107 | try: | |
115 |
out = |
|
108 | out = output_from_msg(msg) | |
116 | name=content['name'], |
|
109 | except ValueError: | |
117 | data=content['data'], |
|
|||
118 | ) |
|
|||
119 | elif msg_type == 'display_data': |
|
|||
120 | out = new_output(output_type=msg_type, |
|
|||
121 | metadata=content['metadata'], |
|
|||
122 | mime_bundle=content['data'], |
|
|||
123 | ) |
|
|||
124 | elif msg_type == 'error': |
|
|||
125 | out = new_output(output_type=msg_type, |
|
|||
126 | ename=content['ename'], |
|
|||
127 | evalue=content['evalue'], |
|
|||
128 | traceback=content['traceback'], |
|
|||
129 | ) |
|
|||
130 | else: |
|
|||
131 | self.log.error("unhandled iopub msg: " + msg_type) |
|
110 | self.log.error("unhandled iopub msg: " + msg_type) | |
|
111 | else: | |||
|
112 | outs.append(out) | |||
132 |
|
113 | |||
133 | outs.append(out) |
|
|||
134 | return outs |
|
114 | return outs |
@@ -7,7 +7,7 b' from .nbbase import (' | |||||
7 | NotebookNode, from_dict, |
|
7 | NotebookNode, from_dict, | |
8 | nbformat, nbformat_minor, nbformat_schema, |
|
8 | nbformat, nbformat_minor, nbformat_schema, | |
9 | new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, |
|
9 | new_code_cell, new_heading_cell, new_markdown_cell, new_notebook, | |
10 | new_output, |
|
10 | new_output, output_from_msg, | |
11 | ) |
|
11 | ) | |
12 |
|
12 | |||
13 | from .nbjson import reads as reads_json, writes as writes_json |
|
13 | from .nbjson import reads as reads_json, writes as writes_json |
@@ -16,11 +16,13 b' nbformat = 4' | |||||
16 | nbformat_minor = 0 |
|
16 | nbformat_minor = 0 | |
17 | nbformat_schema = 'nbformat.v4.schema.json' |
|
17 | nbformat_schema = 'nbformat.v4.schema.json' | |
18 |
|
18 | |||
|
19 | ||||
19 | def validate(node, ref=None): |
|
20 | def validate(node, ref=None): | |
20 | """validate a v4 node""" |
|
21 | """validate a v4 node""" | |
21 | from ..current import validate |
|
22 | from ..current import validate | |
22 | return validate(node, ref=ref, version=nbformat) |
|
23 | return validate(node, ref=ref, version=nbformat) | |
23 |
|
24 | |||
|
25 | ||||
24 | class NotebookNode(Struct): |
|
26 | class NotebookNode(Struct): | |
25 | pass |
|
27 | pass | |
26 |
|
28 | |||
@@ -50,6 +52,51 b' def new_output(output_type, mime_bundle=None, **kwargs):' | |||||
50 | validate(output, output_type) |
|
52 | validate(output, output_type) | |
51 | return output |
|
53 | return output | |
52 |
|
54 | |||
|
55 | ||||
|
56 | def output_from_msg(msg): | |||
|
57 | """Create a NotebookNode for an output from a kernel's IOPub message. | |||
|
58 | ||||
|
59 | Returns | |||
|
60 | ------- | |||
|
61 | ||||
|
62 | NotebookNode: the output as a notebook node. | |||
|
63 | ||||
|
64 | Raises | |||
|
65 | ------ | |||
|
66 | ||||
|
67 | ValueError: if the message is not an output message. | |||
|
68 | ||||
|
69 | """ | |||
|
70 | msg_type = msg['header']['msg_type'] | |||
|
71 | content = msg['content'] | |||
|
72 | ||||
|
73 | if msg_type == 'execute_result': | |||
|
74 | return new_output(output_type=msg_type, | |||
|
75 | metadata=content['metadata'], | |||
|
76 | mime_bundle=content['data'], | |||
|
77 | prompt_number=content['execution_count'], | |||
|
78 | ) | |||
|
79 | ||||
|
80 | elif msg_type == 'stream': | |||
|
81 | return new_output(output_type=msg_type, | |||
|
82 | name=content['name'], | |||
|
83 | data=content['data'], | |||
|
84 | ) | |||
|
85 | elif msg_type == 'display_data': | |||
|
86 | return new_output(output_type=msg_type, | |||
|
87 | metadata=content['metadata'], | |||
|
88 | mime_bundle=content['data'], | |||
|
89 | ) | |||
|
90 | elif msg_type == 'error': | |||
|
91 | return new_output(output_type=msg_type, | |||
|
92 | ename=content['ename'], | |||
|
93 | evalue=content['evalue'], | |||
|
94 | traceback=content['traceback'], | |||
|
95 | ) | |||
|
96 | else: | |||
|
97 | raise ValueError("Unrecognized output msg type: %r" % msg_type) | |||
|
98 | ||||
|
99 | ||||
53 | def new_code_cell(source='', **kwargs): |
|
100 | def new_code_cell(source='', **kwargs): | |
54 | """Create a new code cell""" |
|
101 | """Create a new code cell""" | |
55 | cell = NotebookNode(cell_type='code', source=source) |
|
102 | cell = NotebookNode(cell_type='code', source=source) |
General Comments 0
You need to be logged in to leave comments.
Login now