##// END OF EJS Templates
allow name as kwarg to new_notebook...
MinRK -
Show More
@@ -1,194 +1,196
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
35
36 class NotebookNode(Struct):
36 class NotebookNode(Struct):
37 pass
37 pass
38
38
39
39
40 def from_dict(d):
40 def from_dict(d):
41 if isinstance(d, dict):
41 if isinstance(d, dict):
42 newd = NotebookNode()
42 newd = NotebookNode()
43 for k,v in d.items():
43 for k,v in d.items():
44 newd[k] = from_dict(v)
44 newd[k] = from_dict(v)
45 return newd
45 return newd
46 elif isinstance(d, (tuple, list)):
46 elif isinstance(d, (tuple, list)):
47 return [from_dict(i) for i in d]
47 return [from_dict(i) for i in d]
48 else:
48 else:
49 return d
49 return d
50
50
51
51
52 def new_output(output_type=None, output_text=None, output_png=None,
52 def new_output(output_type=None, output_text=None, output_png=None,
53 output_html=None, output_svg=None, output_latex=None, output_json=None,
53 output_html=None, output_svg=None, output_latex=None, output_json=None,
54 output_javascript=None, output_jpeg=None, prompt_number=None,
54 output_javascript=None, output_jpeg=None, prompt_number=None,
55 etype=None, evalue=None, traceback=None):
55 etype=None, evalue=None, traceback=None):
56 """Create a new code cell with input and output"""
56 """Create a new code cell with input and output"""
57 output = NotebookNode()
57 output = NotebookNode()
58 if output_type is not None:
58 if output_type is not None:
59 output.output_type = unicode(output_type)
59 output.output_type = unicode(output_type)
60
60
61 if output_type != 'pyerr':
61 if output_type != 'pyerr':
62 if output_text is not None:
62 if output_text is not None:
63 output.text = unicode(output_text)
63 output.text = unicode(output_text)
64 if output_png is not None:
64 if output_png is not None:
65 output.png = bytes(output_png)
65 output.png = bytes(output_png)
66 if output_jpeg is not None:
66 if output_jpeg is not None:
67 output.jpeg = bytes(output_jpeg)
67 output.jpeg = bytes(output_jpeg)
68 if output_html is not None:
68 if output_html is not None:
69 output.html = unicode(output_html)
69 output.html = unicode(output_html)
70 if output_svg is not None:
70 if output_svg is not None:
71 output.svg = unicode(output_svg)
71 output.svg = unicode(output_svg)
72 if output_latex is not None:
72 if output_latex is not None:
73 output.latex = unicode(output_latex)
73 output.latex = unicode(output_latex)
74 if output_json is not None:
74 if output_json is not None:
75 output.json = unicode(output_json)
75 output.json = unicode(output_json)
76 if output_javascript is not None:
76 if output_javascript is not None:
77 output.javascript = unicode(output_javascript)
77 output.javascript = unicode(output_javascript)
78
78
79 if output_type == u'pyout':
79 if output_type == u'pyout':
80 if prompt_number is not None:
80 if prompt_number is not None:
81 output.prompt_number = int(prompt_number)
81 output.prompt_number = int(prompt_number)
82
82
83 if output_type == u'pyerr':
83 if output_type == u'pyerr':
84 if etype is not None:
84 if etype is not None:
85 output.etype = unicode(etype)
85 output.etype = unicode(etype)
86 if evalue is not None:
86 if evalue is not None:
87 output.evalue = unicode(evalue)
87 output.evalue = unicode(evalue)
88 if traceback is not None:
88 if traceback is not None:
89 output.traceback = [unicode(frame) for frame in list(traceback)]
89 output.traceback = [unicode(frame) for frame in list(traceback)]
90
90
91 return output
91 return output
92
92
93
93
94 def new_code_cell(input=None, prompt_number=None, outputs=None,
94 def new_code_cell(input=None, prompt_number=None, outputs=None,
95 language=u'python', collapsed=False):
95 language=u'python', collapsed=False):
96 """Create a new code cell with input and output"""
96 """Create a new code cell with input and output"""
97 cell = NotebookNode()
97 cell = NotebookNode()
98 cell.cell_type = u'code'
98 cell.cell_type = u'code'
99 if language is not None:
99 if language is not None:
100 cell.language = unicode(language)
100 cell.language = unicode(language)
101 if input is not None:
101 if input is not None:
102 cell.input = unicode(input)
102 cell.input = unicode(input)
103 if prompt_number is not None:
103 if prompt_number is not None:
104 cell.prompt_number = int(prompt_number)
104 cell.prompt_number = int(prompt_number)
105 if outputs is None:
105 if outputs is None:
106 cell.outputs = []
106 cell.outputs = []
107 else:
107 else:
108 cell.outputs = outputs
108 cell.outputs = outputs
109 if collapsed is not None:
109 if collapsed is not None:
110 cell.collapsed = bool(collapsed)
110 cell.collapsed = bool(collapsed)
111
111
112 return cell
112 return cell
113
113
114 def new_text_cell(cell_type, source=None, rendered=None):
114 def new_text_cell(cell_type, source=None, rendered=None):
115 """Create a new text cell."""
115 """Create a new text cell."""
116 cell = NotebookNode()
116 cell = NotebookNode()
117 if source is not None:
117 if source is not None:
118 cell.source = unicode(source)
118 cell.source = unicode(source)
119 if rendered is not None:
119 if rendered is not None:
120 cell.rendered = unicode(rendered)
120 cell.rendered = unicode(rendered)
121 cell.cell_type = cell_type
121 cell.cell_type = cell_type
122 return cell
122 return cell
123
123
124
124
125 def new_heading_cell(source=None, rendered=None, level=1):
125 def new_heading_cell(source=None, rendered=None, level=1):
126 """Create a new section cell with a given integer level."""
126 """Create a new section cell with a given integer level."""
127 cell = NotebookNode()
127 cell = NotebookNode()
128 cell.cell_type = u'heading'
128 cell.cell_type = u'heading'
129 if source is not None:
129 if source is not None:
130 cell.source = unicode(source)
130 cell.source = unicode(source)
131 if rendered is not None:
131 if rendered is not None:
132 cell.rendered = unicode(rendered)
132 cell.rendered = unicode(rendered)
133 cell.level = int(level)
133 cell.level = int(level)
134 return cell
134 return cell
135
135
136
136
137 def new_worksheet(name=None, cells=None):
137 def new_worksheet(name=None, cells=None):
138 """Create a worksheet by name with with a list of cells."""
138 """Create a worksheet by name with with a list of cells."""
139 ws = NotebookNode()
139 ws = NotebookNode()
140 if name is not None:
140 if name is not None:
141 ws.name = unicode(name)
141 ws.name = unicode(name)
142 if cells is None:
142 if cells is None:
143 ws.cells = []
143 ws.cells = []
144 else:
144 else:
145 ws.cells = list(cells)
145 ws.cells = list(cells)
146 return ws
146 return ws
147
147
148
148
149 def new_notebook(metadata=None, worksheets=None):
149 def new_notebook(name=None, metadata=None, worksheets=None):
150 """Create a notebook by name, id and a list of worksheets."""
150 """Create a notebook by name, id and a list of worksheets."""
151 nb = NotebookNode()
151 nb = NotebookNode()
152 nb.nbformat = nbformat
152 nb.nbformat = nbformat
153 if worksheets is None:
153 if worksheets is None:
154 nb.worksheets = []
154 nb.worksheets = []
155 else:
155 else:
156 nb.worksheets = list(worksheets)
156 nb.worksheets = list(worksheets)
157 if metadata is None:
157 if metadata is None:
158 nb.metadata = new_metadata()
158 nb.metadata = new_metadata()
159 else:
159 else:
160 nb.metadata = NotebookNode(metadata)
160 nb.metadata = NotebookNode(metadata)
161 if name is not None:
162 nb.metadata.name = unicode(name)
161 return nb
163 return nb
162
164
163
165
164 def new_metadata(name=None, authors=None, license=None, created=None,
166 def new_metadata(name=None, authors=None, license=None, created=None,
165 modified=None, gistid=None):
167 modified=None, gistid=None):
166 """Create a new metadata node."""
168 """Create a new metadata node."""
167 metadata = NotebookNode()
169 metadata = NotebookNode()
168 if name is not None:
170 if name is not None:
169 metadata.name = unicode(name)
171 metadata.name = unicode(name)
170 if authors is not None:
172 if authors is not None:
171 metadata.authors = list(authors)
173 metadata.authors = list(authors)
172 if created is not None:
174 if created is not None:
173 metadata.created = unicode(created)
175 metadata.created = unicode(created)
174 if modified is not None:
176 if modified is not None:
175 metadata.modified = unicode(modified)
177 metadata.modified = unicode(modified)
176 if license is not None:
178 if license is not None:
177 metadata.license = unicode(license)
179 metadata.license = unicode(license)
178 if gistid is not None:
180 if gistid is not None:
179 metadata.gistid = unicode(gistid)
181 metadata.gistid = unicode(gistid)
180 return metadata
182 return metadata
181
183
182 def new_author(name=None, email=None, affiliation=None, url=None):
184 def new_author(name=None, email=None, affiliation=None, url=None):
183 """Create a new author."""
185 """Create a new author."""
184 author = NotebookNode()
186 author = NotebookNode()
185 if name is not None:
187 if name is not None:
186 author.name = unicode(name)
188 author.name = unicode(name)
187 if email is not None:
189 if email is not None:
188 author.email = unicode(email)
190 author.email = unicode(email)
189 if affiliation is not None:
191 if affiliation is not None:
190 author.affiliation = unicode(affiliation)
192 author.affiliation = unicode(affiliation)
191 if url is not None:
193 if url is not None:
192 author.url = unicode(url)
194 author.url = unicode(url)
193 return author
195 return author
194
196
@@ -1,136 +1,143
1 from unittest import TestCase
1 from unittest import TestCase
2
2
3 from ..nbbase import (
3 from ..nbbase import (
4 NotebookNode,
4 NotebookNode,
5 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
5 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
6 new_author, new_metadata, new_heading_cell, nbformat
6 new_author, new_metadata, new_heading_cell, nbformat
7 )
7 )
8
8
9 class TestCell(TestCase):
9 class TestCell(TestCase):
10
10
11 def test_empty_code_cell(self):
11 def test_empty_code_cell(self):
12 cc = new_code_cell()
12 cc = new_code_cell()
13 self.assertEquals(cc.cell_type,u'code')
13 self.assertEquals(cc.cell_type,u'code')
14 self.assertEquals(u'input' not in cc, True)
14 self.assertEquals(u'input' not in cc, True)
15 self.assertEquals(u'prompt_number' not in cc, True)
15 self.assertEquals(u'prompt_number' not in cc, True)
16 self.assertEquals(cc.outputs, [])
16 self.assertEquals(cc.outputs, [])
17 self.assertEquals(cc.collapsed, False)
17 self.assertEquals(cc.collapsed, False)
18
18
19 def test_code_cell(self):
19 def test_code_cell(self):
20 cc = new_code_cell(input='a=10', prompt_number=0, collapsed=True)
20 cc = new_code_cell(input='a=10', prompt_number=0, collapsed=True)
21 cc.outputs = [new_output(output_type=u'pyout',
21 cc.outputs = [new_output(output_type=u'pyout',
22 output_svg=u'foo',output_text=u'10',prompt_number=0)]
22 output_svg=u'foo',output_text=u'10',prompt_number=0)]
23 self.assertEquals(cc.input, u'a=10')
23 self.assertEquals(cc.input, u'a=10')
24 self.assertEquals(cc.prompt_number, 0)
24 self.assertEquals(cc.prompt_number, 0)
25 self.assertEquals(cc.language, u'python')
25 self.assertEquals(cc.language, u'python')
26 self.assertEquals(cc.outputs[0].svg, u'foo')
26 self.assertEquals(cc.outputs[0].svg, u'foo')
27 self.assertEquals(cc.outputs[0].text, u'10')
27 self.assertEquals(cc.outputs[0].text, u'10')
28 self.assertEquals(cc.outputs[0].prompt_number, 0)
28 self.assertEquals(cc.outputs[0].prompt_number, 0)
29 self.assertEquals(cc.collapsed, True)
29 self.assertEquals(cc.collapsed, True)
30
30
31 def test_pyerr(self):
31 def test_pyerr(self):
32 o = new_output(output_type=u'pyerr', etype=u'NameError',
32 o = new_output(output_type=u'pyerr', etype=u'NameError',
33 evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2']
33 evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2']
34 )
34 )
35 self.assertEquals(o.output_type, u'pyerr')
35 self.assertEquals(o.output_type, u'pyerr')
36 self.assertEquals(o.etype, u'NameError')
36 self.assertEquals(o.etype, u'NameError')
37 self.assertEquals(o.evalue, u'Name not found')
37 self.assertEquals(o.evalue, u'Name not found')
38 self.assertEquals(o.traceback, [u'frame 0', u'frame 1', u'frame 2'])
38 self.assertEquals(o.traceback, [u'frame 0', u'frame 1', u'frame 2'])
39
39
40 def test_empty_html_cell(self):
40 def test_empty_html_cell(self):
41 tc = new_text_cell(u'html')
41 tc = new_text_cell(u'html')
42 self.assertEquals(tc.cell_type, u'html')
42 self.assertEquals(tc.cell_type, u'html')
43 self.assertEquals(u'source' not in tc, True)
43 self.assertEquals(u'source' not in tc, True)
44 self.assertEquals(u'rendered' not in tc, True)
44 self.assertEquals(u'rendered' not in tc, True)
45
45
46 def test_html_cell(self):
46 def test_html_cell(self):
47 tc = new_text_cell(u'html', 'hi', 'hi')
47 tc = new_text_cell(u'html', 'hi', 'hi')
48 self.assertEquals(tc.source, u'hi')
48 self.assertEquals(tc.source, u'hi')
49 self.assertEquals(tc.rendered, u'hi')
49 self.assertEquals(tc.rendered, u'hi')
50
50
51 def test_empty_markdown_cell(self):
51 def test_empty_markdown_cell(self):
52 tc = new_text_cell(u'markdown')
52 tc = new_text_cell(u'markdown')
53 self.assertEquals(tc.cell_type, u'markdown')
53 self.assertEquals(tc.cell_type, u'markdown')
54 self.assertEquals(u'source' not in tc, True)
54 self.assertEquals(u'source' not in tc, True)
55 self.assertEquals(u'rendered' not in tc, True)
55 self.assertEquals(u'rendered' not in tc, True)
56
56
57 def test_markdown_cell(self):
57 def test_markdown_cell(self):
58 tc = new_text_cell(u'markdown', 'hi', 'hi')
58 tc = new_text_cell(u'markdown', 'hi', 'hi')
59 self.assertEquals(tc.source, u'hi')
59 self.assertEquals(tc.source, u'hi')
60 self.assertEquals(tc.rendered, u'hi')
60 self.assertEquals(tc.rendered, u'hi')
61
61
62 def test_empty_plaintext_cell(self):
62 def test_empty_plaintext_cell(self):
63 tc = new_text_cell(u'plaintext')
63 tc = new_text_cell(u'plaintext')
64 self.assertEquals(tc.cell_type, u'plaintext')
64 self.assertEquals(tc.cell_type, u'plaintext')
65 self.assertEquals(u'source' not in tc, True)
65 self.assertEquals(u'source' not in tc, True)
66 self.assertEquals(u'rendered' not in tc, True)
66 self.assertEquals(u'rendered' not in tc, True)
67
67
68 def test_plaintext_cell(self):
68 def test_plaintext_cell(self):
69 tc = new_text_cell(u'plaintext', 'hi', 'hi')
69 tc = new_text_cell(u'plaintext', 'hi', 'hi')
70 self.assertEquals(tc.source, u'hi')
70 self.assertEquals(tc.source, u'hi')
71 self.assertEquals(tc.rendered, u'hi')
71 self.assertEquals(tc.rendered, u'hi')
72
72
73 def test_empty_heading_cell(self):
73 def test_empty_heading_cell(self):
74 tc = new_heading_cell()
74 tc = new_heading_cell()
75 self.assertEquals(tc.cell_type, u'heading')
75 self.assertEquals(tc.cell_type, u'heading')
76 self.assertEquals(u'source' not in tc, True)
76 self.assertEquals(u'source' not in tc, True)
77 self.assertEquals(u'rendered' not in tc, True)
77 self.assertEquals(u'rendered' not in tc, True)
78
78
79 def test_heading_cell(self):
79 def test_heading_cell(self):
80 tc = new_heading_cell(u'hi', u'hi', level=2)
80 tc = new_heading_cell(u'hi', u'hi', level=2)
81 self.assertEquals(tc.source, u'hi')
81 self.assertEquals(tc.source, u'hi')
82 self.assertEquals(tc.rendered, u'hi')
82 self.assertEquals(tc.rendered, u'hi')
83 self.assertEquals(tc.level, 2)
83 self.assertEquals(tc.level, 2)
84
84
85
85
86 class TestWorksheet(TestCase):
86 class TestWorksheet(TestCase):
87
87
88 def test_empty_worksheet(self):
88 def test_empty_worksheet(self):
89 ws = new_worksheet()
89 ws = new_worksheet()
90 self.assertEquals(ws.cells,[])
90 self.assertEquals(ws.cells,[])
91 self.assertEquals(u'name' not in ws, True)
91 self.assertEquals(u'name' not in ws, True)
92
92
93 def test_worksheet(self):
93 def test_worksheet(self):
94 cells = [new_code_cell(), new_text_cell(u'html')]
94 cells = [new_code_cell(), new_text_cell(u'html')]
95 ws = new_worksheet(cells=cells,name=u'foo')
95 ws = new_worksheet(cells=cells,name=u'foo')
96 self.assertEquals(ws.cells,cells)
96 self.assertEquals(ws.cells,cells)
97 self.assertEquals(ws.name,u'foo')
97 self.assertEquals(ws.name,u'foo')
98
98
99 class TestNotebook(TestCase):
99 class TestNotebook(TestCase):
100
100
101 def test_empty_notebook(self):
101 def test_empty_notebook(self):
102 nb = new_notebook()
102 nb = new_notebook()
103 self.assertEquals(nb.worksheets, [])
103 self.assertEquals(nb.worksheets, [])
104 self.assertEquals(nb.metadata, NotebookNode())
104 self.assertEquals(nb.metadata, NotebookNode())
105 self.assertEquals(nb.nbformat,nbformat)
105 self.assertEquals(nb.nbformat,nbformat)
106
106
107 def test_notebook(self):
107 def test_notebook(self):
108 worksheets = [new_worksheet(),new_worksheet()]
108 worksheets = [new_worksheet(),new_worksheet()]
109 metadata = new_metadata(name=u'foo')
109 metadata = new_metadata(name=u'foo')
110 nb = new_notebook(metadata=metadata,worksheets=worksheets)
110 nb = new_notebook(metadata=metadata,worksheets=worksheets)
111 self.assertEquals(nb.metadata.name,u'foo')
111 self.assertEquals(nb.metadata.name,u'foo')
112 self.assertEquals(nb.worksheets,worksheets)
112 self.assertEquals(nb.worksheets,worksheets)
113 self.assertEquals(nb.nbformat,nbformat)
113 self.assertEquals(nb.nbformat,nbformat)
114
114
115 def test_notebook_name(self):
116 worksheets = [new_worksheet(),new_worksheet()]
117 nb = new_notebook(name='foo',worksheets=worksheets)
118 self.assertEquals(nb.metadata.name,u'foo')
119 self.assertEquals(nb.worksheets,worksheets)
120 self.assertEquals(nb.nbformat,nbformat)
121
115 class TestMetadata(TestCase):
122 class TestMetadata(TestCase):
116
123
117 def test_empty_metadata(self):
124 def test_empty_metadata(self):
118 md = new_metadata()
125 md = new_metadata()
119 self.assertEquals(u'name' not in md, True)
126 self.assertEquals(u'name' not in md, True)
120 self.assertEquals(u'authors' not in md, True)
127 self.assertEquals(u'authors' not in md, True)
121 self.assertEquals(u'license' not in md, True)
128 self.assertEquals(u'license' not in md, True)
122 self.assertEquals(u'saved' not in md, True)
129 self.assertEquals(u'saved' not in md, True)
123 self.assertEquals(u'modified' not in md, True)
130 self.assertEquals(u'modified' not in md, True)
124 self.assertEquals(u'gistid' not in md, True)
131 self.assertEquals(u'gistid' not in md, True)
125
132
126 def test_metadata(self):
133 def test_metadata(self):
127 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')]
134 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')]
128 md = new_metadata(name=u'foo',license=u'BSD',created=u'today',
135 md = new_metadata(name=u'foo',license=u'BSD',created=u'today',
129 modified=u'now',gistid=u'21341231',authors=authors)
136 modified=u'now',gistid=u'21341231',authors=authors)
130 self.assertEquals(md.name, u'foo')
137 self.assertEquals(md.name, u'foo')
131 self.assertEquals(md.license, u'BSD')
138 self.assertEquals(md.license, u'BSD')
132 self.assertEquals(md.created, u'today')
139 self.assertEquals(md.created, u'today')
133 self.assertEquals(md.modified, u'now')
140 self.assertEquals(md.modified, u'now')
134 self.assertEquals(md.gistid, u'21341231')
141 self.assertEquals(md.gistid, u'21341231')
135 self.assertEquals(md.authors, authors)
142 self.assertEquals(md.authors, authors)
136
143
General Comments 0
You need to be logged in to leave comments. Login now