notebook.py
83 lines
| 1.9 KiB
| text/x-python
|
PythonLexer
/ converters / notebook.py
David Warde-Farley
|
r8719 | import os | ||
Matthias BUSSONNIER
|
r8618 | from converters.base import Converter | ||
Matthias BUSSONNIER
|
r8623 | from converters.utils import cell_to_lines | ||
Matthias BUSSONNIER
|
r8620 | from shutil import rmtree | ||
import json | ||||
Matthias BUSSONNIER
|
r8618 | |||
David Warde-Farley
|
r8749 | |||
Matthias BUSSONNIER
|
r8618 | class ConverterNotebook(Converter): | ||
""" | ||||
A converter that is essentially a null-op. | ||||
This exists so it can be subclassed | ||||
David Warde-Farley
|
r8718 | for custom handlers of .ipynb files | ||
Matthias BUSSONNIER
|
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' | ||||
def __init__(self, infile, outbase): | ||||
Converter.__init__(self, infile) | ||||
self.outbase = outbase | ||||
rmtree(self.files_dir) | ||||
def convert(self): | ||||
David Warde-Farley
|
r8749 | return unicode(json.dumps(json.loads(Converter.convert(self, ',')), | ||
indent=1, sort_keys=True)) | ||||
Matthias BUSSONNIER
|
r8618 | |||
def optional_header(self): | ||||
s = \ | ||||
"""{ | ||||
"metadata": { | ||||
"name": "%(name)s" | ||||
}, | ||||
"nbformat": 3, | ||||
"worksheets": [ | ||||
{ | ||||
David Warde-Farley
|
r8719 | "cells": [""" % {'name': os.path.basename(self.outbase)} | ||
Matthias BUSSONNIER
|
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] | ||||