##// END OF EJS Templates
Updates to basic notebook format....
Brian E. Granger -
Show More
@@ -10,18 +10,13 b' class NotebookNode(Struct):'
10 10 pass
11 11
12 12
13 def new_code_cell(input=None, prompt_number=None, output_text=None, output_png=None,
13 def new_output(output_type=None, output_text=None, output_png=None,
14 14 output_html=None, output_svg=None, output_latex=None, output_json=None,
15 15 output_javascript=None):
16 16 """Create a new code cell with input and output"""
17 cell = NotebookNode()
18 cell.cell_type = 'code'
19 if input is not None:
20 cell.input = unicode(input)
21 if prompt_number is not None:
22 cell.prompt_number = int(prompt_number)
23
24 17 output = NotebookNode()
18 if output_type is not None:
19 output.output_type = unicode(output_type)
25 20 if output_text is not None:
26 21 output.text = unicode(output_text)
27 22 if output_png is not None:
@@ -37,16 +32,32 b' def new_code_cell(input=None, prompt_number=None, output_text=None, output_png=N'
37 32 if output_javascript is not None:
38 33 output.javascript = unicode(output_javascript)
39 34
40 cell.output = output
41 return cell
35 return output
42 36
43 37
38 def new_code_cell(input=None, prompt_number=None, outputs=None, language=u'python'):
39 """Create a new code cell with input and output"""
40 cell = NotebookNode()
41 cell.cell_type = u'code'
42 if language is not None:
43 cell.language = unicode(language)
44 if input is not None:
45 cell.input = unicode(input)
46 if prompt_number is not None:
47 cell.prompt_number = int(prompt_number)
48 if outputs is None:
49 cell.outputs = []
50 else:
51 cell.outputs = outputs
52
53 return cell
54
44 55 def new_text_cell(text=None):
45 56 """Create a new text cell."""
46 57 cell = NotebookNode()
47 58 if text is not None:
48 59 cell.text = unicode(text)
49 cell.cell_type = 'text'
60 cell.cell_type = u'text'
50 61 return cell
51 62
52 63
@@ -3,8 +3,9 b''
3 3 from xml.etree import ElementTree as ET
4 4
5 5 from .rwbase import NotebookReader, NotebookWriter
6 from .nbbase import new_code_cell, new_text_cell, new_worksheet, new_notebook
7
6 from .nbbase import (
7 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
8 )
8 9
9 10 def indent(elem, level=0):
10 11 i = "\n" + level*" "
@@ -30,6 +31,26 b' def _get_text(e, tag):'
30 31 return sub_e.text
31 32
32 33
34 def _set_text(nbnode, attr, parent, tag):
35 if attr in nbnode:
36 e = ET.SubElement(parent, tag)
37 e.text = nbnode[attr]
38
39
40 def _get_int(e, tag):
41 sub_e = e.find(tag)
42 if sub_e is None:
43 return None
44 else:
45 return int(sub_e.text)
46
47
48 def _set_int(nbnode, attr, parent, tag):
49 if attr in nbnode:
50 e = ET.SubElement(parent, tag)
51 e.text = unicode(nbnode[attr])
52
53
33 54 class XMLReader(NotebookReader):
34 55
35 56 def reads(self, s, **kwargs):
@@ -39,14 +60,17 b' class XMLReader(NotebookReader):'
39 60 nbid = _get_text(root,'id')
40 61
41 62 worksheets = []
42 for ws_e in root.getiterator('worksheet'):
63 for ws_e in root.find('worksheets').getiterator('worksheet'):
43 64 wsname = _get_text(ws_e,'name')
44 65 cells = []
45 for cell_e in ws_e.getiterator():
66 for cell_e in ws_e.find('cells').getiterator():
46 67 if cell_e.tag == 'codecell':
47 68 input = _get_text(cell_e,'input')
48 output_e = cell_e.find('output')
49 if output_e is not None:
69 prompt_number = _get_int(cell_e,'prompt_number')
70 language = _get_text(cell_e,'language')
71 outputs = []
72 for output_e in cell_e.find('outputs').getiterator('output'):
73 output_type = _get_text(output_e,'output_type')
50 74 output_text = _get_text(output_e,'text')
51 75 output_png = _get_text(output_e,'png')
52 76 output_svg = _get_text(output_e,'svg')
@@ -54,11 +78,14 b' class XMLReader(NotebookReader):'
54 78 output_latex = _get_text(output_e,'latex')
55 79 output_json = _get_text(output_e,'json')
56 80 output_javascript = _get_text(output_e,'javascript')
57 cc = new_code_cell(input=input,output_png=output_png,
58 output_text=output_text,output_svg=output_svg,
59 output_html=output_html,output_latex=output_latex,
60 output_json=output_json,output_javascript=output_javascript
61 )
81 output = new_output(output_type=output_type,output_png=output_png,
82 output_text=output_text,output_svg=output_svg,
83 output_html=output_html,output_latex=output_latex,
84 output_json=output_json,output_javascript=output_javascript
85 )
86 outputs.append(output)
87 cc = new_code_cell(input=input,prompt_number=prompt_number,
88 language=language,outputs=outputs)
62 89 cells.append(cc)
63 90 if cell_e.tag == 'textcell':
64 91 text = _get_text(cell_e,'text')
@@ -74,57 +101,34 b' class XMLWriter(NotebookWriter):'
74 101
75 102 def writes(self, nb, **kwargs):
76 103 nb_e = ET.Element('notebook')
77 if 'name' in nb:
78 name_e = ET.SubElement(nb_e, 'name')
79 name_e.text = nb.name
80 if 'id' in nb:
81 id_e = ET.SubElement(nb_e, 'id')
82 id_e.text = nb.id
104 _set_text(nb,'name',nb_e,'name')
105 _set_text(nb,'id',nb_e,'id')
106 wss_e = ET.SubElement(nb_e,'worksheets')
83 107 for ws in nb.worksheets:
84 ws_e = ET.SubElement(nb_e, 'worksheet')
85 if 'name' in ws:
86 ws_name_e = ET.SubElement(ws_e, 'name')
87 ws_name_e.text = ws.name
108 ws_e = ET.SubElement(wss_e, 'worksheet')
109 _set_text(ws,'name',ws_e,'name')
110 cells_e = ET.SubElement(ws_e,'cells')
88 111 for cell in ws.cells:
89 112 cell_type = cell.cell_type
90 113 if cell_type == 'code':
91 output = cell.output
92 cell_e = ET.SubElement(ws_e, 'codecell')
93 output_e = ET.SubElement(cell_e, 'output')
94
95 if 'input' in cell:
96 input_e = ET.SubElement(cell_e, 'input')
97 input_e.text = cell.input
98 if 'prompt_number' in cell:
99 prompt_number_e = ET.SubElement(cell_e, 'prompt_number')
100 input_e.text = cell.prompt_number
101
102 if 'text' in output:
103 text_e = ET.SubElement(output_e, 'text')
104 text_e.text = output.text
105 if 'png' in output:
106 png_e = ET.SubElement(output_e, 'png')
107 png_e.text = output.png
108 if 'html' in output:
109 html_e = ET.SubElement(output_e, 'html')
110 html_e.text = output.html
111 if 'svg' in output:
112 svg_e = ET.SubElement(output_e, 'svg')
113 svg_e.text = output.svg
114 if 'latex' in output:
115 latex_e = ET.SubElement(output_e, 'latex')
116 latex_e.text = output.latex
117 if 'json' in output:
118 json_e = ET.SubElement(output_e, 'json')
119 json_e.text = output.json
120 if 'javascript' in output:
121 javascript_e = ET.SubElement(output_e, 'javascript')
122 javascript_e.text = output.javascript
114 cell_e = ET.SubElement(cells_e, 'codecell')
115 _set_text(cell,'input',cell_e,'input')
116 _set_text(cell,'language',cell_e,'language')
117 _set_int(cell,'prompt_number',cell_e,'prompt_number')
118 outputs_e = ET.SubElement(cell_e, 'outputs')
119 for output in cell.outputs:
120 output_e = ET.SubElement(outputs_e, 'output')
121 _set_text(output,'output_type',output_e,'output_type')
122 _set_text(output,'text',output_e,'text')
123 _set_text(output,'png',output_e,'png')
124 _set_text(output,'html',output_e,'html')
125 _set_text(output,'svg',output_e,'svg')
126 _set_text(output,'latex',output_e,'latex')
127 _set_text(output,'json',output_e,'json')
128 _set_text(output,'javascript',output_e,'javascript')
123 129 elif cell_type == 'text':
124 cell_e = ET.SubElement(ws_e, 'textcell')
125 if 'text' in cell:
126 cell_text_e = ET.SubElement(cell_e, 'text')
127 cell_text_e.text = cell.text
130 cell_e = ET.SubElement(cells_e, 'textcell')
131 _set_text(cell,'text',cell_e,'text')
128 132
129 133 indent(nb_e)
130 134 txt = ET.tostring(nb_e, encoding="utf-8")
@@ -1,6 +1,6 b''
1 1 from IPython.nbformat.nbbase import (
2 2 NotebookNode,
3 new_code_cell, new_text_cell, new_worksheet, new_notebook
3 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
4 4 )
5 5
6 6
@@ -13,22 +13,37 b' ws.cells.append(new_text_cell('
13 13
14 14
15 15 ws.cells.append(new_code_cell(
16 input='import numpy'
16 input='import numpy',
17 prompt_number=1
17 18 ))
18 19
19 20 ws.cells.append(new_code_cell(
20 input='a = numpy.random.rand(100)'
21 input='a = numpy.random.rand(100)',
22 prompt_number=2
21 23 ))
22 24
23 25 ws.cells.append(new_code_cell(
24 26 input='print a',
25 output_text='<array a>',
26 output_html='The HTML rep',
27 output_latex='$a$',
28 output_png=b'data',
29 output_svg='<svg>',
30 output_json='json data',
31 output_javascript='var i=0;'
27 prompt_number=3,
28 outputs=[new_output(
29 output_type=u'pyout',
30 output_text=u'<array a>',
31 output_html=u'The HTML rep',
32 output_latex=u'$a$',
33 output_png=b'data',
34 output_svg=u'<svg>',
35 output_json=u'json data',
36 output_javascript=u'var i=0;'
37 ),new_output(
38 output_type=u'display_data',
39 output_text=u'<array a>',
40 output_html=u'The HTML rep',
41 output_latex=u'$a$',
42 output_png=b'data',
43 output_svg=u'<svg>',
44 output_json=u'json data',
45 output_javascript=u'var i=0;'
46 )]
32 47 ))
33 48
34 49 nb0 = new_notebook(
@@ -2,7 +2,7 b' from unittest import TestCase'
2 2
3 3 from IPython.nbformat.nbbase import (
4 4 NotebookNode,
5 new_code_cell, new_text_cell, new_worksheet, new_notebook
5 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
6 6 )
7 7
8 8 class TestCell(TestCase):
@@ -12,14 +12,16 b' class TestCell(TestCase):'
12 12 self.assertEquals(cc.cell_type,'code')
13 13 self.assertEquals('input' not in cc, True)
14 14 self.assertEquals('prompt_number' not in cc, True)
15 self.assertEquals(cc.output, NotebookNode())
15 self.assertEquals(cc.outputs, [])
16 16
17 17 def test_code_cell(self):
18 cc = new_code_cell(input='a=10', prompt_number=0, output_svg='foo', output_text='10')
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 20 self.assertEquals(cc.input, u'a=10')
20 21 self.assertEquals(cc.prompt_number, 0)
21 self.assertEquals(cc.output.svg, u'foo')
22 self.assertEquals(cc.output.text, u'10')
22 self.assertEquals(cc.language, u'python')
23 self.assertEquals(cc.outputs[0].svg, u'foo')
24 self.assertEquals(cc.outputs[0].text, u'10')
23 25
24 26 def test_empty_text_cell(self):
25 27 tc = new_text_cell()
@@ -2,11 +2,17 b' from unittest import TestCase'
2 2
3 3 from IPython.nbformat.nbxml import reads, writes
4 4 from IPython.nbformat.tests.nbexamples import nb0
5
5 import pprint
6 6
7 7 class TestXML(TestCase):
8 8
9 9 def test_roundtrip(self):
10 10 s = writes(nb0)
11 # print
12 # print pprint.pformat(nb0,indent=2)
13 # print
14 # print pprint.pformat(reads(s),indent=2)
15 # print
16 # print s
11 17 self.assertEquals(reads(s),nb0)
12 18
@@ -299,7 +299,7 b' def make_runners():'
299 299
300 300 # Packages to be tested via nose, that only depend on the stdlib
301 301 nose_pkg_names = ['config', 'core', 'extensions', 'frontend', 'lib',
302 'scripts', 'testing', 'utils' ]
302 'scripts', 'testing', 'utils', 'nbformat' ]
303 303
304 304 if have['zmq']:
305 305 nose_pkg_names.append('parallel')
@@ -126,6 +126,7 b' def find_packages():'
126 126 add_package(packages, 'frontend.qt.console', tests=True)
127 127 add_package(packages, 'frontend.terminal', tests=True)
128 128 add_package(packages, 'lib', tests=True)
129 add_package(packages, 'nbformat', tests=True)
129 130 add_package(packages, 'parallel', tests=True, scripts=True,
130 131 others=['apps','engine','client','controller'])
131 132 add_package(packages, 'quarantine', tests=True)
General Comments 0
You need to be logged in to leave comments. Login now