##// END OF EJS Templates
Markdown cells are now saved and restored in notebooks.
Brian E. Granger -
Show More
@@ -624,6 +624,9 b' var IPython = (function (IPython) {'
624 } else if (cell_data.cell_type === 'html') {
624 } else if (cell_data.cell_type === 'html') {
625 new_cell = this.insert_html_cell_after();
625 new_cell = this.insert_html_cell_after();
626 new_cell.fromJSON(cell_data);
626 new_cell.fromJSON(cell_data);
627 } else if (cell_data.cell_type === 'markdown') {
628 new_cell = this.insert_markdown_cell_after();
629 new_cell.fromJSON(cell_data);
627 };
630 };
628 };
631 };
629 };
632 };
@@ -132,7 +132,7 b' var IPython = (function (IPython) {'
132 if (data.cell_type === this.cell_type) {
132 if (data.cell_type === this.cell_type) {
133 if (data.source !== undefined) {
133 if (data.source !== undefined) {
134 this.set_source(data.source);
134 this.set_source(data.source);
135 this.set_rendered(data.source);
135 this.set_rendered(data.rendered);
136 };
136 };
137 };
137 };
138 }
138 }
@@ -7,7 +7,7 b' 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_html_cell, new_notebook, new_output, new_worksheet
10 new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet
11 )
11 )
12
12
13
13
@@ -1,7 +1,7 b''
1
1
2 from .nbbase import (
2 from .nbbase import (
3 NotebookNode,
3 NotebookNode,
4 new_code_cell, new_html_cell, new_notebook, new_output, new_worksheet
4 new_code_cell, new_text_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
@@ -1,5 +1,5 b''
1 from .nbbase import (
1 from .nbbase import (
2 new_code_cell, new_html_cell, new_worksheet, new_notebook, new_output
2 new_code_cell, new_text_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):
@@ -7,10 +7,10 b' def convert_to_this_nbformat(nb, 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 == u'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 == u'text':
13 newcell = new_html_cell(source=cell.get('text'))
13 newcell = new_text_cell(u'markdown',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
@@ -65,12 +65,14 b" def new_code_cell(input=None, prompt_number=None, outputs=None, language=u'pytho"
65
65
66 return cell
66 return cell
67
67
68 def new_html_cell(source=None):
68 def new_text_cell(cell_type, source=None, rendered=None):
69 """Create a new text cell."""
69 """Create a new text cell."""
70 cell = NotebookNode()
70 cell = NotebookNode()
71 if source is not None:
71 if source is not None:
72 cell.source = unicode(source)
72 cell.source = unicode(source)
73 cell.cell_type = u'html'
73 if rendered is not None:
74 cell.rendered = unicode(rendered)
75 cell.cell_type = cell_type
74 return cell
76 return cell
75
77
76
78
@@ -5,7 +5,7 b' 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_html_cell, new_worksheet, new_notebook, new_output
8 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
9 )
9 )
10
10
11 def indent(elem, level=0):
11 def indent(elem, level=0):
@@ -108,7 +108,12 b' class XMLReader(NotebookReader):'
108 cells.append(cc)
108 cells.append(cc)
109 if cell_e.tag == 'htmlcell':
109 if cell_e.tag == 'htmlcell':
110 source = _get_text(cell_e,'source')
110 source = _get_text(cell_e,'source')
111 cells.append(new_html_cell(source=source))
111 rendered = _get_text(cell_e,'rendered')
112 cells.append(new_text_cell(u'html', source=source, rendered=rendered))
113 if cell_e.tag == 'markdowncell':
114 source = _get_text(cell_e,'source')
115 rendered = _get_text(cell_e,'rendered')
116 cells.append(new_text_cell(u'markdown', source=source, rendered=rendered))
112 ws = new_worksheet(name=wsname,cells=cells)
117 ws = new_worksheet(name=wsname,cells=cells)
113 worksheets.append(ws)
118 worksheets.append(ws)
114
119
@@ -150,6 +155,11 b' class XMLWriter(NotebookWriter):'
150 elif cell_type == 'html':
155 elif cell_type == 'html':
151 cell_e = ET.SubElement(cells_e, 'htmlcell')
156 cell_e = ET.SubElement(cells_e, 'htmlcell')
152 _set_text(cell,'source',cell_e,'source')
157 _set_text(cell,'source',cell_e,'source')
158 _set_text(cell,'rendered',cell_e,'rendered')
159 elif cell_type == 'markdown':
160 cell_e = ET.SubElement(cells_e, 'markdowncell')
161 _set_text(cell,'source',cell_e,'source')
162 _set_text(cell,'rendered',cell_e,'rendered')
153
163
154 indent(nb_e)
164 indent(nb_e)
155 txt = ET.tostring(nb_e, encoding="utf-8")
165 txt = ET.tostring(nb_e, encoding="utf-8")
@@ -1,14 +1,16 b''
1 from ..nbbase import (
1 from ..nbbase import (
2 NotebookNode,
2 NotebookNode,
3 new_code_cell, new_html_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_html_cell(
10 ws.cells.append(new_text_cell(
11 source='Some NumPy Examples'
11 u'html',
12 source='Some NumPy Examples',
13 rendered='Some NumPy Examples'
12 ))
14 ))
13
15
14
16
@@ -17,6 +19,12 b' ws.cells.append(new_code_cell('
17 prompt_number=1
19 prompt_number=1
18 ))
20 ))
19
21
22 ws.cells.append(new_text_cell(
23 u'markdown',
24 source='Some NumPy Examples',
25 rendered='Some NumPy Examples'
26 ))
27
20 ws.cells.append(new_code_cell(
28 ws.cells.append(new_code_cell(
21 input='a = numpy.random.rand(100)',
29 input='a = numpy.random.rand(100)',
22 prompt_number=2
30 prompt_number=2
@@ -2,7 +2,7 b' from unittest import TestCase'
2
2
3 from ..nbbase import (
3 from ..nbbase import (
4 NotebookNode,
4 NotebookNode,
5 new_code_cell, new_html_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):
@@ -26,13 +26,26 b' class TestCell(TestCase):'
26 self.assertEquals(cc.outputs[0].prompt_number, 0)
26 self.assertEquals(cc.outputs[0].prompt_number, 0)
27
27
28 def test_empty_html_cell(self):
28 def test_empty_html_cell(self):
29 tc = new_html_cell()
29 tc = new_text_cell(u'html')
30 self.assertEquals(tc.cell_type, 'html')
30 self.assertEquals(tc.cell_type, u'html')
31 self.assertEquals('source' not in tc, True)
31 self.assertEquals('source' not in tc, True)
32 self.assertEquals('rendered' not in tc, True)
32
33
33 def test_html_cell(self):
34 def test_html_cell(self):
34 tc = new_html_cell('hi')
35 tc = new_text_cell(u'html', 'hi', 'hi')
35 self.assertEquals(tc.source, u'hi')
36 self.assertEquals(tc.source, u'hi')
37 self.assertEquals(tc.rendered, u'hi')
38
39 def test_empty_markdown_cell(self):
40 tc = new_text_cell(u'markdown')
41 self.assertEquals(tc.cell_type, u'markdown')
42 self.assertEquals('source' not in tc, True)
43 self.assertEquals('rendered' not in tc, True)
44
45 def test_markdown_cell(self):
46 tc = new_text_cell(u'markdown', 'hi', 'hi')
47 self.assertEquals(tc.source, u'hi')
48 self.assertEquals(tc.rendered, u'hi')
36
49
37
50
38 class TestWorksheet(TestCase):
51 class TestWorksheet(TestCase):
@@ -43,7 +56,7 b' class TestWorksheet(TestCase):'
43 self.assertEquals('name' not in ws, True)
56 self.assertEquals('name' not in ws, True)
44
57
45 def test_worksheet(self):
58 def test_worksheet(self):
46 cells = [new_code_cell(), new_html_cell()]
59 cells = [new_code_cell(), new_text_cell(u'html')]
47 ws = new_worksheet(cells=cells,name='foo')
60 ws = new_worksheet(cells=cells,name='foo')
48 self.assertEquals(ws.cells,cells)
61 self.assertEquals(ws.cells,cells)
49 self.assertEquals(ws.name,u'foo')
62 self.assertEquals(ws.name,u'foo')
@@ -2,7 +2,7 b' from unittest import TestCase'
2
2
3 from ..nbbase import (
3 from ..nbbase import (
4 NotebookNode,
4 NotebookNode,
5 new_code_cell, new_html_cell, new_worksheet, new_notebook
5 new_code_cell, new_text_cell, new_worksheet, new_notebook
6 )
6 )
7
7
8 from ..nbpy import reads, writes
8 from ..nbpy import reads, writes
General Comments 0
You need to be logged in to leave comments. Login now