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