Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 105 lines (87 sloc) 2.874 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
"""The basic dict based notebook format."""

import pprint
import uuid

from IPython.utils.ipstruct import Struct


class NotebookNode(Struct):
    pass


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


def new_output(output_type=None, output_text=None, output_png=None,
    output_html=None, output_svg=None, output_latex=None, output_json=None,
    output_javascript=None, prompt_number=None):
    """Create a new code cell with input and output"""
    output = NotebookNode()
    if output_type is not None:
        output.output_type = unicode(output_type)
    if output_text is not None:
        output.text = unicode(output_text)
    if output_png is not None:
        output.png = bytes(output_png)
    if output_html is not None:
        output.html = unicode(output_html)
    if output_svg is not None:
        output.svg = unicode(output_svg)
    if output_latex is not None:
        output.latex = unicode(output_latex)
    if output_json is not None:
        output.json = unicode(output_json)
    if output_javascript is not None:
        output.javascript = unicode(output_javascript)
    if prompt_number is not None:
        output.prompt_number = int(prompt_number)
    return output


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

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


def new_worksheet(name=None, cells=None):
    """Create a worksheet by name with with a list of cells."""
    ws = NotebookNode()
    if name is not None:
        ws.name = unicode(name)
    if cells is None:
        ws.cells = []
    else:
        ws.cells = list(cells)
    return ws


def new_notebook(name=None, id=None, worksheets=None):
    """Create a notebook by name, id and a list of worksheets."""
    nb = NotebookNode()
    nb.nbformat = 2
    if name is not None:
        nb.name = unicode(name)
    if id is None:
        nb.id = unicode(uuid.uuid4())
    else:
        nb.id = unicode(id)
    if worksheets is None:
        nb.worksheets = []
    else:
        nb.worksheets = list(worksheets)
    return nb

Something went wrong with that request. Please try again.