##// END OF EJS Templates
Refactoring of text/markdown/rst/html cells.
Refactoring of text/markdown/rst/html cells.

File last commit:

r4498:b0e516c2
r4508:fbce9421
Show More
nbbase.py
104 lines | 2.8 KiB | text/x-python | PythonLexer
Brian E. Granger
Adding nbformat subpackage.
r4392 """The basic dict based notebook format."""
Brian E. Granger
Initial draft of more formal notebook format....
r4401 import pprint
Brian E. Granger
Adding nbformat subpackage.
r4392 import uuid
Brian E. Granger
Initial draft of more formal notebook format....
r4401 from IPython.utils.ipstruct import Struct
class NotebookNode(Struct):
pass
Brian E. Granger
Adding nbformat subpackage.
r4392
Brian E. Granger
Full versioning added to nbformat.
r4406 def from_dict(d):
if isinstance(d, dict):
newd = NotebookNode()
for k,v in d.items():
newd[k] = from_dict(v)
return newd
elif isinstance(d, (tuple, list)):
return [from_dict(i) for i in d]
else:
return d
Brian E. Granger
Updates to basic notebook format....
r4402 def new_output(output_type=None, output_text=None, output_png=None,
Brian E. Granger
Adding nbformat subpackage.
r4392 output_html=None, output_svg=None, output_latex=None, output_json=None,
Brian E. Granger
Added saving and loading of output of all types.
r4497 output_javascript=None, prompt_number=None):
Brian E. Granger
Adding nbformat subpackage.
r4392 """Create a new code cell with input and output"""
Brian E. Granger
Initial draft of more formal notebook format....
r4401 output = NotebookNode()
Brian E. Granger
Updates to basic notebook format....
r4402 if output_type is not None:
output.output_type = unicode(output_type)
Brian E. Granger
Adding nbformat subpackage.
r4392 if output_text is not None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 output.text = unicode(output_text)
Brian E. Granger
Adding nbformat subpackage.
r4392 if output_png is not None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 output.png = bytes(output_png)
Brian E. Granger
Adding nbformat subpackage.
r4392 if output_html is not None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 output.html = unicode(output_html)
Brian E. Granger
Adding nbformat subpackage.
r4392 if output_svg is not None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 output.svg = unicode(output_svg)
Brian E. Granger
Adding nbformat subpackage.
r4392 if output_latex is not None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 output.latex = unicode(output_latex)
Brian E. Granger
Adding nbformat subpackage.
r4392 if output_json is not None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 output.json = unicode(output_json)
Brian E. Granger
Adding nbformat subpackage.
r4392 if output_javascript is not None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 output.javascript = unicode(output_javascript)
Brian E. Granger
Added saving and loading of output of all types.
r4497 if prompt_number is not None:
output.prompt_number = int(prompt_number)
Brian E. Granger
Updates to basic notebook format....
r4402 return output
Brian E. Granger
Adding nbformat subpackage.
r4392
Brian E. Granger
Updates to basic notebook format....
r4402 def new_code_cell(input=None, prompt_number=None, outputs=None, language=u'python'):
"""Create a new code cell with input and output"""
cell = NotebookNode()
cell.cell_type = u'code'
if language is not None:
cell.language = unicode(language)
if input is not None:
cell.input = unicode(input)
if prompt_number is not None:
cell.prompt_number = int(prompt_number)
if outputs is None:
cell.outputs = []
else:
cell.outputs = outputs
return cell
Brian E. Granger
Starting to rename text cell to html cell.
r4498 def new_html_cell(source=None):
Brian E. Granger
Adding nbformat subpackage.
r4392 """Create a new text cell."""
Brian E. Granger
Initial draft of more formal notebook format....
r4401 cell = NotebookNode()
Brian E. Granger
Starting to rename text cell to html cell.
r4498 if source is not None:
cell.source = unicode(source)
cell.cell_type = u'html'
Brian E. Granger
Adding nbformat subpackage.
r4392 return cell
def new_worksheet(name=None, cells=None):
"""Create a worksheet by name with with a list of cells."""
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ws = NotebookNode()
Brian E. Granger
Adding nbformat subpackage.
r4392 if name is not None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ws.name = unicode(name)
Brian E. Granger
Adding nbformat subpackage.
r4392 if cells is None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ws.cells = []
Brian E. Granger
Adding nbformat subpackage.
r4392 else:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ws.cells = list(cells)
Brian E. Granger
Adding nbformat subpackage.
r4392 return ws
def new_notebook(name=None, id=None, worksheets=None):
"""Create a notebook by name, id and a list of worksheets."""
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb = NotebookNode()
Brian E. Granger
Full versioning added to nbformat.
r4406 nb.nbformat = 2
Brian E. Granger
Adding nbformat subpackage.
r4392 if name is not None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb.name = unicode(name)
Brian E. Granger
Adding nbformat subpackage.
r4392 if id is None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb.id = unicode(uuid.uuid4())
Brian E. Granger
Adding nbformat subpackage.
r4392 else:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb.id = unicode(id)
Brian E. Granger
Adding nbformat subpackage.
r4392 if worksheets is None:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb.worksheets = []
Brian E. Granger
Adding nbformat subpackage.
r4392 else:
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb.worksheets = list(worksheets)
Brian E. Granger
Adding nbformat subpackage.
r4392 return nb