Skip to content

Commit

Permalink
Markdown cells are now saved and restored in notebooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisonbg committed Aug 9, 2011
1 parent 978f742 commit a85c3e7
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 20 deletions.
3 changes: 3 additions & 0 deletions IPython/frontend/html/notebook/static/js/notebook.js
Expand Up @@ -624,6 +624,9 @@ var IPython = (function (IPython) {
} else if (cell_data.cell_type === 'html') { } else if (cell_data.cell_type === 'html') {
new_cell = this.insert_html_cell_after(); new_cell = this.insert_html_cell_after();
new_cell.fromJSON(cell_data); new_cell.fromJSON(cell_data);
} else if (cell_data.cell_type === 'markdown') {
new_cell = this.insert_markdown_cell_after();
new_cell.fromJSON(cell_data);
}; };
}; };
}; };
Expand Down
2 changes: 1 addition & 1 deletion IPython/frontend/html/notebook/static/js/textcell.js
Expand Up @@ -132,7 +132,7 @@ var IPython = (function (IPython) {
if (data.cell_type === this.cell_type) { if (data.cell_type === this.cell_type) {
if (data.source !== undefined) { if (data.source !== undefined) {
this.set_source(data.source); this.set_source(data.source);
this.set_rendered(data.source); this.set_rendered(data.rendered);
}; };
}; };
} }
Expand Down
2 changes: 1 addition & 1 deletion IPython/nbformat/current.py
Expand Up @@ -7,7 +7,7 @@


from IPython.nbformat.v2 import ( from IPython.nbformat.v2 import (
NotebookNode, NotebookNode,
new_code_cell, new_html_cell, new_notebook, new_output, new_worksheet new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet
) )




Expand Down
2 changes: 1 addition & 1 deletion IPython/nbformat/v2/__init__.py
@@ -1,7 +1,7 @@


from .nbbase import ( from .nbbase import (
NotebookNode, NotebookNode,
new_code_cell, new_html_cell, new_notebook, new_output, new_worksheet new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet
) )


from .nbjson import reads as reads_json, writes as writes_json from .nbjson import reads as reads_json, writes as writes_json
Expand Down
8 changes: 4 additions & 4 deletions IPython/nbformat/v2/convert.py
@@ -1,16 +1,16 @@
from .nbbase import ( from .nbbase import (
new_code_cell, new_html_cell, new_worksheet, new_notebook, new_output new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
) )


def convert_to_this_nbformat(nb, orig_version=1): def convert_to_this_nbformat(nb, orig_version=1):
if orig_version == 1: if orig_version == 1:
newnb = new_notebook() newnb = new_notebook()
ws = new_worksheet() ws = new_worksheet()
for cell in nb.cells: for cell in nb.cells:
if cell.cell_type == 'code': if cell.cell_type == u'code':
newcell = new_code_cell(input=cell.get('code'),prompt_number=cell.get('prompt_number')) newcell = new_code_cell(input=cell.get('code'),prompt_number=cell.get('prompt_number'))
elif cell.cell_type == 'text': elif cell.cell_type == u'text':
newcell = new_html_cell(source=cell.get('text')) newcell = new_text_cell(u'markdown',source=cell.get('text'))
ws.cells.append(newcell) ws.cells.append(newcell)
newnb.worksheets.append(ws) newnb.worksheets.append(ws)
return newnb return newnb
Expand Down
6 changes: 4 additions & 2 deletions IPython/nbformat/v2/nbbase.py
Expand Up @@ -65,12 +65,14 @@ def new_code_cell(input=None, prompt_number=None, outputs=None, language=u'pytho


return cell return cell


def new_html_cell(source=None): def new_text_cell(cell_type, source=None, rendered=None):
"""Create a new text cell.""" """Create a new text cell."""
cell = NotebookNode() cell = NotebookNode()
if source is not None: if source is not None:
cell.source = unicode(source) cell.source = unicode(source)
cell.cell_type = u'html' if rendered is not None:
cell.rendered = unicode(rendered)
cell.cell_type = cell_type
return cell return cell




Expand Down
14 changes: 12 additions & 2 deletions IPython/nbformat/v2/nbxml.py
Expand Up @@ -5,7 +5,7 @@


from .rwbase import NotebookReader, NotebookWriter from .rwbase import NotebookReader, NotebookWriter
from .nbbase import ( from .nbbase import (
new_code_cell, new_html_cell, new_worksheet, new_notebook, new_output new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
) )


def indent(elem, level=0): def indent(elem, level=0):
Expand Down Expand Up @@ -108,7 +108,12 @@ def to_notebook(self, root, **kwargs):
cells.append(cc) cells.append(cc)
if cell_e.tag == 'htmlcell': if cell_e.tag == 'htmlcell':
source = _get_text(cell_e,'source') source = _get_text(cell_e,'source')
cells.append(new_html_cell(source=source)) rendered = _get_text(cell_e,'rendered')
cells.append(new_text_cell(u'html', source=source, rendered=rendered))
if cell_e.tag == 'markdowncell':
source = _get_text(cell_e,'source')
rendered = _get_text(cell_e,'rendered')
cells.append(new_text_cell(u'markdown', source=source, rendered=rendered))
ws = new_worksheet(name=wsname,cells=cells) ws = new_worksheet(name=wsname,cells=cells)
worksheets.append(ws) worksheets.append(ws)


Expand Down Expand Up @@ -150,6 +155,11 @@ def writes(self, nb, **kwargs):
elif cell_type == 'html': elif cell_type == 'html':
cell_e = ET.SubElement(cells_e, 'htmlcell') cell_e = ET.SubElement(cells_e, 'htmlcell')
_set_text(cell,'source',cell_e,'source') _set_text(cell,'source',cell_e,'source')
_set_text(cell,'rendered',cell_e,'rendered')
elif cell_type == 'markdown':
cell_e = ET.SubElement(cells_e, 'markdowncell')
_set_text(cell,'source',cell_e,'source')
_set_text(cell,'rendered',cell_e,'rendered')


indent(nb_e) indent(nb_e)
txt = ET.tostring(nb_e, encoding="utf-8") txt = ET.tostring(nb_e, encoding="utf-8")
Expand Down
14 changes: 11 additions & 3 deletions IPython/nbformat/v2/tests/nbexamples.py
@@ -1,14 +1,16 @@
from ..nbbase import ( from ..nbbase import (
NotebookNode, NotebookNode,
new_code_cell, new_html_cell, new_worksheet, new_notebook, new_output new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
) )






ws = new_worksheet(name='worksheet1') ws = new_worksheet(name='worksheet1')


ws.cells.append(new_html_cell( ws.cells.append(new_text_cell(
source='Some NumPy Examples' u'html',
source='Some NumPy Examples',
rendered='Some NumPy Examples'
)) ))




Expand All @@ -17,6 +19,12 @@
prompt_number=1 prompt_number=1
)) ))


