Show More
@@ -0,0 +1,190 b'' | |||||
|
1 | import json | |||
|
2 | from xml.etree import ElementTree as ET | |||
|
3 | import re | |||
|
4 | ||||
|
5 | from IPython.nbformat import v2 | |||
|
6 | from IPython.nbformat import v1 | |||
|
7 | ||||
|
8 | ||||
|
9 | current_nbformat = 2 | |||
|
10 | ||||
|
11 | ||||
|
12 | class NBFormatError(Exception): | |||
|
13 | pass | |||
|
14 | ||||
|
15 | ||||
|
16 | def parse_json(s, **kwargs): | |||
|
17 | """Parse a string into a (nbformat, dict) tuple.""" | |||
|
18 | d = json.loads(s, **kwargs) | |||
|
19 | nbformat = d.get('nbformat',1) | |||
|
20 | return nbformat, d | |||
|
21 | ||||
|
22 | ||||
|
23 | def parse_xml(s, **kwargs): | |||
|
24 | """Parse a string into a (nbformat, etree) tuple.""" | |||
|
25 | root = ET.fromstring(s) | |||
|
26 | nbformat_e = root.find('nbformat') | |||
|
27 | if nbformat_e is not None: | |||
|
28 | nbformat = int(nbformat_e.text) | |||
|
29 | else: | |||
|
30 | raise NBFormatError('No nbformat version found') | |||
|
31 | return nbformat, root | |||
|
32 | ||||
|
33 | ||||
|
34 | def parse_py(s, **kwargs): | |||
|
35 | """Parse a string into a (nbformat, string) tuple.""" | |||
|
36 | pattern = r'# <nbformat>(?P<nbformat>\d+)</nbformat>' | |||
|
37 | m = re.search(pattern,s) | |||
|
38 | if m is not None: | |||
|
39 | nbformat = int(m.group('nbformat')) | |||
|
40 | else: | |||
|
41 | raise NBFormatError('No nbformat version found') | |||
|
42 | return nbformat, s | |||
|
43 | ||||
|
44 | ||||
|
45 | def reads_json(s, **kwargs): | |||
|
46 | """Read a JSON notebook from a string and return the NotebookNode object.""" | |||
|
47 | nbformat, d = parse_json(s, **kwargs) | |||
|
48 | if nbformat == 1: | |||
|
49 | nb = v1.to_notebook_json(d, **kwargs) | |||
|
50 | nb = v2.convert_to_this_nbformat(nb, orig_version=1) | |||
|
51 | elif nbformat == 2: | |||
|
52 | nb = v2.to_notebook_json(d, **kwargs) | |||
|
53 | else: | |||
|
54 | raise NBFormatError('Unsupported JSON nbformat version: %i' % nbformat) | |||
|
55 | return nb | |||
|
56 | ||||
|
57 | ||||
|
58 | def writes_json(nb, **kwargs): | |||
|
59 | return v2.writes_json(nb, **kwargs) | |||
|
60 | ||||
|
61 | ||||
|
62 | def reads_xml(s, **kwargs): | |||
|
63 | """Read an XML notebook from a string and return the NotebookNode object.""" | |||
|
64 | nbformat, root = parse_xml(s, **kwargs) | |||
|
65 | if nbformat == 2: | |||
|
66 | nb = v2.to_notebook_xml(root, **kwargs) | |||
|
67 | else: | |||
|
68 | raise NBFormatError('Unsupported XML nbformat version: %i' % nbformat) | |||
|
69 | return nb | |||
|
70 | ||||
|
71 | ||||
|
72 | def writes_xml(nb, **kwargs): | |||
|
73 | return v2.writes_xml(nb, **kwargs) | |||
|
74 | ||||
|
75 | ||||
|
76 | def reads_py(s, **kwargs): | |||
|
77 | """Read a .py notebook from a string and return the NotebookNode object.""" | |||
|
78 | nbformat, s = parse_py(s, **kwargs) | |||
|
79 | if nbformat == 2: | |||
|
80 | nb = v2.to_notebook_py(s, **kwargs) | |||
|
81 | else: | |||
|
82 | raise NBFormatError('Unsupported PY nbformat version: %i' % nbformat) | |||
|
83 | return nb | |||
|
84 | ||||
|
85 | ||||
|
86 | def writes_py(nb, **kwargs): | |||
|
87 | return v2.writes_py(nb, **kwargs) | |||
|
88 | ||||
|
89 | ||||
|
90 | # High level API | |||
|
91 | ||||
|
92 | ||||
|
93 | def reads(s, format, **kwargs): | |||
|
94 | """Read a notebook from a string and return the NotebookNode object. | |||
|
95 | ||||
|
96 | This function properly handles notebooks of any version. The notebook | |||
|
97 | returned will always be in the current version's format. | |||
|
98 | ||||
|
99 | Parameters | |||
|
100 | ---------- | |||
|
101 | s : str | |||
|
102 | The raw string to read the notebook from. | |||
|
103 | format : ('xml','json','py') | |||
|
104 | The format that the string is in. | |||
|
105 | ||||
|
106 | Returns | |||
|
107 | ------- | |||
|
108 | nb : NotebookNode | |||
|
109 | The notebook that was read. | |||
|
110 | """ | |||
|
111 | if format == 'xml': | |||
|
112 | return reads_xml(s, **kwargs) | |||
|
113 | elif format == 'json': | |||
|
114 | return reads_json(s, **kwargs) | |||
|
115 | elif format == 'py': | |||
|
116 | return reads_py(s, **kwargs) | |||
|
117 | else: | |||
|
118 | raise NBFormatError('Unsupported format: %s' % format) | |||
|
119 | ||||
|
120 | ||||
|
121 | def writes(nb, format, **kwargs): | |||
|
122 | """Write a notebook to a string in a given format in the current nbformat version. | |||
|
123 | ||||
|
124 | This function always writes the notebook in the current nbformat version. | |||
|
125 | ||||
|
126 | Parameters | |||
|
127 | ---------- | |||
|
128 | nb : NotebookNode | |||
|
129 | The notebook to write. | |||
|
130 | format : ('xml','json','py') | |||
|
131 | The format to write the notebook in. | |||
|
132 | ||||
|
133 | Returns | |||
|
134 | ------- | |||
|
135 | s : str | |||
|
136 | The notebook string. | |||
|
137 | """ | |||
|
138 | if format == 'xml': | |||
|
139 | return writes_xml(nb, **kwargs) | |||
|
140 | elif format == 'json': | |||
|
141 | return writes_json(nb, **kwargs) | |||
|
142 | elif format == 'py': | |||
|
143 | return writes_py(nb, **kwargs) | |||
|
144 | else: | |||
|
145 | raise NBFormatError('Unsupported format: %s' % format) | |||
|
146 | ||||
|
147 | ||||
|
148 | def read(fp, format, **kwargs): | |||
|
149 | """Read a notebook from a file and return the NotebookNode object. | |||
|
150 | ||||
|
151 | This function properly handles notebooks of any version. The notebook | |||
|
152 | returned will always be in the current version's format. | |||
|
153 | ||||
|
154 | Parameters | |||
|
155 | ---------- | |||
|
156 | fp : file | |||
|
157 | Any file-like object with a read method. | |||
|
158 | format : ('xml','json','py') | |||
|
159 | The format that the string is in. | |||
|
160 | ||||
|
161 | Returns | |||
|
162 | ------- | |||
|
163 | nb : NotebookNode | |||
|
164 | The notebook that was read. | |||
|
165 | """ | |||
|
166 | return reads(fp.read(), format, **kwargs) | |||
|
167 | ||||
|
168 | ||||
|
169 | def write(nb, fp, format, **kwargs): | |||
|
170 | """Write a notebook to a file in a given format in the current nbformat version. | |||
|
171 | ||||
|
172 | This function always writes the notebook in the current nbformat version. | |||
|
173 | ||||
|
174 | Parameters | |||
|
175 | ---------- | |||
|
176 | nb : NotebookNode | |||
|
177 | The notebook to write. | |||
|
178 | fp : file | |||
|
179 | Any file-like object with a write method. | |||
|
180 | format : ('xml','json','py') | |||
|
181 | The format to write the notebook in. | |||
|
182 | ||||
|
183 | Returns | |||
|
184 | ------- | |||
|
185 | s : str | |||
|
186 | The notebook string. | |||
|
187 | """ | |||
|
188 | return fp.write(writes(nb, format, **kwargs)) | |||
|
189 | ||||
|
190 |
@@ -0,0 +1,12 b'' | |||||
|
1 | ||||
|
2 | from .nbbase import ( | |||
|
3 | NotebookNode, | |||
|
4 | new_code_cell, new_text_cell, new_notebook | |||
|
5 | ) | |||
|
6 | ||||
|
7 | from .nbjson import reads as reads_json, writes as writes_json | |||
|
8 | from .nbjson import reads as read_json, writes as write_json | |||
|
9 | from .nbjson import to_notebook as to_notebook_json | |||
|
10 | ||||
|
11 | from .convert import convert_to_this_nbformat | |||
|
12 |
@@ -0,0 +1,5 b'' | |||||
|
1 | ||||
|
2 | ||||
|
3 | def convert_to_this_nbformat(nb, orig_version=None): | |||
|
4 | raise ValueError('Cannot convert to v1 notebook format') | |||
|
5 |
@@ -0,0 +1,53 b'' | |||||
|
1 | """The basic dict based notebook format.""" | |||
|
2 | ||||
|
3 | import pprint | |||
|
4 | import uuid | |||
|
5 | ||||
|
6 | from IPython.utils.ipstruct import Struct | |||
|
7 | ||||
|
8 | ||||
|
9 | class NotebookNode(Struct): | |||
|
10 | pass | |||
|
11 | ||||
|
12 | ||||
|
13 | def from_dict(d): | |||
|
14 | if isinstance(d, dict): | |||
|
15 | newd = NotebookNode() | |||
|
16 | for k,v in d.items(): | |||
|
17 | newd[k] = from_dict(v) | |||
|
18 | return newd | |||
|
19 | elif isinstance(d, (tuple, list)): | |||
|
20 | return [from_dict(i) for i in d] | |||
|
21 | else: | |||
|
22 | return d | |||
|
23 | ||||
|
24 | ||||
|
25 | def new_code_cell(code=None, prompt_number=None): | |||
|
26 | """Create a new code cell with input and output""" | |||
|
27 | cell = NotebookNode() | |||
|
28 | cell.cell_type = u'code' | |||
|
29 | if code is not None: | |||
|
30 | cell.code = unicode(code) | |||
|
31 | if prompt_number is not None: | |||
|
32 | cell.prompt_number = int(prompt_number) | |||
|
33 | return cell | |||
|
34 | ||||
|
35 | ||||
|
36 | def new_text_cell(text=None): | |||
|
37 | """Create a new text cell.""" | |||
|
38 | cell = NotebookNode() | |||
|
39 | if text is not None: | |||
|
40 | cell.text = unicode(text) | |||
|
41 | cell.cell_type = u'text' | |||
|
42 | return cell | |||
|
43 | ||||
|
44 | ||||
|
45 | def new_notebook(cells=None): | |||
|
46 | """Create a notebook by name, id and a list of worksheets.""" | |||
|
47 | nb = NotebookNode() | |||
|
48 | if cells is not None: | |||
|
49 | nb.cells = cells | |||
|
50 | else: | |||
|
51 | nb.cells = [] | |||
|
52 | return nb | |||
|
53 |
@@ -0,0 +1,35 b'' | |||||
|
1 | """Read and write notebooks in JSON format.""" | |||
|
2 | ||||
|
3 | from base64 import encodestring | |||
|
4 | from .rwbase import NotebookReader, NotebookWriter | |||
|
5 | from .nbbase import from_dict | |||
|
6 | import json | |||
|
7 | ||||
|
8 | ||||
|
9 | class JSONReader(NotebookReader): | |||
|
10 | ||||
|
11 | def reads(self, s, **kwargs): | |||
|
12 | nb = json.loads(s, **kwargs) | |||
|
13 | return self.to_notebook(nb, **kwargs) | |||
|
14 | ||||
|
15 | def to_notebook(self, d, **kwargs): | |||
|
16 | """Convert from a raw JSON dict to a nested NotebookNode structure.""" | |||
|
17 | return from_dict(d) | |||
|
18 | ||||
|
19 | ||||
|
20 | class JSONWriter(NotebookWriter): | |||
|
21 | ||||
|
22 | def writes(self, nb, **kwargs): | |||
|
23 | kwargs['indent'] = 4 | |||
|
24 | return json.dumps(nb, **kwargs) | |||
|
25 | ||||
|
26 | ||||
|
27 | _reader = JSONReader() | |||
|
28 | _writer = JSONWriter() | |||
|
29 | ||||
|
30 | reads = _reader.reads | |||
|
31 | read = _reader.read | |||
|
32 | to_notebook = _reader.to_notebook | |||
|
33 | write = _writer.write | |||
|
34 | writes = _writer.writes | |||
|
35 |
@@ -0,0 +1,26 b'' | |||||
|
1 | from base64 import encodestring, decodestring | |||
|
2 | ||||
|
3 | ||||
|
4 | class NotebookReader(object): | |||
|
5 | ||||
|
6 | def reads(self, s, **kwargs): | |||
|
7 | """Read a notebook from a string.""" | |||
|
8 | raise NotImplementedError("loads must be implemented in a subclass") | |||
|
9 | ||||
|
10 | def read(self, fp, **kwargs): | |||
|
11 | """Read a notebook from a file like object""" | |||
|
12 | return self.reads(fp.read(), **kwargs) | |||
|
13 | ||||
|
14 | ||||
|
15 | class NotebookWriter(object): | |||
|
16 | ||||
|
17 | def writes(self, nb, **kwargs): | |||
|
18 | """Write a notebook to a string.""" | |||
|
19 | raise NotImplementedError("loads must be implemented in a subclass") | |||
|
20 | ||||
|
21 | def write(self, nb, fp, **kwargs): | |||
|
22 | """Write a notebook to a file like object""" | |||
|
23 | return fp.write(self.writes(nb,**kwargs)) | |||
|
24 | ||||
|
25 | ||||
|
26 |
@@ -0,0 +1,29 b'' | |||||
|
1 | from ..nbbase import ( | |||
|
2 | NotebookNode, | |||
|
3 | new_code_cell, new_text_cell, new_notebook | |||
|
4 | ) | |||
|
5 | ||||
|
6 | ||||
|
7 | ||||
|
8 | nb0 = new_notebook() | |||
|
9 | ||||
|
10 | nb0.cells.append(new_text_cell( | |||
|
11 | text='Some NumPy Examples' | |||
|
12 | )) | |||
|
13 | ||||
|
14 | ||||
|
15 | nb0.cells.append(new_code_cell( | |||
|
16 | code='import numpy', | |||
|
17 | prompt_number=1 | |||
|
18 | )) | |||
|
19 | ||||
|
20 | nb0.cells.append(new_code_cell( | |||
|
21 | code='a = numpy.random.rand(100)', | |||
|
22 | prompt_number=2 | |||
|
23 | )) | |||
|
24 | ||||
|
25 | nb0.cells.append(new_code_cell( | |||
|
26 | code='print a', | |||
|
27 | prompt_number=3 | |||
|
28 | )) | |||
|
29 |
@@ -0,0 +1,41 b'' | |||||
|
1 | from unittest import TestCase | |||
|
2 | ||||
|
3 | from ..nbbase import ( | |||
|
4 | NotebookNode, | |||
|
5 | new_code_cell, new_text_cell, new_notebook | |||
|
6 | ) | |||
|
7 | ||||
|
8 | class TestCell(TestCase): | |||
|
9 | ||||
|
10 | def test_empty_code_cell(self): | |||
|
11 | cc = new_code_cell() | |||
|
12 | self.assertEquals(cc.cell_type,'code') | |||
|
13 | self.assertEquals('code' not in cc, True) | |||
|
14 | self.assertEquals('prompt_number' not in cc, True) | |||
|
15 | ||||
|
16 | def test_code_cell(self): | |||
|
17 | cc = new_code_cell(code='a=10', prompt_number=0) | |||
|
18 | self.assertEquals(cc.code, u'a=10') | |||
|
19 | self.assertEquals(cc.prompt_number, 0) | |||
|
20 | ||||
|
21 | def test_empty_text_cell(self): | |||
|
22 | tc = new_text_cell() | |||
|
23 | self.assertEquals(tc.cell_type, 'text') | |||
|
24 | self.assertEquals('text' not in tc, True) | |||
|
25 | ||||
|
26 | def test_text_cell(self): | |||
|
27 | tc = new_text_cell('hi') | |||
|
28 | self.assertEquals(tc.text, u'hi') | |||
|
29 | ||||
|
30 | ||||
|
31 | class TestNotebook(TestCase): | |||
|
32 | ||||
|
33 | def test_empty_notebook(self): | |||
|
34 | nb = new_notebook() | |||
|
35 | self.assertEquals(nb.cells, []) | |||
|
36 | ||||
|
37 | def test_notebooke(self): | |||
|
38 | cells = [new_code_cell(),new_text_cell()] | |||
|
39 | nb = new_notebook(cells=cells) | |||
|
40 | self.assertEquals(nb.cells,cells) | |||
|
41 |
@@ -0,0 +1,21 b'' | |||||
|
1 | ||||
|
2 | from .nbbase import ( | |||
|
3 | NotebookNode, | |||
|
4 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet | |||
|
5 | ) | |||
|
6 | ||||
|
7 | from .nbjson import reads as reads_json, writes as writes_json | |||
|
8 | from .nbjson import reads as read_json, writes as write_json | |||
|
9 | from .nbjson import to_notebook as to_notebook_json | |||
|
10 | ||||
|
11 | from .nbxml import reads as reads_xml, writes as writes_xml | |||
|
12 | from .nbxml import reads as read_xml, writes as write_xml | |||
|
13 | from .nbxml import to_notebook as to_notebook_xml | |||
|
14 | ||||
|
15 | from .nbpy import reads as reads_py, writes as writes_py | |||
|
16 | from .nbpy import reads as read_py, writes as write_py | |||
|
17 | from .nbpy import to_notebook as to_notebook_py | |||
|
18 | ||||
|
19 | from .convert import convert_to_this_nbformat | |||
|
20 | ||||
|
21 |
@@ -0,0 +1,20 b'' | |||||
|
1 | from .nbbase import ( | |||
|
2 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output | |||
|
3 | ) | |||
|
4 | ||||
|
5 | def convert_to_this_nbformat(nb, orig_version=1): | |||
|
6 | if orig_version == 1: | |||
|
7 | newnb = new_notebook() | |||
|
8 | ws = new_worksheet() | |||
|
9 | for cell in nb.cells: | |||
|
10 | if cell.cell_type == 'code': | |||
|
11 | newcell = new_code_cell(input=cell.get('code'),prompt_number=cell.get('prompt_number')) | |||
|
12 | elif cell.cell_type == 'text': | |||
|
13 | newcell = new_text_cell(text=cell.get('text')) | |||
|
14 | ws.cells.append(newcell) | |||
|
15 | newnb.worksheets.append(ws) | |||
|
16 | return newnb | |||
|
17 | else: | |||
|
18 | raise ValueError('Cannot convert a notebook from v%s to v2' % orig_version) | |||
|
19 | ||||
|
20 |
1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 |
@@ -0,0 +1,15 b'' | |||||
|
1 | ||||
|
2 | from unittest import TestCase | |||
|
3 | ||||
|
4 | from ..nbjson import reads, writes | |||
|
5 | from .nbexamples import nb0 | |||
|
6 | ||||
|
7 | ||||
|
8 | class TestJSON(TestCase): | |||
|
9 | ||||
|
10 | def test_roundtrip(self): | |||
|
11 | s = writes(nb0) | |||
|
12 | self.assertEquals(reads(s),nb0) | |||
|
13 | ||||
|
14 | ||||
|
15 |
1 | NO CONTENT: file renamed from IPython/nbformat/tests/__init__.py to IPython/nbformat/v1/tests/__init__.py |
|
NO CONTENT: file renamed from IPython/nbformat/tests/__init__.py to IPython/nbformat/v1/tests/__init__.py |
@@ -1,14 +1,14 b'' | |||||
1 | from unittest import TestCase |
|
1 | from unittest import TestCase | |
2 |
|
2 | |||
3 |
from |
|
3 | from ..nbjson import reads, writes | |
4 |
from |
|
4 | from .nbexamples import nb0 | |
5 |
|
5 | |||
6 |
|
6 | |||
7 | class TestJSON(TestCase): |
|
7 | class TestJSON(TestCase): | |
8 |
|
8 | |||
9 | def test_roundtrip(self): |
|
9 | def test_roundtrip(self): | |
10 | s = writes(nb0) |
|
10 | s = writes(nb0) | |
11 | self.assertEquals(reads(s),nb0) |
|
11 | self.assertEquals(reads(s),nb0) | |
12 |
|
12 | |||
13 |
|
13 | |||
14 |
|
14 |
@@ -1,90 +1,103 b'' | |||||
1 | """The basic dict based notebook format.""" |
|
1 | """The basic dict based notebook format.""" | |
2 |
|
2 | |||
3 | import pprint |
|
3 | import pprint | |
4 | import uuid |
|
4 | import uuid | |
5 |
|
5 | |||
6 | from IPython.utils.ipstruct import Struct |
|
6 | from IPython.utils.ipstruct import Struct | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | class NotebookNode(Struct): |
|
9 | class NotebookNode(Struct): | |
10 | pass |
|
10 | pass | |
11 |
|
11 | |||
12 |
|
12 | |||
|
13 | def from_dict(d): | |||
|
14 | if isinstance(d, dict): | |||
|
15 | newd = NotebookNode() | |||
|
16 | for k,v in d.items(): | |||
|
17 | newd[k] = from_dict(v) | |||
|
18 | return newd | |||
|
19 | elif isinstance(d, (tuple, list)): | |||
|
20 | return [from_dict(i) for i in d] | |||
|
21 | else: | |||
|
22 | return d | |||
|
23 | ||||
|
24 | ||||
13 | def new_output(output_type=None, output_text=None, output_png=None, |
|
25 | def new_output(output_type=None, output_text=None, output_png=None, | |
14 | output_html=None, output_svg=None, output_latex=None, output_json=None, |
|
26 | output_html=None, output_svg=None, output_latex=None, output_json=None, | |
15 | output_javascript=None): |
|
27 | output_javascript=None): | |
16 | """Create a new code cell with input and output""" |
|
28 | """Create a new code cell with input and output""" | |
17 | output = NotebookNode() |
|
29 | output = NotebookNode() | |
18 | if output_type is not None: |
|
30 | if output_type is not None: | |
19 | output.output_type = unicode(output_type) |
|
31 | output.output_type = unicode(output_type) | |
20 | if output_text is not None: |
|
32 | if output_text is not None: | |
21 | output.text = unicode(output_text) |
|
33 | output.text = unicode(output_text) | |
22 | if output_png is not None: |
|
34 | if output_png is not None: | |
23 | output.png = bytes(output_png) |
|
35 | output.png = bytes(output_png) | |
24 | if output_html is not None: |
|
36 | if output_html is not None: | |
25 | output.html = unicode(output_html) |
|
37 | output.html = unicode(output_html) | |
26 | if output_svg is not None: |
|
38 | if output_svg is not None: | |
27 | output.svg = unicode(output_svg) |
|
39 | output.svg = unicode(output_svg) | |
28 | if output_latex is not None: |
|
40 | if output_latex is not None: | |
29 | output.latex = unicode(output_latex) |
|
41 | output.latex = unicode(output_latex) | |
30 | if output_json is not None: |
|
42 | if output_json is not None: | |
31 | output.json = unicode(output_json) |
|
43 | output.json = unicode(output_json) | |
32 | if output_javascript is not None: |
|
44 | if output_javascript is not None: | |
33 | output.javascript = unicode(output_javascript) |
|
45 | output.javascript = unicode(output_javascript) | |
34 |
|
46 | |||
35 | return output |
|
47 | return output | |
36 |
|
48 | |||
37 |
|
49 | |||
38 | def new_code_cell(input=None, prompt_number=None, outputs=None, language=u'python'): |
|
50 | def new_code_cell(input=None, prompt_number=None, outputs=None, language=u'python'): | |
39 | """Create a new code cell with input and output""" |
|
51 | """Create a new code cell with input and output""" | |
40 | cell = NotebookNode() |
|
52 | cell = NotebookNode() | |
41 | cell.cell_type = u'code' |
|
53 | cell.cell_type = u'code' | |
42 | if language is not None: |
|
54 | if language is not None: | |
43 | cell.language = unicode(language) |
|
55 | cell.language = unicode(language) | |
44 | if input is not None: |
|
56 | if input is not None: | |
45 | cell.input = unicode(input) |
|
57 | cell.input = unicode(input) | |
46 | if prompt_number is not None: |
|
58 | if prompt_number is not None: | |
47 | cell.prompt_number = int(prompt_number) |
|
59 | cell.prompt_number = int(prompt_number) | |
48 | if outputs is None: |
|
60 | if outputs is None: | |
49 | cell.outputs = [] |
|
61 | cell.outputs = [] | |
50 | else: |
|
62 | else: | |
51 | cell.outputs = outputs |
|
63 | cell.outputs = outputs | |
52 |
|
64 | |||
53 | return cell |
|
65 | return cell | |
54 |
|
66 | |||
55 | def new_text_cell(text=None): |
|
67 | def new_text_cell(text=None): | |
56 | """Create a new text cell.""" |
|
68 | """Create a new text cell.""" | |
57 | cell = NotebookNode() |
|
69 | cell = NotebookNode() | |
58 | if text is not None: |
|
70 | if text is not None: | |
59 | cell.text = unicode(text) |
|
71 | cell.text = unicode(text) | |
60 | cell.cell_type = u'text' |
|
72 | cell.cell_type = u'text' | |
61 | return cell |
|
73 | return cell | |
62 |
|
74 | |||
63 |
|
75 | |||
64 | def new_worksheet(name=None, cells=None): |
|
76 | def new_worksheet(name=None, cells=None): | |
65 | """Create a worksheet by name with with a list of cells.""" |
|
77 | """Create a worksheet by name with with a list of cells.""" | |
66 | ws = NotebookNode() |
|
78 | ws = NotebookNode() | |
67 | if name is not None: |
|
79 | if name is not None: | |
68 | ws.name = unicode(name) |
|
80 | ws.name = unicode(name) | |
69 | if cells is None: |
|
81 | if cells is None: | |
70 | ws.cells = [] |
|
82 | ws.cells = [] | |
71 | else: |
|
83 | else: | |
72 | ws.cells = list(cells) |
|
84 | ws.cells = list(cells) | |
73 | return ws |
|
85 | return ws | |
74 |
|
86 | |||
75 |
|
87 | |||
76 | def new_notebook(name=None, id=None, worksheets=None): |
|
88 | def new_notebook(name=None, id=None, worksheets=None): | |
77 | """Create a notebook by name, id and a list of worksheets.""" |
|
89 | """Create a notebook by name, id and a list of worksheets.""" | |
78 | nb = NotebookNode() |
|
90 | nb = NotebookNode() | |
|
91 | nb.nbformat = 2 | |||
79 | if name is not None: |
|
92 | if name is not None: | |
80 | nb.name = unicode(name) |
|
93 | nb.name = unicode(name) | |
81 | if id is None: |
|
94 | if id is None: | |
82 | nb.id = unicode(uuid.uuid4()) |
|
95 | nb.id = unicode(uuid.uuid4()) | |
83 | else: |
|
96 | else: | |
84 | nb.id = unicode(id) |
|
97 | nb.id = unicode(id) | |
85 | if worksheets is None: |
|
98 | if worksheets is None: | |
86 | nb.worksheets = [] |
|
99 | nb.worksheets = [] | |
87 | else: |
|
100 | else: | |
88 | nb.worksheets = list(worksheets) |
|
101 | nb.worksheets = list(worksheets) | |
89 | return nb |
|
102 | return nb | |
90 |
|
103 |
@@ -1,38 +1,43 b'' | |||||
1 | """Read and write notebooks in JSON format.""" |
|
1 | """Read and write notebooks in JSON format.""" | |
2 |
|
2 | |||
3 | from base64 import encodestring |
|
3 | from base64 import encodestring | |
|
4 | from .nbbase import from_dict | |||
4 | from .rwbase import NotebookReader, NotebookWriter, base64_decode |
|
5 | from .rwbase import NotebookReader, NotebookWriter, base64_decode | |
5 | import json |
|
6 | import json | |
6 |
|
7 | |||
7 |
|
8 | |||
8 | class BytesEncoder(json.JSONEncoder): |
|
9 | class BytesEncoder(json.JSONEncoder): | |
9 | def default(self, obj): |
|
10 | def default(self, obj): | |
10 | if isinstance(obj, bytes): |
|
11 | if isinstance(obj, bytes): | |
11 | return unicode(encodestring(bytes)) |
|
12 | return unicode(encodestring(bytes)) | |
12 | return json.JSONEncoder.default(self, obj) |
|
13 | return json.JSONEncoder.default(self, obj) | |
13 |
|
14 | |||
14 |
|
15 | |||
15 | class JSONReader(NotebookReader): |
|
16 | class JSONReader(NotebookReader): | |
16 |
|
17 | |||
17 | def reads(self, s, **kwargs): |
|
18 | def reads(self, s, **kwargs): | |
18 | nb = json.loads(s, **kwargs) |
|
19 | nb = json.loads(s, **kwargs) | |
19 |
nb = |
|
20 | nb = self.to_notebook(nb, **kwargs) | |
20 | return nb |
|
21 | return nb | |
21 |
|
22 | |||
|
23 | def to_notebook(self, d, **kwargs): | |||
|
24 | return base64_decode(from_dict(d)) | |||
|
25 | ||||
22 |
|
26 | |||
23 | class JSONWriter(NotebookWriter): |
|
27 | class JSONWriter(NotebookWriter): | |
24 |
|
28 | |||
25 | def writes(self, nb, **kwargs): |
|
29 | def writes(self, nb, **kwargs): | |
26 | kwargs['cls'] = BytesEncoder |
|
30 | kwargs['cls'] = BytesEncoder | |
27 | kwargs['indent'] = 4 |
|
31 | kwargs['indent'] = 4 | |
28 | return json.dumps(nb, **kwargs) |
|
32 | return json.dumps(nb, **kwargs) | |
29 |
|
33 | |||
30 |
|
34 | |||
31 | _reader = JSONReader() |
|
35 | _reader = JSONReader() | |
32 | _writer = JSONWriter() |
|
36 | _writer = JSONWriter() | |
33 |
|
37 | |||
34 | reads = _reader.reads |
|
38 | reads = _reader.reads | |
35 | read = _reader.read |
|
39 | read = _reader.read | |
|
40 | to_notebook = _reader.to_notebook | |||
36 | write = _writer.write |
|
41 | write = _writer.write | |
37 | writes = _writer.writes |
|
42 | writes = _writer.writes | |
38 |
|
43 |
@@ -1,47 +1,55 b'' | |||||
1 | """Read and write notebooks as regular .py files.""" |
|
1 | """Read and write notebooks as regular .py files.""" | |
2 |
|
2 | |||
3 | from .rwbase import NotebookReader, NotebookWriter |
|
3 | from .rwbase import NotebookReader, NotebookWriter | |
4 | from .nbbase import new_code_cell, new_worksheet, new_notebook |
|
4 | from .nbbase import new_code_cell, new_worksheet, new_notebook | |
5 |
|
5 | |||
6 |
|
6 | |||
7 | class PyReader(NotebookReader): |
|
7 | class PyReader(NotebookReader): | |
8 |
|
8 | |||
9 | def reads(self, s, **kwargs): |
|
9 | def reads(self, s, **kwargs): | |
|
10 | return self.to_notebook(s,**kwargs) | |||
|
11 | ||||
|
12 | def to_notebook(self, s, **kwargs): | |||
10 | lines = s.splitlines() |
|
13 | lines = s.splitlines() | |
11 | cells = [] |
|
14 | cells = [] | |
12 | cell_lines = [] |
|
15 | cell_lines = [] | |
13 | for line in lines: |
|
16 | for line in lines: | |
14 | if line.startswith(u'# <codecell>'): |
|
17 | if line.startswith(u'# <codecell>'): | |
|
18 | cell_lines = [] | |||
|
19 | if line.startswith(u'# </codecell>'): | |||
15 | code = u'\n'.join(cell_lines) |
|
20 | code = u'\n'.join(cell_lines) | |
16 | code = code.strip(u'\n') |
|
21 | code = code.strip(u'\n') | |
17 | if code: |
|
22 | if code: | |
18 | cells.append(new_code_cell(input=code)) |
|
23 | cells.append(new_code_cell(input=code)) | |
19 | cell_lines = [] |
|
|||
20 | else: |
|
24 | else: | |
21 | cell_lines.append(line) |
|
25 | cell_lines.append(line) | |
22 | ws = new_worksheet(cells=cells) |
|
26 | ws = new_worksheet(cells=cells) | |
23 | nb = new_notebook(worksheets=[ws]) |
|
27 | nb = new_notebook(worksheets=[ws]) | |
24 | return nb |
|
28 | return nb | |
25 |
|
29 | |||
26 |
|
30 | |||
27 | class PyWriter(NotebookWriter): |
|
31 | class PyWriter(NotebookWriter): | |
28 |
|
32 | |||
29 | def writes(self, nb, **kwargs): |
|
33 | def writes(self, nb, **kwargs): | |
30 | lines = [] |
|
34 | lines = [] | |
|
35 | lines.extend(['# <nbformat>2</nbformat>','']) | |||
31 | for ws in nb.worksheets: |
|
36 | for ws in nb.worksheets: | |
32 | for cell in ws.cells: |
|
37 | for cell in ws.cells: | |
33 | if cell.cell_type == 'code': |
|
38 | if cell.cell_type == 'code': | |
34 | input = cell.input |
|
39 | input = cell.input | |
35 | lines.extend([u'# <codecell>',u'']) |
|
40 | lines.extend([u'# <codecell>',u'']) | |
36 | lines.extend(input.splitlines()) |
|
41 | lines.extend(input.splitlines()) | |
37 |
lines. |
|
42 | lines.extend([u'',u'# </codecell>']) | |
|
43 | lines.append('') | |||
38 | return unicode('\n'.join(lines)) |
|
44 | return unicode('\n'.join(lines)) | |
39 |
|
45 | |||
40 |
|
46 | |||
41 | _reader = PyReader() |
|
47 | _reader = PyReader() | |
42 | _writer = PyWriter() |
|
48 | _writer = PyWriter() | |
43 |
|
49 | |||
44 | reads = _reader.reads |
|
50 | reads = _reader.reads | |
45 | read = _reader.read |
|
51 | read = _reader.read | |
|
52 | to_notebook = _reader.to_notebook | |||
46 | write = _writer.write |
|
53 | write = _writer.write | |
47 | writes = _writer.writes |
|
54 | writes = _writer.writes | |
|
55 |
@@ -1,145 +1,165 b'' | |||||
1 | """Read and write notebook files as XML.""" |
|
1 | """Read and write notebook files as XML.""" | |
2 |
|
2 | |||
|
3 | from base64 import encodestring, decodestring | |||
3 | from xml.etree import ElementTree as ET |
|
4 | from xml.etree import ElementTree as ET | |
4 |
|
5 | |||
5 | from .rwbase import NotebookReader, NotebookWriter |
|
6 | from .rwbase import NotebookReader, NotebookWriter | |
6 | from .nbbase import ( |
|
7 | from .nbbase import ( | |
7 | 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 | |
8 | ) |
|
9 | ) | |
9 |
|
10 | |||
10 | def indent(elem, level=0): |
|
11 | def indent(elem, level=0): | |
11 | i = "\n" + level*" " |
|
12 | i = "\n" + level*" " | |
12 | if len(elem): |
|
13 | if len(elem): | |
13 | if not elem.text or not elem.text.strip(): |
|
14 | if not elem.text or not elem.text.strip(): | |
14 | elem.text = i + " " |
|
15 | elem.text = i + " " | |
15 | if not elem.tail or not elem.tail.strip(): |
|
16 | if not elem.tail or not elem.tail.strip(): | |
16 | elem.tail = i |
|
17 | elem.tail = i | |
17 | for elem in elem: |
|
18 | for elem in elem: | |
18 | indent(elem, level+1) |
|
19 | indent(elem, level+1) | |
19 | if not elem.tail or not elem.tail.strip(): |
|
20 | if not elem.tail or not elem.tail.strip(): | |
20 | elem.tail = i |
|
21 | elem.tail = i | |
21 | else: |
|
22 | else: | |
22 | if level and (not elem.tail or not elem.tail.strip()): |
|
23 | if level and (not elem.tail or not elem.tail.strip()): | |
23 | elem.tail = i |
|
24 | elem.tail = i | |
24 |
|
25 | |||
25 |
|
26 | |||
26 | def _get_text(e, tag): |
|
27 | def _get_text(e, tag): | |
27 | sub_e = e.find(tag) |
|
28 | sub_e = e.find(tag) | |
28 | if sub_e is None: |
|
29 | if sub_e is None: | |
29 | return None |
|
30 | return None | |
30 | else: |
|
31 | else: | |
31 | return sub_e.text |
|
32 | return sub_e.text | |
32 |
|
33 | |||
33 |
|
34 | |||
34 | def _set_text(nbnode, attr, parent, tag): |
|
35 | def _set_text(nbnode, attr, parent, tag): | |
35 | if attr in nbnode: |
|
36 | if attr in nbnode: | |
36 | e = ET.SubElement(parent, tag) |
|
37 | e = ET.SubElement(parent, tag) | |
37 | e.text = nbnode[attr] |
|
38 | e.text = nbnode[attr] | |
38 |
|
39 | |||
39 |
|
40 | |||
40 | def _get_int(e, tag): |
|
41 | def _get_int(e, tag): | |
41 | sub_e = e.find(tag) |
|
42 | sub_e = e.find(tag) | |
42 | if sub_e is None: |
|
43 | if sub_e is None: | |
43 | return None |
|
44 | return None | |
44 | else: |
|
45 | else: | |
45 | return int(sub_e.text) |
|
46 | return int(sub_e.text) | |
46 |
|
47 | |||
47 |
|
48 | |||
48 | def _set_int(nbnode, attr, parent, tag): |
|
49 | def _set_int(nbnode, attr, parent, tag): | |
49 | if attr in nbnode: |
|
50 | if attr in nbnode: | |
50 | e = ET.SubElement(parent, tag) |
|
51 | e = ET.SubElement(parent, tag) | |
51 | e.text = unicode(nbnode[attr]) |
|
52 | e.text = unicode(nbnode[attr]) | |
52 |
|
53 | |||
53 |
|
54 | |||
|
55 | def _get_binary(e, tag): | |||
|
56 | sub_e = e.find(tag) | |||
|
57 | if sub_e is None: | |||
|
58 | return None | |||
|
59 | else: | |||
|
60 | return decodestring(sub_e.text) | |||
|
61 | ||||
|
62 | ||||
|
63 | def _set_binary(nbnode, attr, parent, tag): | |||
|
64 | if attr in nbnode: | |||
|
65 | e = ET.SubElement(parent, tag) | |||
|
66 | e.text = encodestring(nbnode[attr]) | |||
|
67 | ||||
|
68 | ||||
54 | class XMLReader(NotebookReader): |
|
69 | class XMLReader(NotebookReader): | |
55 |
|
70 | |||
56 | def reads(self, s, **kwargs): |
|
71 | def reads(self, s, **kwargs): | |
57 | root = ET.fromstring(s) |
|
72 | root = ET.fromstring(s) | |
|
73 | return self.to_notebook(root, **kwargs) | |||
58 |
|
74 | |||
|
75 | def to_notebook(self, root, **kwargs): | |||
59 | nbname = _get_text(root,'name') |
|
76 | nbname = _get_text(root,'name') | |
60 | nbid = _get_text(root,'id') |
|
77 | nbid = _get_text(root,'id') | |
61 |
|
78 | |||
62 | worksheets = [] |
|
79 | worksheets = [] | |
63 | for ws_e in root.find('worksheets').getiterator('worksheet'): |
|
80 | for ws_e in root.find('worksheets').getiterator('worksheet'): | |
64 | wsname = _get_text(ws_e,'name') |
|
81 | wsname = _get_text(ws_e,'name') | |
65 | cells = [] |
|
82 | cells = [] | |
66 | for cell_e in ws_e.find('cells').getiterator(): |
|
83 | for cell_e in ws_e.find('cells').getiterator(): | |
67 | if cell_e.tag == 'codecell': |
|
84 | if cell_e.tag == 'codecell': | |
68 | input = _get_text(cell_e,'input') |
|
85 | input = _get_text(cell_e,'input') | |
69 | prompt_number = _get_int(cell_e,'prompt_number') |
|
86 | prompt_number = _get_int(cell_e,'prompt_number') | |
70 | language = _get_text(cell_e,'language') |
|
87 | language = _get_text(cell_e,'language') | |
71 | outputs = [] |
|
88 | outputs = [] | |
72 | for output_e in cell_e.find('outputs').getiterator('output'): |
|
89 | for output_e in cell_e.find('outputs').getiterator('output'): | |
73 | output_type = _get_text(output_e,'output_type') |
|
90 | output_type = _get_text(output_e,'output_type') | |
74 | output_text = _get_text(output_e,'text') |
|
91 | output_text = _get_text(output_e,'text') | |
75 |
output_png = _get_ |
|
92 | output_png = _get_binary(output_e,'png') | |
76 | output_svg = _get_text(output_e,'svg') |
|
93 | output_svg = _get_text(output_e,'svg') | |
77 | output_html = _get_text(output_e,'html') |
|
94 | output_html = _get_text(output_e,'html') | |
78 | output_latex = _get_text(output_e,'latex') |
|
95 | output_latex = _get_text(output_e,'latex') | |
79 | output_json = _get_text(output_e,'json') |
|
96 | output_json = _get_text(output_e,'json') | |
80 | output_javascript = _get_text(output_e,'javascript') |
|
97 | output_javascript = _get_text(output_e,'javascript') | |
81 | output = new_output(output_type=output_type,output_png=output_png, |
|
98 | output = new_output(output_type=output_type,output_png=output_png, | |
82 | output_text=output_text,output_svg=output_svg, |
|
99 | output_text=output_text,output_svg=output_svg, | |
83 | output_html=output_html,output_latex=output_latex, |
|
100 | output_html=output_html,output_latex=output_latex, | |
84 | output_json=output_json,output_javascript=output_javascript |
|
101 | output_json=output_json,output_javascript=output_javascript | |
85 | ) |
|
102 | ) | |
86 | outputs.append(output) |
|
103 | outputs.append(output) | |
87 | cc = new_code_cell(input=input,prompt_number=prompt_number, |
|
104 | cc = new_code_cell(input=input,prompt_number=prompt_number, | |
88 | language=language,outputs=outputs) |
|
105 | language=language,outputs=outputs) | |
89 | cells.append(cc) |
|
106 | cells.append(cc) | |
90 | if cell_e.tag == 'textcell': |
|
107 | if cell_e.tag == 'textcell': | |
91 | text = _get_text(cell_e,'text') |
|
108 | text = _get_text(cell_e,'text') | |
92 | cells.append(new_text_cell(text=text)) |
|
109 | cells.append(new_text_cell(text=text)) | |
93 | ws = new_worksheet(name=wsname,cells=cells) |
|
110 | ws = new_worksheet(name=wsname,cells=cells) | |
94 | worksheets.append(ws) |
|
111 | worksheets.append(ws) | |
95 |
|
112 | |||
96 | nb = new_notebook(name=nbname,id=nbid,worksheets=worksheets) |
|
113 | nb = new_notebook(name=nbname,id=nbid,worksheets=worksheets) | |
97 | return nb |
|
114 | return nb | |
98 |
|
115 | |||
99 |
|
116 | |||
100 | class XMLWriter(NotebookWriter): |
|
117 | class XMLWriter(NotebookWriter): | |
101 |
|
118 | |||
102 | def writes(self, nb, **kwargs): |
|
119 | def writes(self, nb, **kwargs): | |
103 | nb_e = ET.Element('notebook') |
|
120 | nb_e = ET.Element('notebook') | |
104 | _set_text(nb,'name',nb_e,'name') |
|
121 | _set_text(nb,'name',nb_e,'name') | |
105 | _set_text(nb,'id',nb_e,'id') |
|
122 | _set_text(nb,'id',nb_e,'id') | |
|
123 | _set_int(nb,'nbformat',nb_e,'nbformat') | |||
106 | wss_e = ET.SubElement(nb_e,'worksheets') |
|
124 | wss_e = ET.SubElement(nb_e,'worksheets') | |
107 | for ws in nb.worksheets: |
|
125 | for ws in nb.worksheets: | |
108 | ws_e = ET.SubElement(wss_e, 'worksheet') |
|
126 | ws_e = ET.SubElement(wss_e, 'worksheet') | |
109 | _set_text(ws,'name',ws_e,'name') |
|
127 | _set_text(ws,'name',ws_e,'name') | |
110 | cells_e = ET.SubElement(ws_e,'cells') |
|
128 | cells_e = ET.SubElement(ws_e,'cells') | |
111 | for cell in ws.cells: |
|
129 | for cell in ws.cells: | |
112 | cell_type = cell.cell_type |
|
130 | cell_type = cell.cell_type | |
113 | if cell_type == 'code': |
|
131 | if cell_type == 'code': | |
114 | cell_e = ET.SubElement(cells_e, 'codecell') |
|
132 | cell_e = ET.SubElement(cells_e, 'codecell') | |
115 | _set_text(cell,'input',cell_e,'input') |
|
133 | _set_text(cell,'input',cell_e,'input') | |
116 | _set_text(cell,'language',cell_e,'language') |
|
134 | _set_text(cell,'language',cell_e,'language') | |
117 | _set_int(cell,'prompt_number',cell_e,'prompt_number') |
|
135 | _set_int(cell,'prompt_number',cell_e,'prompt_number') | |
118 | outputs_e = ET.SubElement(cell_e, 'outputs') |
|
136 | outputs_e = ET.SubElement(cell_e, 'outputs') | |
119 | for output in cell.outputs: |
|
137 | for output in cell.outputs: | |
120 | output_e = ET.SubElement(outputs_e, 'output') |
|
138 | output_e = ET.SubElement(outputs_e, 'output') | |
121 | _set_text(output,'output_type',output_e,'output_type') |
|
139 | _set_text(output,'output_type',output_e,'output_type') | |
122 | _set_text(output,'text',output_e,'text') |
|
140 | _set_text(output,'text',output_e,'text') | |
123 |
_set_ |
|
141 | _set_binary(output,'png',output_e,'png') | |
124 | _set_text(output,'html',output_e,'html') |
|
142 | _set_text(output,'html',output_e,'html') | |
125 | _set_text(output,'svg',output_e,'svg') |
|
143 | _set_text(output,'svg',output_e,'svg') | |
126 | _set_text(output,'latex',output_e,'latex') |
|
144 | _set_text(output,'latex',output_e,'latex') | |
127 | _set_text(output,'json',output_e,'json') |
|
145 | _set_text(output,'json',output_e,'json') | |
128 | _set_text(output,'javascript',output_e,'javascript') |
|
146 | _set_text(output,'javascript',output_e,'javascript') | |
129 | elif cell_type == 'text': |
|
147 | elif cell_type == 'text': | |
130 | cell_e = ET.SubElement(cells_e, 'textcell') |
|
148 | cell_e = ET.SubElement(cells_e, 'textcell') | |
131 | _set_text(cell,'text',cell_e,'text') |
|
149 | _set_text(cell,'text',cell_e,'text') | |
132 |
|
150 | |||
133 | indent(nb_e) |
|
151 | indent(nb_e) | |
134 | txt = ET.tostring(nb_e, encoding="utf-8") |
|
152 | txt = ET.tostring(nb_e, encoding="utf-8") | |
135 | txt = '<?xml version="1.0" encoding="utf-8"?>\n' + txt |
|
153 | txt = '<?xml version="1.0" encoding="utf-8"?>\n' + txt | |
136 | return txt |
|
154 | return txt | |
137 |
|
155 | |||
138 |
|
156 | |||
139 | _reader = XMLReader() |
|
157 | _reader = XMLReader() | |
140 | _writer = XMLWriter() |
|
158 | _writer = XMLWriter() | |
141 |
|
159 | |||
142 | reads = _reader.reads |
|
160 | reads = _reader.reads | |
143 | read = _reader.read |
|
161 | read = _reader.read | |
|
162 | to_notebook = _reader.to_notebook | |||
144 | write = _writer.write |
|
163 | write = _writer.write | |
145 | writes = _writer.writes |
|
164 | writes = _writer.writes | |
|
165 |
@@ -1,46 +1,46 b'' | |||||
1 | from base64 import encodestring, decodestring |
|
1 | from base64 import encodestring, decodestring | |
2 |
|
2 | import pprint | ||
3 |
|
3 | |||
4 | def base64_decode(nb): |
|
4 | def base64_decode(nb): | |
5 | """Base64 encode all bytes objects in the notebook.""" |
|
5 | """Base64 encode all bytes objects in the notebook.""" | |
6 |
for ws in nb |
|
6 | for ws in nb.worksheets: | |
7 |
for cell in ws |
|
7 | for cell in ws.cells: | |
8 |
if cell |
|
8 | if cell.cell_type == 'code': | |
9 |
if cell |
|
9 | if 'png' in cell: | |
10 |
cell |
|
10 | cell.png = bytes(decodestring(cell.png)) | |
11 | return nb |
|
11 | return nb | |
12 |
|
12 | |||
13 |
|
13 | |||
14 | def base64_encode(nb): |
|
14 | def base64_encode(nb): | |
15 | """Base64 decode all binary objects in the notebook.""" |
|
15 | """Base64 decode all binary objects in the notebook.""" | |
16 |
for ws in nb |
|
16 | for ws in nb.worksheets: | |
17 |
for cell in ws |
|
17 | for cell in ws.cells: | |
18 |
if cell |
|
18 | if cell.cell_type == 'code': | |
19 |
if cell |
|
19 | if 'png' in cell: | |
20 |
cell |
|
20 | cell.png = unicode(encodestring(cell.png)) | |
21 | return nb |
|
21 | return nb | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | class NotebookReader(object): |
|
24 | class NotebookReader(object): | |
25 |
|
25 | |||
26 | def reads(self, s, **kwargs): |
|
26 | def reads(self, s, **kwargs): | |
27 | """Read a notebook from a string.""" |
|
27 | """Read a notebook from a string.""" | |
28 | raise NotImplementedError("loads must be implemented in a subclass") |
|
28 | raise NotImplementedError("loads must be implemented in a subclass") | |
29 |
|
29 | |||
30 | def read(self, fp, **kwargs): |
|
30 | def read(self, fp, **kwargs): | |
31 | """Read a notebook from a file like object""" |
|
31 | """Read a notebook from a file like object""" | |
32 |
return self. |
|
32 | return self.read(fp.read(), **kwargs) | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | class NotebookWriter(object): |
|
35 | class NotebookWriter(object): | |
36 |
|
36 | |||
37 | def writes(self, nb, **kwargs): |
|
37 | def writes(self, nb, **kwargs): | |
38 | """Write a notebook to a string.""" |
|
38 | """Write a notebook to a string.""" | |
39 | raise NotImplementedError("loads must be implemented in a subclass") |
|
39 | raise NotImplementedError("loads must be implemented in a subclass") | |
40 |
|
40 | |||
41 | def write(self, nb, fp, **kwargs): |
|
41 | def write(self, nb, fp, **kwargs): | |
42 | """Write a notebook to a file like object""" |
|
42 | """Write a notebook to a file like object""" | |
43 |
return fp.write(self. |
|
43 | return fp.write(self.writes(nb,**kwargs)) | |
44 |
|
44 | |||
45 |
|
45 | |||
46 |
|
46 |
@@ -1,67 +1,73 b'' | |||||
1 |
from |
|
1 | from ..nbbase import ( | |
2 | NotebookNode, |
|
2 | NotebookNode, | |
3 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output |
|
3 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output | |
4 | ) |
|
4 | ) | |
5 |
|
5 | |||
6 |
|
6 | |||
7 |
|
7 | |||
8 | ws = new_worksheet(name='worksheet1') |
|
8 | ws = new_worksheet(name='worksheet1') | |
9 |
|
9 | |||
10 | ws.cells.append(new_text_cell( |
|
10 | ws.cells.append(new_text_cell( | |
11 | text='Some NumPy Examples' |
|
11 | text='Some NumPy Examples' | |
12 | )) |
|
12 | )) | |
13 |
|
13 | |||
14 |
|
14 | |||
15 | ws.cells.append(new_code_cell( |
|
15 | ws.cells.append(new_code_cell( | |
16 | input='import numpy', |
|
16 | input='import numpy', | |
17 | prompt_number=1 |
|
17 | prompt_number=1 | |
18 | )) |
|
18 | )) | |
19 |
|
19 | |||
20 | ws.cells.append(new_code_cell( |
|
20 | ws.cells.append(new_code_cell( | |
21 | input='a = numpy.random.rand(100)', |
|
21 | input='a = numpy.random.rand(100)', | |
22 | prompt_number=2 |
|
22 | prompt_number=2 | |
23 | )) |
|
23 | )) | |
24 |
|
24 | |||
25 | ws.cells.append(new_code_cell( |
|
25 | ws.cells.append(new_code_cell( | |
26 | input='print a', |
|
26 | input='print a', | |
27 | prompt_number=3, |
|
27 | prompt_number=3, | |
28 | outputs=[new_output( |
|
28 | outputs=[new_output( | |
29 | output_type=u'pyout', |
|
29 | output_type=u'pyout', | |
30 | output_text=u'<array a>', |
|
30 | output_text=u'<array a>', | |
31 | output_html=u'The HTML rep', |
|
31 | output_html=u'The HTML rep', | |
32 | output_latex=u'$a$', |
|
32 | output_latex=u'$a$', | |
33 | output_png=b'data', |
|
33 | output_png=b'data', | |
34 | output_svg=u'<svg>', |
|
34 | output_svg=u'<svg>', | |
35 | output_json=u'json data', |
|
35 | output_json=u'json data', | |
36 | output_javascript=u'var i=0;' |
|
36 | output_javascript=u'var i=0;' | |
37 | ),new_output( |
|
37 | ),new_output( | |
38 | output_type=u'display_data', |
|
38 | output_type=u'display_data', | |
39 | output_text=u'<array a>', |
|
39 | output_text=u'<array a>', | |
40 | output_html=u'The HTML rep', |
|
40 | output_html=u'The HTML rep', | |
41 | output_latex=u'$a$', |
|
41 | output_latex=u'$a$', | |
42 | output_png=b'data', |
|
42 | output_png=b'data', | |
43 | output_svg=u'<svg>', |
|
43 | output_svg=u'<svg>', | |
44 | output_json=u'json data', |
|
44 | output_json=u'json data', | |
45 | output_javascript=u'var i=0;' |
|
45 | output_javascript=u'var i=0;' | |
46 | )] |
|
46 | )] | |
47 | )) |
|
47 | )) | |
48 |
|
48 | |||
49 | nb0 = new_notebook( |
|
49 | nb0 = new_notebook( | |
50 | name='nb0', |
|
50 | name='nb0', | |
51 | worksheets=[ws, new_worksheet(name='worksheet2')] |
|
51 | worksheets=[ws, new_worksheet(name='worksheet2')] | |
52 | ) |
|
52 | ) | |
53 |
|
53 | |||
54 | nb0_py = """# <codecell> |
|
54 | nb0_py = """# <nbformat>2</nbformat> | |
|
55 | ||||
|
56 | # <codecell> | |||
55 |
|
57 | |||
56 | import numpy |
|
58 | import numpy | |
57 |
|
59 | |||
|
60 | # </codecell> | |||
58 | # <codecell> |
|
61 | # <codecell> | |
59 |
|
62 | |||
60 | a = numpy.random.rand(100) |
|
63 | a = numpy.random.rand(100) | |
61 |
|
64 | |||
|
65 | # </codecell> | |||
62 | # <codecell> |
|
66 | # <codecell> | |
63 |
|
67 | |||
64 | print a |
|
68 | print a | |
|
69 | ||||
|
70 | # </codecell> | |||
65 | """ |
|
71 | """ | |
66 |
|
72 | |||
67 |
|
73 |
@@ -1,62 +1,64 b'' | |||||
1 | from unittest import TestCase |
|
1 | from unittest import TestCase | |
2 |
|
2 | |||
3 |
from |
|
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 | ) |
|
6 | ) | |
7 |
|
7 | |||
8 | class TestCell(TestCase): |
|
8 | class TestCell(TestCase): | |
9 |
|
9 | |||
10 | def test_empty_code_cell(self): |
|
10 | def test_empty_code_cell(self): | |
11 | cc = new_code_cell() |
|
11 | cc = new_code_cell() | |
12 | self.assertEquals(cc.cell_type,'code') |
|
12 | self.assertEquals(cc.cell_type,'code') | |
13 | self.assertEquals('input' not in cc, True) |
|
13 | self.assertEquals('input' not in cc, True) | |
14 | self.assertEquals('prompt_number' not in cc, True) |
|
14 | self.assertEquals('prompt_number' not in cc, True) | |
15 | self.assertEquals(cc.outputs, []) |
|
15 | self.assertEquals(cc.outputs, []) | |
16 |
|
16 | |||
17 | def test_code_cell(self): |
|
17 | def test_code_cell(self): | |
18 | cc = new_code_cell(input='a=10', prompt_number=0) |
|
18 | cc = new_code_cell(input='a=10', prompt_number=0) | |
19 | cc.outputs = [new_output(output_type='pyout',output_svg='foo',output_text='10')] |
|
19 | cc.outputs = [new_output(output_type='pyout',output_svg='foo',output_text='10')] | |
20 | self.assertEquals(cc.input, u'a=10') |
|
20 | self.assertEquals(cc.input, u'a=10') | |
21 | self.assertEquals(cc.prompt_number, 0) |
|
21 | self.assertEquals(cc.prompt_number, 0) | |
22 | self.assertEquals(cc.language, u'python') |
|
22 | self.assertEquals(cc.language, u'python') | |
23 | self.assertEquals(cc.outputs[0].svg, u'foo') |
|
23 | self.assertEquals(cc.outputs[0].svg, u'foo') | |
24 | self.assertEquals(cc.outputs[0].text, u'10') |
|
24 | self.assertEquals(cc.outputs[0].text, u'10') | |
25 |
|
25 | |||
26 | def test_empty_text_cell(self): |
|
26 | def test_empty_text_cell(self): | |
27 | tc = new_text_cell() |
|
27 | tc = new_text_cell() | |
28 | self.assertEquals(tc.cell_type, 'text') |
|
28 | self.assertEquals(tc.cell_type, 'text') | |
29 | self.assertEquals('text' not in tc, True) |
|
29 | self.assertEquals('text' not in tc, True) | |
30 |
|
30 | |||
31 | def test_text_cell(self): |
|
31 | def test_text_cell(self): | |
32 | tc = new_text_cell('hi') |
|
32 | tc = new_text_cell('hi') | |
33 | self.assertEquals(tc.text, u'hi') |
|
33 | self.assertEquals(tc.text, u'hi') | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | class TestWorksheet(TestCase): |
|
36 | class TestWorksheet(TestCase): | |
37 |
|
37 | |||
38 | def test_empty_worksheet(self): |
|
38 | def test_empty_worksheet(self): | |
39 | ws = new_worksheet() |
|
39 | ws = new_worksheet() | |
40 | self.assertEquals(ws.cells,[]) |
|
40 | self.assertEquals(ws.cells,[]) | |
41 | self.assertEquals('name' not in ws, True) |
|
41 | self.assertEquals('name' not in ws, True) | |
42 |
|
42 | |||
43 | def test_worksheet(self): |
|
43 | def test_worksheet(self): | |
44 | cells = [new_code_cell(), new_text_cell()] |
|
44 | cells = [new_code_cell(), new_text_cell()] | |
45 | ws = new_worksheet(cells=cells,name='foo') |
|
45 | ws = new_worksheet(cells=cells,name='foo') | |
46 | self.assertEquals(ws.cells,cells) |
|
46 | self.assertEquals(ws.cells,cells) | |
47 | self.assertEquals(ws.name,u'foo') |
|
47 | self.assertEquals(ws.name,u'foo') | |
48 |
|
48 | |||
49 | class TestNotebook(TestCase): |
|
49 | class TestNotebook(TestCase): | |
50 |
|
50 | |||
51 | def test_empty_notebook(self): |
|
51 | def test_empty_notebook(self): | |
52 | nb = new_notebook() |
|
52 | nb = new_notebook() | |
53 | self.assertEquals('id' in nb, True) |
|
53 | self.assertEquals('id' in nb, True) | |
54 | self.assertEquals(nb.worksheets, []) |
|
54 | self.assertEquals(nb.worksheets, []) | |
55 | self.assertEquals('name' not in nb, True) |
|
55 | self.assertEquals('name' not in nb, True) | |
|
56 | self.assertEquals(nb.nbformat,2) | |||
56 |
|
57 | |||
57 |
def test_notebook |
|
58 | def test_notebook(self): | |
58 | worksheets = [new_worksheet(),new_worksheet()] |
|
59 | worksheets = [new_worksheet(),new_worksheet()] | |
59 | nb = new_notebook(name='foo',worksheets=worksheets) |
|
60 | nb = new_notebook(name='foo',worksheets=worksheets) | |
60 | self.assertEquals(nb.name,u'foo') |
|
61 | self.assertEquals(nb.name,u'foo') | |
61 | self.assertEquals(nb.worksheets,worksheets) |
|
62 | self.assertEquals(nb.worksheets,worksheets) | |
|
63 | self.assertEquals(nb.nbformat,2) | |||
62 |
|
64 |
@@ -1,18 +1,18 b'' | |||||
1 | from unittest import TestCase |
|
1 | from unittest import TestCase | |
2 |
|
2 | |||
3 |
from |
|
3 | from ..nbbase import ( | |
4 | NotebookNode, |
|
4 | NotebookNode, | |
5 | new_code_cell, new_text_cell, new_worksheet, new_notebook |
|
5 | new_code_cell, new_text_cell, new_worksheet, new_notebook | |
6 | ) |
|
6 | ) | |
7 |
|
7 | |||
8 |
from |
|
8 | from ..nbpy import reads, writes | |
9 |
from |
|
9 | from .nbexamples import nb0, nb0_py | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | class TestPy(TestCase): |
|
12 | class TestPy(TestCase): | |
13 |
|
13 | |||
14 | def test_write(self): |
|
14 | def test_write(self): | |
15 | s = writes(nb0) |
|
15 | s = writes(nb0) | |
16 | self.assertEquals(s,nb0_py) |
|
16 | self.assertEquals(s,nb0_py) | |
17 |
|
17 | |||
18 |
|
18 |
@@ -1,18 +1,18 b'' | |||||
1 | from unittest import TestCase |
|
1 | from unittest import TestCase | |
2 |
|
2 | |||
3 |
from |
|
3 | from ..nbxml import reads, writes | |
4 |
from |
|
4 | from .nbexamples import nb0 | |
5 | import pprint |
|
5 | import pprint | |
6 |
|
6 | |||
7 | class TestXML(TestCase): |
|
7 | class TestXML(TestCase): | |
8 |
|
8 | |||
9 | def test_roundtrip(self): |
|
9 | def test_roundtrip(self): | |
10 | s = writes(nb0) |
|
10 | s = writes(nb0) | |
11 |
|
11 | |||
12 | # print pprint.pformat(nb0,indent=2) |
|
12 | # print pprint.pformat(nb0,indent=2) | |
13 |
|
13 | |||
14 | # print pprint.pformat(reads(s),indent=2) |
|
14 | # print pprint.pformat(reads(s),indent=2) | |
15 |
|
15 | |||
16 | # print s |
|
16 | # print s | |
17 | self.assertEquals(reads(s),nb0) |
|
17 | self.assertEquals(reads(s),nb0) | |
18 |
|
18 |
General Comments 0
You need to be logged in to leave comments.
Login now