##// END OF EJS Templates
adding stream kwarg to current.new_output...
Paul Ivanov -
Show More
@@ -1,206 +1,209 b''
1 1 """The basic dict based notebook format.
2 2
3 3 The Python representation of a notebook is a nested structure of
4 4 dictionary subclasses that support attribute access
5 5 (IPython.utils.ipstruct.Struct). The functions in this module are merely
6 6 helpers to build the structs in the right form.
7 7
8 8 Authors:
9 9
10 10 * Brian Granger
11 11 """
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Copyright (C) 2008-2011 The IPython Development Team
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #-----------------------------------------------------------------------------
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Imports
22 22 #-----------------------------------------------------------------------------
23 23
24 24 import pprint
25 25 import uuid
26 26
27 27 from IPython.utils.ipstruct import Struct
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Code
31 31 #-----------------------------------------------------------------------------
32 32
33 33 # Change this when incrementing the nbformat version
34 34 nbformat = 3
35 35 nbformat_minor = 0
36 36
37 37 class NotebookNode(Struct):
38 38 pass
39 39
40 40
41 41 def from_dict(d):
42 42 if isinstance(d, dict):
43 43 newd = NotebookNode()
44 44 for k,v in d.items():
45 45 newd[k] = from_dict(v)
46 46 return newd
47 47 elif isinstance(d, (tuple, list)):
48 48 return [from_dict(i) for i in d]
49 49 else:
50 50 return d
51 51
52 52
53 53 def new_output(output_type=None, output_text=None, output_png=None,
54 54 output_html=None, output_svg=None, output_latex=None, output_json=None,
55 55 output_javascript=None, output_jpeg=None, prompt_number=None,
56 etype=None, evalue=None, traceback=None):
56 etype=None, evalue=None, traceback=None, stream=None):
57 57 """Create a new code cell with input and output"""
58 58 output = NotebookNode()
59 59 if output_type is not None:
60 60 output.output_type = unicode(output_type)
61 61
62 62 if output_type != 'pyerr':
63 63 if output_text is not None:
64 64 output.text = unicode(output_text)
65 65 if output_png is not None:
66 66 output.png = bytes(output_png)
67 67 if output_jpeg is not None:
68 68 output.jpeg = bytes(output_jpeg)
69 69 if output_html is not None:
70 70 output.html = unicode(output_html)
71 71 if output_svg is not None:
72 72 output.svg = unicode(output_svg)
73 73 if output_latex is not None:
74 74 output.latex = unicode(output_latex)
75 75 if output_json is not None:
76 76 output.json = unicode(output_json)
77 77 if output_javascript is not None:
78 78 output.javascript = unicode(output_javascript)
79 79
80 80 if output_type == u'pyout':
81 81 if prompt_number is not None:
82 82 output.prompt_number = int(prompt_number)
83 83
84 84 if output_type == u'pyerr':
85 85 if etype is not None:
86 86 output.etype = unicode(etype)
87 87 if evalue is not None:
88 88 output.evalue = unicode(evalue)
89 89 if traceback is not None:
90 90 output.traceback = [unicode(frame) for frame in list(traceback)]
91 91
92 if output_type == u'stream':
93 output.stream = 'stdout' if stream is None else unicode(stream)
94
92 95 return output
93 96
94 97
95 98 def new_code_cell(input=None, prompt_number=None, outputs=None,
96 99 language=u'python', collapsed=False, metadata=None):
97 100 """Create a new code cell with input and output"""
98 101 cell = NotebookNode()
99 102 cell.cell_type = u'code'
100 103 if language is not None:
101 104 cell.language = unicode(language)
102 105 if input is not None:
103 106 cell.input = unicode(input)
104 107 if prompt_number is not None:
105 108 cell.prompt_number = int(prompt_number)
106 109 if outputs is None:
107 110 cell.outputs = []
108 111 else:
109 112 cell.outputs = outputs
110 113 if collapsed is not None:
111 114 cell.collapsed = bool(collapsed)
112 115 cell.metadata = NotebookNode(metadata or {})
113 116
114 117 return cell
115 118
116 119 def new_text_cell(cell_type, source=None, rendered=None, metadata=None):
117 120 """Create a new text cell."""
118 121 cell = NotebookNode()
119 122 # VERSIONHACK: plaintext -> raw
120 123 # handle never-released plaintext name for raw cells
121 124 if cell_type == 'plaintext':
122 125 cell_type = 'raw'
123 126 if source is not None:
124 127 cell.source = unicode(source)
125 128 if rendered is not None:
126 129 cell.rendered = unicode(rendered)
127 130 cell.metadata = NotebookNode(metadata or {})
128 131 cell.cell_type = cell_type
129 132 return cell
130 133
131 134
132 135 def new_heading_cell(source=None, rendered=None, level=1, metadata=None):
133 136 """Create a new section cell with a given integer level."""
134 137 cell = NotebookNode()
135 138 cell.cell_type = u'heading'
136 139 if source is not None:
137 140 cell.source = unicode(source)
138 141 if rendered is not None:
139 142 cell.rendered = unicode(rendered)
140 143 cell.level = int(level)
141 144 cell.metadata = NotebookNode(metadata or {})
142 145 return cell
143 146
144 147
145 148 def new_worksheet(name=None, cells=None, metadata=None):
146 149 """Create a worksheet by name with with a list of cells."""
147 150 ws = NotebookNode()
148 151 if name is not None:
149 152 ws.name = unicode(name)
150 153 if cells is None:
151 154 ws.cells = []
152 155 else:
153 156 ws.cells = list(cells)
154 157 ws.metadata = NotebookNode(metadata or {})
155 158 return ws
156 159
157 160
158 161 def new_notebook(name=None, metadata=None, worksheets=None):
159 162 """Create a notebook by name, id and a list of worksheets."""
160 163 nb = NotebookNode()
161 164 nb.nbformat = nbformat
162 165 nb.nbformat_minor = nbformat_minor
163 166 if worksheets is None:
164 167 nb.worksheets = []
165 168 else:
166 169 nb.worksheets = list(worksheets)
167 170 if metadata is None:
168 171 nb.metadata = new_metadata()
169 172 else:
170 173 nb.metadata = NotebookNode(metadata)
171 174 if name is not None:
172 175 nb.metadata.name = unicode(name)
173 176 return nb
174 177
175 178
176 179 def new_metadata(name=None, authors=None, license=None, created=None,
177 180 modified=None, gistid=None):
178 181 """Create a new metadata node."""
179 182 metadata = NotebookNode()
180 183 if name is not None:
181 184 metadata.name = unicode(name)
182 185 if authors is not None:
183 186 metadata.authors = list(authors)
184 187 if created is not None:
185 188 metadata.created = unicode(created)
186 189 if modified is not None:
187 190 metadata.modified = unicode(modified)
188 191 if license is not None:
189 192 metadata.license = unicode(license)
190 193 if gistid is not None:
191 194 metadata.gistid = unicode(gistid)
192 195 return metadata
193 196
194 197 def new_author(name=None, email=None, affiliation=None, url=None):
195 198 """Create a new author."""
196 199 author = NotebookNode()
197 200 if name is not None:
198 201 author.name = unicode(name)
199 202 if email is not None:
200 203 author.email = unicode(email)
201 204 if affiliation is not None:
202 205 author.affiliation = unicode(affiliation)
203 206 if url is not None:
204 207 author.url = unicode(url)
205 208 return author
206 209
@@ -1,153 +1,154 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 import os
4 4 from base64 import encodestring
5 5
6 6 from ..nbbase import (
7 7 NotebookNode,
8 8 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
9 9 new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor
10 10 )
11 11
12 12 # some random base64-encoded *bytes*
13 13 png = encodestring(os.urandom(5))
14 14 jpeg = encodestring(os.urandom(6))
15 15
16 16 ws = new_worksheet(name='worksheet1')
17 17
18 18 ws.cells.append(new_text_cell(
19 19 u'html',
20 20 source='Some NumPy Examples',
21 21 rendered='Some NumPy Examples'
22 22 ))
23 23
24 24
25 25 ws.cells.append(new_code_cell(
26 26 input='import numpy',
27 27 prompt_number=1,
28 28 collapsed=False
29 29 ))
30 30
31 31 ws.cells.append(new_text_cell(
32 32 u'markdown',
33 33 source='A random array',
34 34 rendered='A random array'
35 35 ))
36 36
37 37 ws.cells.append(new_text_cell(
38 38 u'raw',
39 39 source='A random array',
40 40 ))
41 41
42 42 ws.cells.append(new_heading_cell(
43 43 u'My Heading',
44 44 level=2
45 45 ))
46 46
47 47 ws.cells.append(new_code_cell(
48 48 input='a = numpy.random.rand(100)',
49 49 prompt_number=2,
50 50 collapsed=True
51 51 ))
52 52 ws.cells.append(new_code_cell(
53 53 input='a = 10\nb = 5\n',
54 54 prompt_number=3,
55 55 ))
56 56 ws.cells.append(new_code_cell(
57 57 input='a = 10\nb = 5',
58 58 prompt_number=4,
59 59 ))
60 60
61 61 ws.cells.append(new_code_cell(
62 62 input=u'print "ünîcødé"',
63 63 prompt_number=3,
64 64 collapsed=False,
65 65 outputs=[new_output(
66 66 output_type=u'pyout',
67 67 output_text=u'<array a>',
68 68 output_html=u'The HTML rep',
69 69 output_latex=u'$a$',
70 70 output_png=png,
71 71 output_jpeg=jpeg,
72 72 output_svg=u'<svg>',
73 73 output_json=u'json data',
74 74 output_javascript=u'var i=0;',
75 75 prompt_number=3
76 76 ),new_output(
77 77 output_type=u'display_data',
78 78 output_text=u'<array a>',
79 79 output_html=u'The HTML rep',
80 80 output_latex=u'$a$',
81 81 output_png=png,
82 82 output_jpeg=jpeg,
83 83 output_svg=u'<svg>',
84 84 output_json=u'json data',
85 85 output_javascript=u'var i=0;'
86 86 ),new_output(
87 87 output_type=u'pyerr',
88 88 etype=u'NameError',
89 89 evalue=u'NameError was here',
90 90 traceback=[u'frame 0', u'frame 1', u'frame 2']
91 91 ),new_output(
92 92 output_type=u'stream',
93 93 output_text='foo\rbar\r\n'
94 94 ),new_output(
95 95 output_type=u'stream',
96 stream='stderr',
96 97 output_text='\rfoo\rbar\n'
97 98 )]
98 99 ))
99 100
100 101 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com',
101 102 affiliation=u'Fox',url=u'http://www.fox.com')]
102 103 md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here',
103 104 modified=u'8601_goes_here',gistid=u'21341231',authors=authors)
104 105
105 106 nb0 = new_notebook(
106 107 worksheets=[ws, new_worksheet(name='worksheet2')],
107 108 metadata=md
108 109 )
109 110
110 111 nb0_py = u"""# -*- coding: utf-8 -*-
111 112 # <nbformat>%i.%i</nbformat>
112 113
113 114 # <htmlcell>
114 115
115 116 # Some NumPy Examples
116 117
117 118 # <codecell>
118 119
119 120 import numpy
120 121
121 122 # <markdowncell>
122 123
123 124 # A random array
124 125
125 126 # <rawcell>
126 127
127 128 # A random array
128 129
129 130 # <headingcell level=2>
130 131
131 132 # My Heading
132 133
133 134 # <codecell>
134 135
135 136 a = numpy.random.rand(100)
136 137
137 138 # <codecell>
138 139
139 140 a = 10
140 141 b = 5
141 142
142 143 # <codecell>
143 144
144 145 a = 10
145 146 b = 5
146 147
147 148 # <codecell>
148 149
149 150 print "ünîcødé"
150 151
151 152 """ % (nbformat, nbformat_minor)
152 153
153 154
General Comments 0
You need to be logged in to leave comments. Login now