ws.cells.append(new_text_cell(
u'markdown',
source='Some NumPy Examples',
rendered='Some NumPy Examples'
))

ws.cells.append(new_code_cell( ws.cells.append(new_code_cell(
input='a = numpy.random.rand(100)', input='a = numpy.random.rand(100)',
prompt_number=2 prompt_number=2
Expand Down
23 changes: 18 additions & 5 deletions IPython/nbformat/v2/tests/test_nbbase.py
Expand Up @@ -2,7 +2,7 @@


from ..nbbase import ( from ..nbbase import (
NotebookNode, NotebookNode,
new_code_cell, new_html_cell, new_worksheet, new_notebook, new_output new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output
) )


class TestCell(TestCase): class TestCell(TestCase):
Expand All @@ -26,13 +26,26 @@ def test_code_cell(self):
self.assertEquals(cc.outputs[0].prompt_number, 0) self.assertEquals(cc.outputs[0].prompt_number, 0)


def test_empty_html_cell(self): def test_empty_html_cell(self):
tc = new_html_cell() tc = new_text_cell(u'html')
self.assertEquals(tc.cell_type, 'html') self.assertEquals(tc.cell_type, u'html')
self.assertEquals('source' not in tc, True) self.assertEquals('source' not in tc, True)
self.assertEquals('rendered' not in tc, True)


def test_html_cell(self): def test_html_cell(self):
tc = new_html_cell('hi') tc = new_text_cell(u'html', 'hi', 'hi')
self.assertEquals(tc.source, u'hi') self.assertEquals(tc.source, u'hi')
self.assertEquals(tc.rendered, u'hi')

def test_empty_markdown_cell(self):
tc = new_text_cell(u'markdown')
self.assertEquals(tc.cell_type, u'markdown')
self.assertEquals('source' not in tc, True)
self.assertEquals('rendered' not in tc, True)

def test_markdown_cell(self):
tc = new_text_cell(u'markdown', 'hi', 'hi')
self.assertEquals(tc.source, u'hi')
self.assertEquals(tc.rendered, u'hi')




class TestWorksheet(TestCase): class TestWorksheet(TestCase):
Expand All @@ -43,7 +56,7 @@ def test_empty_worksheet(self):
self.assertEquals('name' not in ws, True) self.assertEquals('name' not in ws, True)


def test_worksheet(self): def test_worksheet(self):
cells = [new_code_cell(), new_html_cell()] cells = [new_code_cell(), new_text_cell(u'html')]
ws = new_worksheet(cells=cells,name='foo') ws = new_worksheet(cells=cells,name='foo')
self.assertEquals(ws.cells,cells) self.assertEquals(ws.cells,cells)
self.assertEquals(ws.name,u'foo') self.assertEquals(ws.name,u'foo')
Expand Down
2 changes: 1 addition & 1 deletion IPython/nbformat/v2/tests/test_nbpy.py
Expand Up @@ -2,7 +2,7 @@


from ..nbbase import ( from ..nbbase import (
NotebookNode, NotebookNode,
new_code_cell, new_html_cell, new_worksheet, new_notebook new_code_cell, new_text_cell, new_worksheet, new_notebook
) )


from ..nbpy import reads, writes from ..nbpy import reads, writes
Expand Down

0 comments on commit a85c3e7

Please sign in to comment.