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