##// END OF EJS Templates
Fix minor formatting issues
Fix minor formatting issues

File last commit:

r7566:5d4da6eb
r7660:ee23a75b
Show More
nbpy.py
204 lines | 7.4 KiB | text/x-python | PythonLexer
Brian E. Granger
More review changes....
r4609 """Read and write notebooks as regular .py files.
Authors:
* Brian Granger
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Brian E. Granger
Adding nbformat subpackage.
r4392
Thomas Kluyver
Strip out encoding declaration when loading notebook.
r5739 import re
Brian E. Granger
Initial draft of more formal notebook format....
r4401 from .rwbase import NotebookReader, NotebookWriter
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 from .nbbase import (
new_code_cell, new_text_cell, new_worksheet,
MinRK
add minor-version support to .py export
r7566 new_notebook, new_heading_cell, nbformat, nbformat_minor,
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 )
Brian E. Granger
Adding nbformat subpackage.
r4392
Brian E. Granger
More review changes....
r4609 #-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
Brian E. Granger
Adding nbformat subpackage.
r4392
Thomas Kluyver
Use emacs-recognised encoding declaration.
r5745 _encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)")
Thomas Kluyver
Strip out encoding declaration when loading notebook.
r5739
Brian E. Granger
Improvements to file uploaded, mime types and .py reader....
r4493 class PyReaderError(Exception):
pass
Brian E. Granger
Adding nbformat subpackage.
r4392 class PyReader(NotebookReader):
Brian E. Granger
Initial draft of more formal notebook format....
r4401 def reads(self, s, **kwargs):
Brian E. Granger
Full versioning added to nbformat.
r4406 return self.to_notebook(s,**kwargs)
def to_notebook(self, s, **kwargs):
Brian E. Granger
Adding nbformat subpackage.
r4392 lines = s.splitlines()
cells = []
cell_lines = []
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 kwargs = {}
Brian E. Granger
New .py notebook format implemented.
r4536 state = u'codecell'
Brian E. Granger
Adding nbformat subpackage.
r4392 for line in lines:
Thomas Kluyver
Strip out encoding declaration when loading notebook.
r5739 if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line):
Brian E. Granger
Created new notebook magic that can export/convert notebooks....
r4520 pass
elif line.startswith(u'# <codecell>'):
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 cell = self.new_cell(state, cell_lines, **kwargs)
Brian E. Granger
New .py notebook format implemented.
r4536 if cell is not None:
cells.append(cell)
state = u'codecell'
cell_lines = []
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 kwargs = {}
Bernardo B. Marques
remove all trailling spaces
r4872 elif line.startswith(u'# <htmlcell>'):
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 cell = self.new_cell(state, cell_lines, **kwargs)
Brian E. Granger
New .py notebook format implemented.
r4536 if cell is not None:
cells.append(cell)
state = u'htmlcell'
cell_lines = []
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 kwargs = {}
Brian E. Granger
New .py notebook format implemented.
r4536 elif line.startswith(u'# <markdowncell>'):
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 cell = self.new_cell(state, cell_lines, **kwargs)
Brian E. Granger
New .py notebook format implemented.
r4536 if cell is not None:
cells.append(cell)
state = u'markdowncell'
Brian E. Granger
Full versioning added to nbformat.
r4406 cell_lines = []
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 kwargs = {}
MinRK
add VERSIONHACK markers for never-released plaintext handling
r6477 # VERSIONHACK: plaintext -> raw
MinRK
interpret 'plaintext' cells with their new name 'raw'
r6255 elif line.startswith(u'# <rawcell>') or line.startswith(u'# <plaintextcell>'):
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 cell = self.new_cell(state, cell_lines, **kwargs)
if cell is not None:
cells.append(cell)
MinRK
rename plaintext cell -> raw cell
r6248 state = u'rawcell'
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 cell_lines = []
kwargs = {}
elif line.startswith(u'# <headingcell'):
cell = self.new_cell(state, cell_lines, **kwargs)
if cell is not None:
cells.append(cell)
cell_lines = []
m = re.match(r'# <headingcell level=(?P<level>\d)>',line)
if m is not None:
state = u'headingcell'
kwargs = {}
kwargs['level'] = int(m.group('level'))
else:
state = u'codecell'
kwargs = {}
cell_lines = []
Brian E. Granger
Adding nbformat subpackage.
r4392 else:
cell_lines.append(line)
Brian E. Granger
New .py notebook format implemented.
r4536 if cell_lines and state == u'codecell':
cell = self.new_cell(state, cell_lines)
if cell is not None:
cells.append(cell)
Brian E. Granger
Adding nbformat subpackage.
r4392 ws = new_worksheet(cells=cells)
nb = new_notebook(worksheets=[ws])
return nb
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 def new_cell(self, state, lines, **kwargs):
Brian E. Granger
New .py notebook format implemented.
r4536 if state == u'codecell':
input = u'\n'.join(lines)
input = input.strip(u'\n')
if input:
return new_code_cell(input=input)
elif state == u'htmlcell':
text = self._remove_comments(lines)
if text:
return new_text_cell(u'html',source=text)
elif state == u'markdowncell':
text = self._remove_comments(lines)
if text:
return new_text_cell(u'markdown',source=text)
MinRK
rename plaintext cell -> raw cell
r6248 elif state == u'rawcell':
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 text = self._remove_comments(lines)
if text:
MinRK
rename plaintext cell -> raw cell
r6248 return new_text_cell(u'raw',source=text)
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 elif state == u'headingcell':
text = self._remove_comments(lines)
level = kwargs.get('level',1)
if text:
return new_heading_cell(source=text,level=level)
Brian E. Granger
New .py notebook format implemented.
r4536
def _remove_comments(self, lines):
new_lines = []
for line in lines:
if line.startswith(u'#'):
new_lines.append(line[2:])
else:
new_lines.append(line)
text = u'\n'.join(new_lines)
text = text.strip(u'\n')
return text
Brian E. Granger
Improvements to file uploaded, mime types and .py reader....
r4493 def split_lines_into_blocks(self, lines):
Brian E. Granger
Created new notebook magic that can export/convert notebooks....
r4520 if len(lines) == 1:
yield lines[0]
raise StopIteration()
Brian E. Granger
Improvements to file uploaded, mime types and .py reader....
r4493 import ast
source = '\n'.join(lines)
code = ast.parse(source)
starts = [x.lineno-1 for x in code.body]
for i in range(len(starts)-1):
yield '\n'.join(lines[starts[i]:starts[i+1]]).strip('\n')
yield '\n'.join(lines[starts[-1]:]).strip('\n')
Brian E. Granger
Adding nbformat subpackage.
r4392
class PyWriter(NotebookWriter):
Brian E. Granger
Initial draft of more formal notebook format....
r4401 def writes(self, nb, **kwargs):
Thomas Kluyver
Use emacs-recognised encoding declaration.
r5745 lines = [u'# -*- coding: utf-8 -*-']
MinRK
add minor-version support to .py export
r7566 lines.extend([
u'# <nbformat>%i.%i</nbformat>' % (nbformat, nbformat_minor),
u'',
])
Brian E. Granger
Initial draft of more formal notebook format....
r4401 for ws in nb.worksheets:
for cell in ws.cells:
Brian E. Granger
New .py notebook format implemented.
r4536 if cell.cell_type == u'code':
input = cell.get(u'input')
Brian E. Granger
Massive work on the notebook document format....
r4484 if input is not None:
lines.extend([u'# <codecell>',u''])
lines.extend(input.splitlines())
Brian E. Granger
New .py notebook format implemented.
r4536 lines.append(u'')
elif cell.cell_type == u'html':
input = cell.get(u'source')
if input is not None:
lines.extend([u'# <htmlcell>',u''])
lines.extend([u'# ' + line for line in input.splitlines()])
lines.append(u'')
elif cell.cell_type == u'markdown':
input = cell.get(u'source')
if input is not None:
lines.extend([u'# <markdowncell>',u''])
lines.extend([u'# ' + line for line in input.splitlines()])
lines.append(u'')
MinRK
rename plaintext cell -> raw cell
r6248 elif cell.cell_type == u'raw':
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 input = cell.get(u'source')
if input is not None:
MinRK
rename plaintext cell -> raw cell
r6248 lines.extend([u'# <rawcell>',u''])
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 lines.extend([u'# ' + line for line in input.splitlines()])
lines.append(u'')
elif cell.cell_type == u'heading':
input = cell.get(u'source')
level = cell.get(u'level',1)
if input is not None:
lines.extend([u'# <headingcell level=%s>' % level,u''])
lines.extend([u'# ' + line for line in input.splitlines()])
lines.append(u'')
Brian E. Granger
Full versioning added to nbformat.
r4406 lines.append('')
Brian E. Granger
Initial draft of more formal notebook format....
r4401 return unicode('\n'.join(lines))
Brian E. Granger
Adding nbformat subpackage.
r4392
_reader = PyReader()
_writer = PyWriter()
reads = _reader.reads
read = _reader.read
Brian E. Granger
Full versioning added to nbformat.
r4406 to_notebook = _reader.to_notebook
Brian E. Granger
Adding nbformat subpackage.
r4392 write = _writer.write
writes = _writer.writes
Brian E. Granger
Full versioning added to nbformat.
r4406