##// END OF EJS Templates
regen references templates
regen references templates

File last commit:

r9572:1f168443
r9694:63ef506c
Show More
notebook.py
107 lines | 2.9 KiB | text/x-python | PythonLexer
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
r8789 """Base class for doing notebook-to-notebook transformations.
This implements a converter class that turns an IPython notebook into another
IPython notebook, mainly so that it can be subclassed to perform more useful
and sophisticated transformations.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2011, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Stdlib imports
import json
David Warde-Farley
Make tests more filesystem-friendly
r8719 import os
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
r8789 from shutil import rmtree
# Our own imports
Anthony Scopatz
convters sub-package use relative imports
r8933 from .base import Converter
from .utils import cell_to_lines
Matthias BUSSONNIER
latex working
r8618
David Warde-Farley
PEP8-ify rest of the repository.
r8749
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
r8789 #-----------------------------------------------------------------------------
David Warde-Farley
More descriptive comment headers.
r8808 # Class declarations
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
r8789 #-----------------------------------------------------------------------------
Matthias BUSSONNIER
latex working
r8618 class ConverterNotebook(Converter):
"""
A converter that is essentially a null-op.
This exists so it can be subclassed
David Warde-Farley
PEP8
r8718 for custom handlers of .ipynb files
Matthias BUSSONNIER
latex working
r8618 that create new .ipynb files.
What distinguishes this from JSONWriter is that
subclasses can specify what to do with each type of cell.
Writes out a notebook file.
"""
extension = 'ipynb'
Matthias BUSSONNIER
fix tests
r9572 def __init__(self, infile=None, outbase=None, **kw):
Converter.__init__(self, infile=infile, **kw)
Matthias BUSSONNIER
latex working
r8618 self.outbase = outbase
rmtree(self.files_dir)
def convert(self):
David Warde-Farley
PEP8-ify rest of the repository.
r8749 return unicode(json.dumps(json.loads(Converter.convert(self, ',')),
indent=1, sort_keys=True))
Matthias BUSSONNIER
latex working
r8618
def optional_header(self):
s = \
"""{
"metadata": {
"name": "%(name)s"
},
"nbformat": 3,
"worksheets": [
{
David Warde-Farley
Make tests more filesystem-friendly
r8719 "cells": [""" % {'name': os.path.basename(self.outbase)}
Matthias BUSSONNIER
latex working
r8618 return s.split('\n')
def optional_footer(self):
s = \
"""]
}
]
}"""
return s.split('\n')
def render_heading(self, cell):
return cell_to_lines(cell)
def render_code(self, cell):
return cell_to_lines(cell)
def render_markdown(self, cell):
return cell_to_lines(cell)
def render_raw(self, cell):
return cell_to_lines(cell)
def render_pyout(self, output):
return cell_to_lines(output)
def render_pyerr(self, output):
return cell_to_lines(output)
def render_display_format_text(self, output):
return [output.text]
def render_display_format_html(self, output):
return [output.html]
def render_display_format_latex(self, output):
return [output.latex]
def render_display_format_json(self, output):
return [output.json]
def render_display_format_javascript(self, output):
return [output.javascript]