##// END OF EJS Templates
Added collapsed field to the code cell.
Brian E. Granger -
Show More
@@ -13,6 +13,7 b' var IPython = (function (IPython) {'
13 this.is_completing = false;
13 this.is_completing = false;
14 this.completion_cursor = null;
14 this.completion_cursor = null;
15 this.outputs = [];
15 this.outputs = [];
16 this.collapsed = false;
16 IPython.Cell.apply(this, arguments);
17 IPython.Cell.apply(this, arguments);
17 };
18 };
18
19
@@ -317,12 +318,18 b' var IPython = (function (IPython) {'
317
318
318
319
319 CodeCell.prototype.collapse = function () {
320 CodeCell.prototype.collapse = function () {
320 this.element.find('div.output').hide();
321 if (!this.collapsed) {
322 this.element.find('div.output').hide();
323 this.collapsed = true;
324 };
321 };
325 };
322
326
323
327
324 CodeCell.prototype.expand = function () {
328 CodeCell.prototype.expand = function () {
325 this.element.find('div.output').show();
329 if (this.collapsed) {
330 this.element.find('div.output').show();
331 this.collapsed = false;
332 };
326 };
333 };
327
334
328
335
@@ -378,6 +385,11 b' var IPython = (function (IPython) {'
378 for (var i=0; i<len; i++) {
385 for (var i=0; i<len; i++) {
379 this.append_output(data.outputs[i]);
386 this.append_output(data.outputs[i]);
380 };
387 };
388 if (data.collapsed !== undefined) {
389 if (data.collapsed) {
390 this.collapse();
391 };
392 };
381 };
393 };
382 };
394 };
383
395
@@ -396,6 +408,7 b' var IPython = (function (IPython) {'
396 };
408 };
397 data.outputs = outputs;
409 data.outputs = outputs;
398 data.language = 'python';
410 data.language = 'python';
411 data.collapsed = this.collapsed;
399 // console.log('Export to JSON:',data);
412 // console.log('Export to JSON:',data);
400 return data;
413 return data;
401 };
414 };
@@ -50,7 +50,8 b' def new_output(output_type=None, output_text=None, output_png=None,'
50 return output
50 return output
51
51
52
52
53 def new_code_cell(input=None, prompt_number=None, outputs=None, language=u'python'):
53 def new_code_cell(input=None, prompt_number=None, outputs=None,
54 language=u'python', collapsed=False):
54 """Create a new code cell with input and output"""
55 """Create a new code cell with input and output"""
55 cell = NotebookNode()
56 cell = NotebookNode()
56 cell.cell_type = u'code'
57 cell.cell_type = u'code'
@@ -64,6 +65,8 b" def new_code_cell(input=None, prompt_number=None, outputs=None, language=u'pytho"
64 cell.outputs = []
65 cell.outputs = []
65 else:
66 else:
66 cell.outputs = outputs
67 cell.outputs = outputs
68 if collapsed is not None:
69 cell.collapsed = collapsed
67
70
68 return cell
71 return cell
69
72
@@ -52,6 +52,23 b' def _set_int(nbnode, attr, parent, tag):'
52 e.text = unicode(nbnode[attr])
52 e.text = unicode(nbnode[attr])
53
53
54
54
55 def _get_bool(e, tag):
56 sub_e = e.find(tag)
57 if sub_e is None:
58 return None
59 else:
60 return bool(int(sub_e.text))
61
62
63 def _set_bool(nbnode, attr, parent, tag):
64 if attr in nbnode:
65 e = ET.SubElement(parent, tag)
66 if nbnode[attr]:
67 e.text = u'1'
68 else:
69 e.text = u'0'
70
71
55 def _get_binary(e, tag):
72 def _get_binary(e, tag):
56 sub_e = e.find(tag)
73 sub_e = e.find(tag)
57 if sub_e is None:
74 if sub_e is None:
@@ -84,6 +101,7 b' class XMLReader(NotebookReader):'
84 if cell_e.tag == 'codecell':
101 if cell_e.tag == 'codecell':
85 input = _get_text(cell_e,'input')
102 input = _get_text(cell_e,'input')
86 prompt_number = _get_int(cell_e,'prompt_number')
103 prompt_number = _get_int(cell_e,'prompt_number')
104 collapsed = _get_bool(cell_e,'collapsed')
87 language = _get_text(cell_e,'language')
105 language = _get_text(cell_e,'language')
88 outputs = []
106 outputs = []
89 for output_e in cell_e.find('outputs').getiterator('output'):
107 for output_e in cell_e.find('outputs').getiterator('output'):
@@ -105,7 +123,7 b' class XMLReader(NotebookReader):'
105 )
123 )
106 outputs.append(output)
124 outputs.append(output)
107 cc = new_code_cell(input=input,prompt_number=prompt_number,
125 cc = new_code_cell(input=input,prompt_number=prompt_number,
108 language=language,outputs=outputs)
126 language=language,outputs=outputs,collapsed=collapsed)
109 cells.append(cc)
127 cells.append(cc)
110 if cell_e.tag == 'htmlcell':
128 if cell_e.tag == 'htmlcell':
111 source = _get_text(cell_e,'source')
129 source = _get_text(cell_e,'source')
@@ -141,6 +159,7 b' class XMLWriter(NotebookWriter):'
141 _set_text(cell,'input',cell_e,'input')
159 _set_text(cell,'input',cell_e,'input')
142 _set_text(cell,'language',cell_e,'language')
160 _set_text(cell,'language',cell_e,'language')
143 _set_int(cell,'prompt_number',cell_e,'prompt_number')
161 _set_int(cell,'prompt_number',cell_e,'prompt_number')
162 _set_bool(cell,'collapsed',cell_e,'collapsed')
144 outputs_e = ET.SubElement(cell_e, 'outputs')
163 outputs_e = ET.SubElement(cell_e, 'outputs')
145 for output in cell.outputs:
164 for output in cell.outputs:
146 output_e = ET.SubElement(outputs_e, 'output')
165 output_e = ET.SubElement(outputs_e, 'output')
@@ -16,7 +16,8 b' ws.cells.append(new_text_cell('
16
16
17 ws.cells.append(new_code_cell(
17 ws.cells.append(new_code_cell(
18 input='import numpy',
18 input='import numpy',
19 prompt_number=1
19 prompt_number=1,
20 collapsed=False
20 ))
21 ))
21
22
22 ws.cells.append(new_text_cell(
23 ws.cells.append(new_text_cell(
@@ -27,12 +28,14 b' ws.cells.append(new_text_cell('
27
28
28 ws.cells.append(new_code_cell(
29 ws.cells.append(new_code_cell(
29 input='a = numpy.random.rand(100)',
30 input='a = numpy.random.rand(100)',
30 prompt_number=2
31 prompt_number=2,
32 collapsed=True
31 ))
33 ))
32
34
33 ws.cells.append(new_code_cell(
35 ws.cells.append(new_code_cell(
34 input='print a',
36 input='print a',
35 prompt_number=3,
37 prompt_number=3,
38 collapsed=False,
36 outputs=[new_output(
39 outputs=[new_output(
37 output_type=u'pyout',
40 output_type=u'pyout',
38 output_text=u'<array a>',
41 output_text=u'<array a>',
@@ -1,4 +1,4 b''
1
1 import pprint
2 from unittest import TestCase
2 from unittest import TestCase
3
3
4 from ..nbjson import reads, writes
4 from ..nbjson import reads, writes
@@ -9,6 +9,12 b' class TestJSON(TestCase):'
9
9
10 def test_roundtrip(self):
10 def test_roundtrip(self):
11 s = writes(nb0)
11 s = writes(nb0)
12 # print
13 # print pprint.pformat(nb0,indent=2)
14 # print
15 # print pprint.pformat(reads(s),indent=2)
16 # print
17 # print s
12 self.assertEquals(reads(s),nb0)
18 self.assertEquals(reads(s),nb0)
13
19
14
20
@@ -13,9 +13,10 b' class TestCell(TestCase):'
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 self.assertEquals(cc.collapsed, False)
16
17
17 def test_code_cell(self):
18 def test_code_cell(self):
18 cc = new_code_cell(input='a=10', prompt_number=0)
19 cc = new_code_cell(input='a=10', prompt_number=0, collapsed=True)
19 cc.outputs = [new_output(output_type='pyout',
20 cc.outputs = [new_output(output_type='pyout',
20 output_svg='foo',output_text='10',prompt_number=0)]
21 output_svg='foo',output_text='10',prompt_number=0)]
21 self.assertEquals(cc.input, u'a=10')
22 self.assertEquals(cc.input, u'a=10')
@@ -24,6 +25,8 b' class TestCell(TestCase):'
24 self.assertEquals(cc.outputs[0].svg, u'foo')
25 self.assertEquals(cc.outputs[0].svg, u'foo')
25 self.assertEquals(cc.outputs[0].text, u'10')
26 self.assertEquals(cc.outputs[0].text, u'10')
26 self.assertEquals(cc.outputs[0].prompt_number, 0)
27 self.assertEquals(cc.outputs[0].prompt_number, 0)
28 self.assertEquals(cc.collapsed, True)
29
27
30
28 def test_empty_html_cell(self):
31 def test_empty_html_cell(self):
29 tc = new_text_cell(u'html')
32 tc = new_text_cell(u'html')
General Comments 0
You need to be logged in to leave comments. Login now