Show More
@@ -1,205 +1,206 b'' | |||
|
1 | 1 | """The official API for working with notebooks in the current format version. |
|
2 | 2 | |
|
3 | 3 | Authors: |
|
4 | 4 | |
|
5 | 5 | * Brian Granger |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | from __future__ import print_function |
|
20 | 20 | import json |
|
21 | 21 | from xml.etree import ElementTree as ET |
|
22 | 22 | import re |
|
23 | 23 | |
|
24 | from IPython.nbformat import v3 | |
|
24 | 25 | from IPython.nbformat import v2 |
|
25 | 26 | from IPython.nbformat import v1 |
|
26 | 27 | |
|
27 |
from IPython.nbformat.v |
|
|
28 | from IPython.nbformat.v3 import ( | |
|
28 | 29 | NotebookNode, |
|
29 | 30 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, |
|
30 | 31 | parse_filename, new_metadata, new_author |
|
31 | 32 | ) |
|
32 | 33 | |
|
33 | 34 | #----------------------------------------------------------------------------- |
|
34 | 35 | # Code |
|
35 | 36 | #----------------------------------------------------------------------------- |
|
36 | 37 | |
|
37 | 38 | current_nbformat = 2 |
|
38 | 39 | |
|
39 | 40 | |
|
40 | 41 | class NBFormatError(Exception): |
|
41 | 42 | pass |
|
42 | 43 | |
|
43 | 44 | |
|
44 | 45 | def parse_json(s, **kwargs): |
|
45 | 46 | """Parse a string into a (nbformat, dict) tuple.""" |
|
46 | 47 | d = json.loads(s, **kwargs) |
|
47 | 48 | nbformat = d.get('nbformat',1) |
|
48 | 49 | return nbformat, d |
|
49 | 50 | |
|
50 | 51 | |
|
51 | 52 | def parse_py(s, **kwargs): |
|
52 | 53 | """Parse a string into a (nbformat, string) tuple.""" |
|
53 | 54 | pattern = r'# <nbformat>(?P<nbformat>\d+)</nbformat>' |
|
54 | 55 | m = re.search(pattern,s) |
|
55 | 56 | if m is not None: |
|
56 | 57 | nbformat = int(m.group('nbformat')) |
|
57 | 58 | else: |
|
58 | 59 | nbformat = 2 |
|
59 | 60 | return nbformat, s |
|
60 | 61 | |
|
61 | 62 | |
|
62 | 63 | def reads_json(s, **kwargs): |
|
63 | 64 | """Read a JSON notebook from a string and return the NotebookNode object.""" |
|
64 | 65 | nbformat, d = parse_json(s, **kwargs) |
|
65 | 66 | if nbformat == 1: |
|
66 | 67 | nb = v1.to_notebook_json(d, **kwargs) |
|
67 | 68 | nb = v2.convert_to_this_nbformat(nb, orig_version=1) |
|
68 | 69 | elif nbformat == 2: |
|
69 | 70 | nb = v2.to_notebook_json(d, **kwargs) |
|
70 | 71 | else: |
|
71 | 72 | raise NBFormatError('Unsupported JSON nbformat version: %i' % nbformat) |
|
72 | 73 | return nb |
|
73 | 74 | |
|
74 | 75 | |
|
75 | 76 | def writes_json(nb, **kwargs): |
|
76 | 77 | return v2.writes_json(nb, **kwargs) |
|
77 | 78 | |
|
78 | 79 | |
|
79 | 80 | def reads_py(s, **kwargs): |
|
80 | 81 | """Read a .py notebook from a string and return the NotebookNode object.""" |
|
81 | 82 | nbformat, s = parse_py(s, **kwargs) |
|
82 | 83 | if nbformat == 2: |
|
83 | 84 | nb = v2.to_notebook_py(s, **kwargs) |
|
84 | 85 | else: |
|
85 | 86 | raise NBFormatError('Unsupported PY nbformat version: %i' % nbformat) |
|
86 | 87 | return nb |
|
87 | 88 | |
|
88 | 89 | |
|
89 | 90 | def writes_py(nb, **kwargs): |
|
90 | 91 | return v2.writes_py(nb, **kwargs) |
|
91 | 92 | |
|
92 | 93 | |
|
93 | 94 | # High level API |
|
94 | 95 | |
|
95 | 96 | |
|
96 | 97 | def reads(s, format, **kwargs): |
|
97 | 98 | """Read a notebook from a string and return the NotebookNode object. |
|
98 | 99 | |
|
99 | 100 | This function properly handles notebooks of any version. The notebook |
|
100 | 101 | returned will always be in the current version's format. |
|
101 | 102 | |
|
102 | 103 | Parameters |
|
103 | 104 | ---------- |
|
104 | 105 | s : unicode |
|
105 | 106 | The raw unicode string to read the notebook from. |
|
106 | 107 | format : (u'json', u'ipynb', u'py') |
|
107 | 108 | The format that the string is in. |
|
108 | 109 | |
|
109 | 110 | Returns |
|
110 | 111 | ------- |
|
111 | 112 | nb : NotebookNode |
|
112 | 113 | The notebook that was read. |
|
113 | 114 | """ |
|
114 | 115 | format = unicode(format) |
|
115 | 116 | if format == u'json' or format == u'ipynb': |
|
116 | 117 | return reads_json(s, **kwargs) |
|
117 | 118 | elif format == u'py': |
|
118 | 119 | return reads_py(s, **kwargs) |
|
119 | 120 | else: |
|
120 | 121 | raise NBFormatError('Unsupported format: %s' % format) |
|
121 | 122 | |
|
122 | 123 | |
|
123 | 124 | def writes(nb, format, **kwargs): |
|
124 | 125 | """Write a notebook to a string in a given format in the current nbformat version. |
|
125 | 126 | |
|
126 | 127 | This function always writes the notebook in the current nbformat version. |
|
127 | 128 | |
|
128 | 129 | Parameters |
|
129 | 130 | ---------- |
|
130 | 131 | nb : NotebookNode |
|
131 | 132 | The notebook to write. |
|
132 | 133 | format : (u'json', u'ipynb', u'py') |
|
133 | 134 | The format to write the notebook in. |
|
134 | 135 | |
|
135 | 136 | Returns |
|
136 | 137 | ------- |
|
137 | 138 | s : unicode |
|
138 | 139 | The notebook string. |
|
139 | 140 | """ |
|
140 | 141 | format = unicode(format) |
|
141 | 142 | if format == u'json' or format == u'ipynb': |
|
142 | 143 | return writes_json(nb, **kwargs) |
|
143 | 144 | elif format == u'py': |
|
144 | 145 | return writes_py(nb, **kwargs) |
|
145 | 146 | else: |
|
146 | 147 | raise NBFormatError('Unsupported format: %s' % format) |
|
147 | 148 | |
|
148 | 149 | |
|
149 | 150 | def read(fp, format, **kwargs): |
|
150 | 151 | """Read a notebook from a file and return the NotebookNode object. |
|
151 | 152 | |
|
152 | 153 | This function properly handles notebooks of any version. The notebook |
|
153 | 154 | returned will always be in the current version's format. |
|
154 | 155 | |
|
155 | 156 | Parameters |
|
156 | 157 | ---------- |
|
157 | 158 | fp : file |
|
158 | 159 | Any file-like object with a read method. |
|
159 | 160 | format : (u'json', u'ipynb', u'py') |
|
160 | 161 | The format that the string is in. |
|
161 | 162 | |
|
162 | 163 | Returns |
|
163 | 164 | ------- |
|
164 | 165 | nb : NotebookNode |
|
165 | 166 | The notebook that was read. |
|
166 | 167 | """ |
|
167 | 168 | return reads(fp.read(), format, **kwargs) |
|
168 | 169 | |
|
169 | 170 | |
|
170 | 171 | def write(nb, fp, format, **kwargs): |
|
171 | 172 | """Write a notebook to a file in a given format in the current nbformat version. |
|
172 | 173 | |
|
173 | 174 | This function always writes the notebook in the current nbformat version. |
|
174 | 175 | |
|
175 | 176 | Parameters |
|
176 | 177 | ---------- |
|
177 | 178 | nb : NotebookNode |
|
178 | 179 | The notebook to write. |
|
179 | 180 | fp : file |
|
180 | 181 | Any file-like object with a write method. |
|
181 | 182 | format : (u'json', u'ipynb', u'py') |
|
182 | 183 | The format to write the notebook in. |
|
183 | 184 | |
|
184 | 185 | Returns |
|
185 | 186 | ------- |
|
186 | 187 | s : unicode |
|
187 | 188 | The notebook string. |
|
188 | 189 | """ |
|
189 | 190 | return fp.write(writes(nb, format, **kwargs)) |
|
190 | 191 | |
|
191 | 192 | def _convert_to_metadata(): |
|
192 | 193 | """Convert to a notebook having notebook metadata.""" |
|
193 | 194 | import glob |
|
194 | 195 | for fname in glob.glob('*.ipynb'): |
|
195 | 196 | print('Converting file:',fname) |
|
196 | 197 | with open(fname,'r') as f: |
|
197 | 198 | nb = read(f,u'json') |
|
198 | 199 | md = new_metadata() |
|
199 | 200 | if u'name' in nb: |
|
200 | 201 | md.name = nb.name |
|
201 | 202 | del nb[u'name'] |
|
202 | 203 | nb.metadata = md |
|
203 | 204 | with open(fname,'w') as f: |
|
204 | 205 | write(nb, f, u'json') |
|
205 | 206 |
@@ -1,200 +1,200 b'' | |||
|
1 | 1 | """Read and write notebooks as regular .py files. |
|
2 | 2 | |
|
3 | 3 | Authors: |
|
4 | 4 | |
|
5 | 5 | * Brian Granger |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | import re |
|
20 | 20 | from .rwbase import NotebookReader, NotebookWriter |
|
21 | 21 | from .nbbase import ( |
|
22 | 22 | new_code_cell, new_text_cell, new_worksheet, |
|
23 | 23 | new_notebook, new_heading_cell |
|
24 | 24 | ) |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Code |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | |
|
30 | 30 | _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)") |
|
31 | 31 | |
|
32 | 32 | class PyReaderError(Exception): |
|
33 | 33 | pass |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | class PyReader(NotebookReader): |
|
37 | 37 | |
|
38 | 38 | def reads(self, s, **kwargs): |
|
39 | 39 | return self.to_notebook(s,**kwargs) |
|
40 | 40 | |
|
41 | 41 | def to_notebook(self, s, **kwargs): |
|
42 | 42 | lines = s.splitlines() |
|
43 | 43 | cells = [] |
|
44 | 44 | cell_lines = [] |
|
45 | 45 | kwargs = {} |
|
46 | 46 | state = u'codecell' |
|
47 | 47 | for line in lines: |
|
48 | 48 | if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line): |
|
49 | 49 | pass |
|
50 | 50 | elif line.startswith(u'# <codecell>'): |
|
51 | 51 | cell = self.new_cell(state, cell_lines, **kwargs) |
|
52 | 52 | if cell is not None: |
|
53 | 53 | cells.append(cell) |
|
54 | 54 | state = u'codecell' |
|
55 | 55 | cell_lines = [] |
|
56 | 56 | kwargs = {} |
|
57 | 57 | elif line.startswith(u'# <htmlcell>'): |
|
58 | 58 | cell = self.new_cell(state, cell_lines, **kwargs) |
|
59 | 59 | if cell is not None: |
|
60 | 60 | cells.append(cell) |
|
61 | 61 | state = u'htmlcell' |
|
62 | 62 | cell_lines = [] |
|
63 | 63 | kwargs = {} |
|
64 | 64 | elif line.startswith(u'# <markdowncell>'): |
|
65 | 65 | cell = self.new_cell(state, cell_lines, **kwargs) |
|
66 | 66 | if cell is not None: |
|
67 | 67 | cells.append(cell) |
|
68 | 68 | state = u'markdowncell' |
|
69 | 69 | cell_lines = [] |
|
70 | 70 | kwargs = {} |
|
71 |
elif line.startswith(u'# < |
|
|
71 | elif line.startswith(u'# <plaintextcell>'): | |
|
72 | 72 | cell = self.new_cell(state, cell_lines, **kwargs) |
|
73 | 73 | if cell is not None: |
|
74 | 74 | cells.append(cell) |
|
75 |
state = u' |
|
|
75 | state = u'plaintextcell' | |
|
76 | 76 | cell_lines = [] |
|
77 | 77 | kwargs = {} |
|
78 | 78 | elif line.startswith(u'# <headingcell'): |
|
79 | 79 | cell = self.new_cell(state, cell_lines, **kwargs) |
|
80 | 80 | if cell is not None: |
|
81 | 81 | cells.append(cell) |
|
82 | 82 | cell_lines = [] |
|
83 | 83 | m = re.match(r'# <headingcell level=(?P<level>\d)>',line) |
|
84 | 84 | if m is not None: |
|
85 | 85 | state = u'headingcell' |
|
86 | 86 | kwargs = {} |
|
87 | 87 | kwargs['level'] = int(m.group('level')) |
|
88 | 88 | else: |
|
89 | 89 | state = u'codecell' |
|
90 | 90 | kwargs = {} |
|
91 | 91 | cell_lines = [] |
|
92 | 92 | else: |
|
93 | 93 | cell_lines.append(line) |
|
94 | 94 | if cell_lines and state == u'codecell': |
|
95 | 95 | cell = self.new_cell(state, cell_lines) |
|
96 | 96 | if cell is not None: |
|
97 | 97 | cells.append(cell) |
|
98 | 98 | ws = new_worksheet(cells=cells) |
|
99 | 99 | nb = new_notebook(worksheets=[ws]) |
|
100 | 100 | return nb |
|
101 | 101 | |
|
102 | 102 | def new_cell(self, state, lines, **kwargs): |
|
103 | 103 | if state == u'codecell': |
|
104 | 104 | input = u'\n'.join(lines) |
|
105 | 105 | input = input.strip(u'\n') |
|
106 | 106 | if input: |
|
107 | 107 | return new_code_cell(input=input) |
|
108 | 108 | elif state == u'htmlcell': |
|
109 | 109 | text = self._remove_comments(lines) |
|
110 | 110 | if text: |
|
111 | 111 | return new_text_cell(u'html',source=text) |
|
112 | 112 | elif state == u'markdowncell': |
|
113 | 113 | text = self._remove_comments(lines) |
|
114 | 114 | if text: |
|
115 | 115 | return new_text_cell(u'markdown',source=text) |
|
116 |
elif state == u' |
|
|
116 | elif state == u'plaintextcell': | |
|
117 | 117 | text = self._remove_comments(lines) |
|
118 | 118 | if text: |
|
119 |
return new_text_cell(u' |
|
|
119 | return new_text_cell(u'plaintext',source=text) | |
|
120 | 120 | elif state == u'headingcell': |
|
121 | 121 | text = self._remove_comments(lines) |
|
122 | 122 | level = kwargs.get('level',1) |
|
123 | 123 | if text: |
|
124 | 124 | return new_heading_cell(source=text,level=level) |
|
125 | 125 | |
|
126 | 126 | def _remove_comments(self, lines): |
|
127 | 127 | new_lines = [] |
|
128 | 128 | for line in lines: |
|
129 | 129 | if line.startswith(u'#'): |
|
130 | 130 | new_lines.append(line[2:]) |
|
131 | 131 | else: |
|
132 | 132 | new_lines.append(line) |
|
133 | 133 | text = u'\n'.join(new_lines) |
|
134 | 134 | text = text.strip(u'\n') |
|
135 | 135 | return text |
|
136 | 136 | |
|
137 | 137 | def split_lines_into_blocks(self, lines): |
|
138 | 138 | if len(lines) == 1: |
|
139 | 139 | yield lines[0] |
|
140 | 140 | raise StopIteration() |
|
141 | 141 | import ast |
|
142 | 142 | source = '\n'.join(lines) |
|
143 | 143 | code = ast.parse(source) |
|
144 | 144 | starts = [x.lineno-1 for x in code.body] |
|
145 | 145 | for i in range(len(starts)-1): |
|
146 | 146 | yield '\n'.join(lines[starts[i]:starts[i+1]]).strip('\n') |
|
147 | 147 | yield '\n'.join(lines[starts[-1]:]).strip('\n') |
|
148 | 148 | |
|
149 | 149 | |
|
150 | 150 | class PyWriter(NotebookWriter): |
|
151 | 151 | |
|
152 | 152 | def writes(self, nb, **kwargs): |
|
153 | 153 | lines = [u'# -*- coding: utf-8 -*-'] |
|
154 | 154 | lines.extend([u'# <nbformat>2</nbformat>','']) |
|
155 | 155 | for ws in nb.worksheets: |
|
156 | 156 | for cell in ws.cells: |
|
157 | 157 | if cell.cell_type == u'code': |
|
158 | 158 | input = cell.get(u'input') |
|
159 | 159 | if input is not None: |
|
160 | 160 | lines.extend([u'# <codecell>',u'']) |
|
161 | 161 | lines.extend(input.splitlines()) |
|
162 | 162 | lines.append(u'') |
|
163 | 163 | elif cell.cell_type == u'html': |
|
164 | 164 | input = cell.get(u'source') |
|
165 | 165 | if input is not None: |
|
166 | 166 | lines.extend([u'# <htmlcell>',u'']) |
|
167 | 167 | lines.extend([u'# ' + line for line in input.splitlines()]) |
|
168 | 168 | lines.append(u'') |
|
169 | 169 | elif cell.cell_type == u'markdown': |
|
170 | 170 | input = cell.get(u'source') |
|
171 | 171 | if input is not None: |
|
172 | 172 | lines.extend([u'# <markdowncell>',u'']) |
|
173 | 173 | lines.extend([u'# ' + line for line in input.splitlines()]) |
|
174 | 174 | lines.append(u'') |
|
175 |
elif cell.cell_type == u' |
|
|
175 | elif cell.cell_type == u'plaintext': | |
|
176 | 176 | input = cell.get(u'source') |
|
177 | 177 | if input is not None: |
|
178 |
lines.extend([u'# < |
|
|
178 | lines.extend([u'# <plaintextcell>',u'']) | |
|
179 | 179 | lines.extend([u'# ' + line for line in input.splitlines()]) |
|
180 | 180 | lines.append(u'') |
|
181 | 181 | elif cell.cell_type == u'heading': |
|
182 | 182 | input = cell.get(u'source') |
|
183 | 183 | level = cell.get(u'level',1) |
|
184 | 184 | if input is not None: |
|
185 | 185 | lines.extend([u'# <headingcell level=%s>' % level,u'']) |
|
186 | 186 | lines.extend([u'# ' + line for line in input.splitlines()]) |
|
187 | 187 | lines.append(u'') |
|
188 | 188 | lines.append('') |
|
189 | 189 | return unicode('\n'.join(lines)) |
|
190 | 190 | |
|
191 | 191 | |
|
192 | 192 | _reader = PyReader() |
|
193 | 193 | _writer = PyWriter() |
|
194 | 194 | |
|
195 | 195 | reads = _reader.reads |
|
196 | 196 | read = _reader.read |
|
197 | 197 | to_notebook = _reader.to_notebook |
|
198 | 198 | write = _writer.write |
|
199 | 199 | writes = _writer.writes |
|
200 | 200 |
@@ -1,127 +1,127 b'' | |||
|
1 | 1 | import os |
|
2 | 2 | from base64 import encodestring |
|
3 | 3 | |
|
4 | 4 | from ..nbbase import ( |
|
5 | 5 | NotebookNode, |
|
6 | 6 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, |
|
7 | 7 | new_metadata, new_author, new_heading_cell |
|
8 | 8 | ) |
|
9 | 9 | |
|
10 | 10 | # some random base64-encoded *bytes* |
|
11 | 11 | png = encodestring(os.urandom(5)) |
|
12 | 12 | jpeg = encodestring(os.urandom(6)) |
|
13 | 13 | |
|
14 | 14 | ws = new_worksheet(name='worksheet1') |
|
15 | 15 | |
|
16 | 16 | ws.cells.append(new_text_cell( |
|
17 | 17 | u'html', |
|
18 | 18 | source='Some NumPy Examples', |
|
19 | 19 | rendered='Some NumPy Examples' |
|
20 | 20 | )) |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | ws.cells.append(new_code_cell( |
|
24 | 24 | input='import numpy', |
|
25 | 25 | prompt_number=1, |
|
26 | 26 | collapsed=False |
|
27 | 27 | )) |
|
28 | 28 | |
|
29 | 29 | ws.cells.append(new_text_cell( |
|
30 | 30 | u'markdown', |
|
31 | 31 | source='A random array', |
|
32 | 32 | rendered='A random array' |
|
33 | 33 | )) |
|
34 | 34 | |
|
35 | 35 | ws.cells.append(new_text_cell( |
|
36 |
u' |
|
|
36 | u'plaintext', | |
|
37 | 37 | source='A random array', |
|
38 | 38 | )) |
|
39 | 39 | |
|
40 | 40 | ws.cells.append(new_heading_cell( |
|
41 | 41 | u'My Heading', |
|
42 | 42 | level=2 |
|
43 | 43 | )) |
|
44 | 44 | |
|
45 | 45 | ws.cells.append(new_code_cell( |
|
46 | 46 | input='a = numpy.random.rand(100)', |
|
47 | 47 | prompt_number=2, |
|
48 | 48 | collapsed=True |
|
49 | 49 | )) |
|
50 | 50 | |
|
51 | 51 | ws.cells.append(new_code_cell( |
|
52 | 52 | input='print a', |
|
53 | 53 | prompt_number=3, |
|
54 | 54 | collapsed=False, |
|
55 | 55 | outputs=[new_output( |
|
56 | 56 | output_type=u'pyout', |
|
57 | 57 | output_text=u'<array a>', |
|
58 | 58 | output_html=u'The HTML rep', |
|
59 | 59 | output_latex=u'$a$', |
|
60 | 60 | output_png=png, |
|
61 | 61 | output_jpeg=jpeg, |
|
62 | 62 | output_svg=u'<svg>', |
|
63 | 63 | output_json=u'json data', |
|
64 | 64 | output_javascript=u'var i=0;', |
|
65 | 65 | prompt_number=3 |
|
66 | 66 | ),new_output( |
|
67 | 67 | output_type=u'display_data', |
|
68 | 68 | output_text=u'<array a>', |
|
69 | 69 | output_html=u'The HTML rep', |
|
70 | 70 | output_latex=u'$a$', |
|
71 | 71 | output_png=png, |
|
72 | 72 | output_jpeg=jpeg, |
|
73 | 73 | output_svg=u'<svg>', |
|
74 | 74 | output_json=u'json data', |
|
75 | 75 | output_javascript=u'var i=0;' |
|
76 | 76 | ),new_output( |
|
77 | 77 | output_type=u'pyerr', |
|
78 | 78 | etype=u'NameError', |
|
79 | 79 | evalue=u'NameError was here', |
|
80 | 80 | traceback=[u'frame 0', u'frame 1', u'frame 2'] |
|
81 | 81 | )] |
|
82 | 82 | )) |
|
83 | 83 | |
|
84 | 84 | authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com', |
|
85 | 85 | affiliation=u'Fox',url=u'http://www.fox.com')] |
|
86 | 86 | md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here', |
|
87 | 87 | modified=u'8601_goes_here',gistid=u'21341231',authors=authors) |
|
88 | 88 | |
|
89 | 89 | nb0 = new_notebook( |
|
90 | 90 | worksheets=[ws, new_worksheet(name='worksheet2')], |
|
91 | 91 | metadata=md |
|
92 | 92 | ) |
|
93 | 93 | |
|
94 | 94 | nb0_py = """# -*- coding: utf-8 -*- |
|
95 | 95 | # <nbformat>2</nbformat> |
|
96 | 96 | |
|
97 | 97 | # <htmlcell> |
|
98 | 98 | |
|
99 | 99 | # Some NumPy Examples |
|
100 | 100 | |
|
101 | 101 | # <codecell> |
|
102 | 102 | |
|
103 | 103 | import numpy |
|
104 | 104 | |
|
105 | 105 | # <markdowncell> |
|
106 | 106 | |
|
107 | 107 | # A random array |
|
108 | 108 | |
|
109 |
# < |
|
|
109 | # <plaintextcell> | |
|
110 | 110 | |
|
111 | 111 | # A random array |
|
112 | 112 | |
|
113 | 113 | # <headingcell level=2> |
|
114 | 114 | |
|
115 | 115 | # My Heading |
|
116 | 116 | |
|
117 | 117 | # <codecell> |
|
118 | 118 | |
|
119 | 119 | a = numpy.random.rand(100) |
|
120 | 120 | |
|
121 | 121 | # <codecell> |
|
122 | 122 | |
|
123 | 123 | print a |
|
124 | 124 | |
|
125 | 125 | """ |
|
126 | 126 | |
|
127 | 127 |
@@ -1,136 +1,136 b'' | |||
|
1 | 1 | from unittest import TestCase |
|
2 | 2 | |
|
3 | 3 | from ..nbbase import ( |
|
4 | 4 | NotebookNode, |
|
5 | 5 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, |
|
6 | 6 | new_author, new_metadata, new_heading_cell |
|
7 | 7 | ) |
|
8 | 8 | |
|
9 | 9 | class TestCell(TestCase): |
|
10 | 10 | |
|
11 | 11 | def test_empty_code_cell(self): |
|
12 | 12 | cc = new_code_cell() |
|
13 | 13 | self.assertEquals(cc.cell_type,u'code') |
|
14 | 14 | self.assertEquals(u'input' not in cc, True) |
|
15 | 15 | self.assertEquals(u'prompt_number' not in cc, True) |
|
16 | 16 | self.assertEquals(cc.outputs, []) |
|
17 | 17 | self.assertEquals(cc.collapsed, False) |
|
18 | 18 | |
|
19 | 19 | def test_code_cell(self): |
|
20 | 20 | cc = new_code_cell(input='a=10', prompt_number=0, collapsed=True) |
|
21 | 21 | cc.outputs = [new_output(output_type=u'pyout', |
|
22 | 22 | output_svg=u'foo',output_text=u'10',prompt_number=0)] |
|
23 | 23 | self.assertEquals(cc.input, u'a=10') |
|
24 | 24 | self.assertEquals(cc.prompt_number, 0) |
|
25 | 25 | self.assertEquals(cc.language, u'python') |
|
26 | 26 | self.assertEquals(cc.outputs[0].svg, u'foo') |
|
27 | 27 | self.assertEquals(cc.outputs[0].text, u'10') |
|
28 | 28 | self.assertEquals(cc.outputs[0].prompt_number, 0) |
|
29 | 29 | self.assertEquals(cc.collapsed, True) |
|
30 | 30 | |
|
31 | 31 | def test_pyerr(self): |
|
32 | 32 | o = new_output(output_type=u'pyerr', etype=u'NameError', |
|
33 | 33 | evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2'] |
|
34 | 34 | ) |
|
35 | 35 | self.assertEquals(o.output_type, u'pyerr') |
|
36 | 36 | self.assertEquals(o.etype, u'NameError') |
|
37 | 37 | self.assertEquals(o.evalue, u'Name not found') |
|
38 | 38 | self.assertEquals(o.traceback, [u'frame 0', u'frame 1', u'frame 2']) |
|
39 | 39 | |
|
40 | 40 | def test_empty_html_cell(self): |
|
41 | 41 | tc = new_text_cell(u'html') |
|
42 | 42 | self.assertEquals(tc.cell_type, u'html') |
|
43 | 43 | self.assertEquals(u'source' not in tc, True) |
|
44 | 44 | self.assertEquals(u'rendered' not in tc, True) |
|
45 | 45 | |
|
46 | 46 | def test_html_cell(self): |
|
47 | 47 | tc = new_text_cell(u'html', 'hi', 'hi') |
|
48 | 48 | self.assertEquals(tc.source, u'hi') |
|
49 | 49 | self.assertEquals(tc.rendered, u'hi') |
|
50 | 50 | |
|
51 | 51 | def test_empty_markdown_cell(self): |
|
52 | 52 | tc = new_text_cell(u'markdown') |
|
53 | 53 | self.assertEquals(tc.cell_type, u'markdown') |
|
54 | 54 | self.assertEquals(u'source' not in tc, True) |
|
55 | 55 | self.assertEquals(u'rendered' not in tc, True) |
|
56 | 56 | |
|
57 | 57 | def test_markdown_cell(self): |
|
58 | 58 | tc = new_text_cell(u'markdown', 'hi', 'hi') |
|
59 | 59 | self.assertEquals(tc.source, u'hi') |
|
60 | 60 | self.assertEquals(tc.rendered, u'hi') |
|
61 | 61 | |
|
62 |
def test_empty_ |
|
|
63 |
tc = new_text_cell(u' |
|
|
64 |
self.assertEquals(tc.cell_type, u' |
|
|
62 | def test_empty_plaintext_cell(self): | |
|
63 | tc = new_text_cell(u'plaintext') | |
|
64 | self.assertEquals(tc.cell_type, u'plaintext') | |
|
65 | 65 | self.assertEquals(u'source' not in tc, True) |
|
66 | 66 | self.assertEquals(u'rendered' not in tc, True) |
|
67 | 67 | |
|
68 |
def test_ |
|
|
69 |
tc = new_text_cell(u' |
|
|
68 | def test_plaintext_cell(self): | |
|
69 | tc = new_text_cell(u'plaintext', 'hi', 'hi') | |
|
70 | 70 | self.assertEquals(tc.source, u'hi') |
|
71 | 71 | self.assertEquals(tc.rendered, u'hi') |
|
72 | 72 | |
|
73 | 73 | def test_empty_heading_cell(self): |
|
74 | 74 | tc = new_heading_cell() |
|
75 | 75 | self.assertEquals(tc.cell_type, u'heading') |
|
76 | 76 | self.assertEquals(u'source' not in tc, True) |
|
77 | 77 | self.assertEquals(u'rendered' not in tc, True) |
|
78 | 78 | |
|
79 | 79 | def test_heading_cell(self): |
|
80 | 80 | tc = new_heading_cell(u'hi', u'hi', level=2) |
|
81 | 81 | self.assertEquals(tc.source, u'hi') |
|
82 | 82 | self.assertEquals(tc.rendered, u'hi') |
|
83 | 83 | self.assertEquals(tc.level, 2) |
|
84 | 84 | |
|
85 | 85 | |
|
86 | 86 | class TestWorksheet(TestCase): |
|
87 | 87 | |
|
88 | 88 | def test_empty_worksheet(self): |
|
89 | 89 | ws = new_worksheet() |
|
90 | 90 | self.assertEquals(ws.cells,[]) |
|
91 | 91 | self.assertEquals(u'name' not in ws, True) |
|
92 | 92 | |
|
93 | 93 | def test_worksheet(self): |
|
94 | 94 | cells = [new_code_cell(), new_text_cell(u'html')] |
|
95 | 95 | ws = new_worksheet(cells=cells,name=u'foo') |
|
96 | 96 | self.assertEquals(ws.cells,cells) |
|
97 | 97 | self.assertEquals(ws.name,u'foo') |
|
98 | 98 | |
|
99 | 99 | class TestNotebook(TestCase): |
|
100 | 100 | |
|
101 | 101 | def test_empty_notebook(self): |
|
102 | 102 | nb = new_notebook() |
|
103 | 103 | self.assertEquals(nb.worksheets, []) |
|
104 | 104 | self.assertEquals(nb.metadata, NotebookNode()) |
|
105 | 105 | self.assertEquals(nb.nbformat,2) |
|
106 | 106 | |
|
107 | 107 | def test_notebook(self): |
|
108 | 108 | worksheets = [new_worksheet(),new_worksheet()] |
|
109 | 109 | metadata = new_metadata(name=u'foo') |
|
110 | 110 | nb = new_notebook(metadata=metadata,worksheets=worksheets) |
|
111 | 111 | self.assertEquals(nb.metadata.name,u'foo') |
|
112 | 112 | self.assertEquals(nb.worksheets,worksheets) |
|
113 | 113 | self.assertEquals(nb.nbformat,2) |
|
114 | 114 | |
|
115 | 115 | class TestMetadata(TestCase): |
|
116 | 116 | |
|
117 | 117 | def test_empty_metadata(self): |
|
118 | 118 | md = new_metadata() |
|
119 | 119 | self.assertEquals(u'name' not in md, True) |
|
120 | 120 | self.assertEquals(u'authors' not in md, True) |
|
121 | 121 | self.assertEquals(u'license' not in md, True) |
|
122 | 122 | self.assertEquals(u'saved' not in md, True) |
|
123 | 123 | self.assertEquals(u'modified' not in md, True) |
|
124 | 124 | self.assertEquals(u'gistid' not in md, True) |
|
125 | 125 | |
|
126 | 126 | def test_metadata(self): |
|
127 | 127 | authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')] |
|
128 | 128 | md = new_metadata(name=u'foo',license=u'BSD',created=u'today', |
|
129 | 129 | modified=u'now',gistid=u'21341231',authors=authors) |
|
130 | 130 | self.assertEquals(md.name, u'foo') |
|
131 | 131 | self.assertEquals(md.license, u'BSD') |
|
132 | 132 | self.assertEquals(md.created, u'today') |
|
133 | 133 | self.assertEquals(md.modified, u'now') |
|
134 | 134 | self.assertEquals(md.gistid, u'21341231') |
|
135 | 135 | self.assertEquals(md.authors, authors) |
|
136 | 136 |
General Comments 0
You need to be logged in to leave comments.
Login now