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