import requests response = requests.get('http://jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb') response.content[0:60]+'...' from IPython.nbformat import current as nbformat jake_notebook = nbformat.reads_json(response.content) jake_notebook.worksheets[0].cells[0] import IPython.nbconvert from IPython.config import Config from IPython.nbconvert import HTMLExporter ## I use `basic` here to have less boilerplate and headers in the HTML. ## we'll see later how to pass config to exporters. exportHtml = HTMLExporter(config=Config({'HTMLExporter':{'default_template':'basic'}})) (body, resources) = exportHtml.from_notebook_node(jake_notebook) print resources.keys() print resources['metadata'] print resources['output_extension'] # print resources['inlining'] # too lng to be shown # Part of the body, here the first Heading start = body.index('

from IPython.config import Config c = Config({ 'ExtractOutputTransformer':{'enabled':True} }) exportHtml = HTMLExporter() exportHtml_and_figs = HTMLExporter(config=c) (_, resources) = exportHtml.from_notebook_node(jake_notebook) (_, resources_with_fig) = exportHtml_and_figs.from_notebook_node(jake_notebook) print 'resources without the "figures" key :' print resources.keys() print '' print 'Here we have one more field ' print resources_with_fig.keys() resources_with_fig['outputs'].keys() from IPython.nbconvert.transformers import Transformer import IPython.config print "Four relevant docstring" print '=============================' print Transformer.__doc__ print '=============================' print Transformer.call.__doc__ print '=============================' print Transformer.transform_cell.__doc__ print '=============================' from traitlets import Integer class PelicanSubCell(Transformer): """A Pelican specific transformer to remove somme of the cells of a notebook""" # I could also read the cells from nbc.metadata.pelican is someone wrote a JS extension # But I'll stay with configurable value. start = Integer(0, config=True, help="first cell of notebook to be converted") end = Integer(-1, config=True, help="last cell of notebook to be converted") def call(self, nb, resources): #nbc = deepcopy(nb) nbc = nb # don't print in real transformer !!! print "I'll keep only cells from ", self.start, "to ", self.end, "\n\n" for worksheet in nbc.worksheets : cells = worksheet.cells[:] worksheet.cells = cells[self.start:self.end] return nbc, resources # I create this on the fly, but this could be loaded from a DB, and config object support merging... c = Config({ 'PelicanSubCell':{ 'enabled':True, 'start':4, 'end':6, } }) pelican = RSTExporter(transformers=[PelicanSubCell], config=c) print pelican.from_notebook_node(jake_notebook)[0] from IPython.nbconvert.filters.highlight import _pygment_highlight from pygments.formatters import HtmlFormatter from IPython.nbconvert.exporters import HTMLExporter from IPython.config import Config from IPython.nbformat import current as nbformat def my_highlight(source, language='ipython'): formatter = HtmlFormatter(cssclass='highlight-ipynb') return _pygment_highlight(source, formatter, language) c = Config({'CSSHtmlHeaderTransformer': {'enabled':False, 'highlight_class':'highlight-ipynb'}}) exportHtml = HTMLExporter( config=c , filters={'highlight2html': my_highlight} ) (body,resources) = exportHtml.from_notebook_node(jake_notebook) i = body.index('highlight-ipynb') body[i-12:i+50] from jinja2 import DictLoader dl = DictLoader({'html_full.tpl': """ {%- extends 'html_basic.tpl' -%} {% block footer %} FOOOOOOOOTEEEEER {% endblock footer %} """}) exportHtml = HTMLExporter( config=None , filters={'highlight': my_highlight}, extra_loaders=[dl] ) (body,resources) = exportHtml.from_notebook_node(jake_notebook) for l in body.split('\n')[-4:]: print l