##// END OF EJS Templates
Merge pull request #8778 from SylvainCorlay/Meta...
Merge pull request #8778 from SylvainCorlay/Meta Use isinstance to check for types

File last commit:

r21253:ff3b995a
r21631:3ac9be53 merge
Show More
Using nbconvert as a Library.ipynb
619 lines | 19.6 KiB | text/plain | TextLexer
/ examples / Notebook / Using nbconvert as a Library.ipynb
Brian E. Granger
Moving things around.
r17497 {
Min RK
upate exmaple notebooks to nbformat v4
r18669 "cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "# Using nbconvert as a Library"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "In this Notebook, you will be introduced to the programatic API of nbconvert and how it can be used in various contexts. \n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "One of [@jakevdp](https://github.com/jakevdp)'s great [blog posts](http://jakevdp.github.io/blog/2013/04/15/code-golf-in-python-sudoku/) will be used to demonstrate. This notebook will not focus on using the command line tool. The attentive reader will point-out that no data is read from or written to disk during the conversion process. Nbconvert has been designed to work in memory so that it works well in a database or web-based environement too."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "## Quick overview"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Credit, Jonathan Frederic (@jdfreder on github)\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
"<center>\n",
Min RK
update nbconvert-as-library notebook...
r19938 " ![nbca](images/nbconvert_arch.png)\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "</center>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "The main principle of nbconvert is to instantiate an `Exporter` that controls\n",
"the pipeline through which notebooks are converted."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "First, download @jakevdp's notebook."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
"import requests\n",
"response = requests.get('http://jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb')\n",
Min RK
update nbconvert-as-library notebook...
r19938 "response.text[0:60]+'...'"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "If you do not have `requests`, install it by running `pip install requests` (or if you don't have pip installed, you can find it on PYPI)."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "The response is a JSON string which represents an IPython notebook. Next, read the response using nbformat.\n",
"\n",
"Doing this will guarantee that the notebook structure is valid. Note that the in-memory format and on disk format are slightly different. In particual, on disk, multiline strings might be splitted into a list of strings."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Min RK
update nbconvert-as-library notebook...
r19938 "from IPython import nbformat\n",
"jake_notebook = nbformat.reads(response.text, as_version=4)\n",
"jake_notebook.cells[0]"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "The nbformat API returns a special dict. You don't need to worry about the details of the structure."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "The nbconvert API exposes some basic exporters for common formats and defaults. You will start\n",
"by using one of them. First you will import it, then instantiate it using most of the defaults, and finally you will process notebook downloaded early."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
Min RK
update dependency imports...
r21253 "from traitlets.config import Config\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "from IPython.nbconvert import HTMLExporter\n",
"\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "# The `basic` template is used here.\n",
"# Later you'll learn how to configure the exporter.\n",
Min RK
update nbconvert-as-library notebook...
r19938 "html_exporter = HTMLExporter(config=Config({'HTMLExporter':{'default_template':'basic'}}))"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
Min RK
update nbconvert-as-library notebook...
r19938 "(body, resources) = html_exporter.from_notebook_node(jake_notebook)"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "The exporter returns a tuple containing the body of the converted notebook, raw HTML in this case, as well as a resources dict. The resource dict contains (among many things) the extracted PNG, JPG [...etc] from the notebook when applicable. The basic HTML exporter leaves the figures as embeded base64, but you can configure it to extract the figures. So for now, the resource dict **should** be mostly empty, except for a key containing CSS and a few others whose content will be obvious.\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "`Exporter`s are stateless, so you won't be able to extract any usefull information beyond their configuration from them. You can re-use an exporter instance to convert another notebook. Each exporter exposes, for convenience, a `from_file` and `from_filename` method."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Min RK
update nbconvert-as-library notebook...
r19938 "print([key for key in resources ])\n",
"print(resources['metadata'])\n",
"print(resources['output_extension'])\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "# print resources['inlining'] # Too long to be shown"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
"# Part of the body, here the first Heading\n",
"start = body.index('<h1 id', )\n",
Min RK
update nbconvert-as-library notebook...
r19938 "print(body[:400]+'...')"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "If you understand HTML, you'll notice that some common tags are ommited, like the `body` tag. Those tags are included in the default `HtmlExporter`, which is what would have been constructed if no Config object was passed into it."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Extracting Figures"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "When exporting you may want to extract the base64 encoded figures as files, this is by default what the `RstExporter` does (as seen below)."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from IPython.nbconvert import RSTExporter\n",
"\n",
Min RK
update nbconvert-as-library notebook...
r19938 "rst_exporter = RSTExporter()\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Min RK
update nbconvert-as-library notebook...
r19938 "(body,resources) = rst_exporter.from_notebook_node(jake_notebook)"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Min RK
update nbconvert-as-library notebook...
r19938 "print(body[:970]+'...')\n",
"print('[.....]')\n",
"print(body[800:1200]+'...')"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Notice that base64 images are not embeded, but instead there are file name like strings. The strings actually are (configurable) keys that map to the binary data in the resources dict.\n"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Note, if you write an RST Plugin, you are responsible for writing all the files to the disk (or uploading, etc...) in the right location. Of course, the naming scheme is configurable."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "As an exercise, this notebook will show you how to get one of those images."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Min RK
update nbconvert-as-library notebook...
r19938 "list(resources['outputs'])"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "There are 5 extracted binary figures, all `png`s, but they could have been `svg`s which then wouldn't appear in the binary sub dict. Keep in mind that objects with multiple reprs will have every repr stored in the notebook avaliable for conversion. \n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Hence if the object provides `_repr_javascript_`, `_repr_latex_`, and `_repr_png_`, you will be able to determine, at conversion time, which representaition is most appropriate. You could even show all of the representaitions of an object in a single export, it's up to you. Doing so would require a little more involvement on your part and a custom Jinja template.\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Back to the task of extracting an image, the Image display object can be used to display one of the images (as seen below)."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
"from IPython.display import Image\n",
"Image(data=resources['outputs']['output_3_0.png'],format='png')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "This image is being rendered without reading or writing to the disk."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Extracting figures with HTML Exporter ?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use case:\n",
"\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "> I write an [awesome blog](http://jakevdp.github.io/) using IPython notebooks converted to HTML, and I want the images to be cached. Having one html file with all of the images base64 encoded inside it is nice when sharing with a coworker, but for a website, not so much. I need an HTML exporter, and I want it to extract the figures!"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Some theory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "The process of converting a notebook to a another format with happens in a few steps:\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 " - Retrieve the notebook and it's accompanying resource (you are responsible for this).\n",
" - Feed them into the exporter, which:\n",
" - Sequentially feeds them into an array of `Preprocessors`. Preprocessors only act on the **structure** of the notebook, and have unrestricted access to it. \n",
" - Feeds the notebook into the Jinja templating engine.\n",
" - The template is configured (you can change which one is used).\n",
" - Templates make use of configurable macros called `filters`.\n",
" - The exporter returns the converted notebook and other relevant resources as a tuple.\n",
" - You write the data to the disk, or elsewhere (you are responsible for this too)."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "You can use `Preprocessors` to accomplish the task at hand. IPython has preprocessors built in which you can use. One of them, the `ExtractOutputPreprocessor` is responsible for crawling the notebook, finding all of the figures, and putting them into the resources directory, as well as choosing the key (i.e. `filename_xx_y.extension`) that can replace the figure inside the template.\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "The `ExtractOutputPreprocessor` is special because it's available in all of the `Exporter`s, and is just disabled in some by default."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Min RK
update nbconvert-as-library notebook...
r19938 "# 3rd one should be <ExtractOutputPreprocessor>\n",
"html_exporter._preprocessors"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Use the IPython configuration/Traitlets system to enable it. If you have already set IPython configuration options, this system is familiar to you. Configuration options will always of the form:\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Min RK
update nbconvert-as-library notebook...
r19938 " ClassName.attribute_name = value\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 " \n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "You can create a configuration object a couple of different ways. Everytime you launch IPython, configuration objects are created from reading config files in your profile directory. Instead of writing a config file, you can also do it programatically using a dictionary. The following creates a config object, that enables the figure extracter, and passes it to an `HTMLExporter`. The output is compared to an `HTMLExporter` without the config object."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Min RK
update dependency imports...
r21253 "from traitlets.config import Config\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
"c = Config({\n",
Min RK
update nbconvert-as-library notebook...
r19938 " 'ExtractOutputPreprocessor':{'enabled':True}\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 " })\n",
"\n",
Min RK
update nbconvert-as-library notebook...
r19938 "exportHTML = HTMLExporter()\n",
"exportHTML_and_figs = HTMLExporter(config=c)\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Min RK
update nbconvert-as-library notebook...
r19938 "(_, resources) = exportHTML.from_notebook_node(jake_notebook)\n",
"(_, resources_with_fig) = exportHTML_and_figs.from_notebook_node(jake_notebook)\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "print('resources without the \"figures\" key:')\n",
Min RK
update nbconvert-as-library notebook...
r19938 "print(list(resources))\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Min RK
update nbconvert-as-library notebook...
r19938 "print('')\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "print('ditto, notice that there\\'s one more field:')\n",
Min RK
update nbconvert-as-library notebook...
r19938 "print(list(resources_with_fig))\n",
"list(resources_with_fig['outputs'])"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Min RK
update nbconvert-as-library notebook...
r19938 "#### Custom Preprocessor"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "There are an endless number of transformations that you may want to apply to a notebook. This is why we provide a way to register your own preprocessors that will be applied to the notebook after the default ones.\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "To do so, you'll have to pass an ordered list of `Preprocessor`s to the `Exporter`'s constructor. \n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "For simple cell-by-cell transformations, `Preprocessor` can be created using a decorator. For more complex operations, you need to subclass `Preprocessor` and define a `call` method (as seen below).\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "All transforers have a flag that allows you to enable and disable them via a configuration object."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Min RK
update nbconvert-as-library notebook...
r19938 "from IPython.nbconvert.preprocessors import Preprocessor\n",
Min RK
update dependency imports...
r21253 "import traitlets.config\n",
Min RK
update nbconvert-as-library notebook...
r19938 "print(\"Four relevant docstring\")\n",
"print('=============================')\n",
"print(Preprocessor.__doc__)\n",
"print('=============================')\n",
"print(Preprocessor.preprocess.__doc__)\n",
"print('=============================')\n",
"print(Preprocessor.preprocess_cell.__doc__)\n",
"print('=============================')"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "The following demonstration was requested in [an IPython GitHub issue](https://github.com/ipython/nbconvert/pull/137#issuecomment-18658235), the ability to exclude a cell by index. \n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Inject cells is similar, and won't be covered here. If you want to inject static content at the beginning/end of a notebook, use a custom template."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
Min RK
update dependency imports...
r21253 "from traitlets import Integer"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
Min RK
update nbconvert-as-library notebook...
r19938 "class PelicanSubCell(Preprocessor):\n",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 " \"\"\"A Pelican specific preprocessor to remove some of the cells of a notebook\"\"\"\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 " \n",
" # I could also read the cells from nbc.metadata.pelican is someone wrote a JS extension\n",
" # But I'll stay with configurable value. \n",
" start = Integer(0, config=True, help=\"first cell of notebook to be converted\")\n",
" end = Integer(-1, config=True, help=\"last cell of notebook to be converted\")\n",
" \n",
Min RK
update nbconvert-as-library notebook...
r19938 " def preprocess(self, nb, resources):\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
" #nbc = deepcopy(nb)\n",
" nbc = nb\n",
Min RK
update nbconvert-as-library notebook...
r19938 " # don't print in real preprocessor !!!\n",
" print(\"I'll keep only cells from \", self.start, \"to \", self.end, \"\\n\\n\")\n",
" nbc.cells = nb.cells[self.start:self.end] \n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 " return nbc, resources"
]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# I create this on the fly, but this could be loaded from a DB, and config object support merging...\n",
Min RK
update nbconvert-as-library notebook...
r19938 "c = Config()\n",
"c.PelicanSubCell.enabled = True\n",
"c.PelicanSubCell.start = 4\n",
"c.PelicanSubCell.end = 6"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "Here a Pelican exporter is created that takes `PelicanSubCell` preprocessors and a `config` object as parameters. This may seem redundant, but with the configuration system you can register an inactive preprocessor on all of the exporters and activate it from config files or the command line. "
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
Min RK
update nbconvert-as-library notebook...
r19938 "pelican = RSTExporter(preprocessors=[PelicanSubCell], config=c)"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
Min RK
update nbconvert-as-library notebook...
r19938 "print(pelican.from_notebook_node(jake_notebook)[0])"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Programatically make templates"
]
},
{
"cell_type": "code",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "execution_count": null,
Min RK
upate exmaple notebooks to nbformat v4
r18669 "metadata": {
"collapsed": false
},
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "outputs": [],
Min RK
upate exmaple notebooks to nbformat v4
r18669 "source": [
"from jinja2 import DictLoader\n",
"\n",
Min RK
update nbconvert-as-library notebook...
r19938 "dl = DictLoader({'full.tpl': \n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\"\"\"\n",
Min RK
update nbconvert-as-library notebook...
r19938 "{%- extends 'basic.tpl' -%} \n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "\n",
"{% block footer %}\n",
"FOOOOOOOOTEEEEER\n",
"{% endblock footer %}\n",
"\"\"\"})\n",
"\n",
"\n",
Min RK
update nbconvert-as-library notebook...
r19938 "exportHTML = HTMLExporter(extra_loaders=[dl])\n",
"(body,resources) = exportHTML.from_notebook_node(jake_notebook)\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "for l in body.split('\\n')[-4:]:\n",
Min RK
update nbconvert-as-library notebook...
r19938 " print(l)"
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Real World Use"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "@jakevdp uses Pelican and IPython Notebook to blog. Pelican [will use](https://github.com/getpelican/pelican-plugins/pull/21) nbconvert programatically to generate blog post. Have a look a [Pythonic Preambulations](http://jakevdp.github.io/) for Jake's blog post."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "@damianavila wrote the Nicholas Plugin to [write blog post as Notebooks](http://www.damian.oquanta.info/posts/one-line-deployment-of-your-site-to-gh-pages.html) and is developping a js-extension to publish notebooks via one click from the web app."
Min RK
upate exmaple notebooks to nbformat v4
r18669 ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
Min RK
update nbconvert-as-library notebook...
r19938 "<blockquote class=\"twitter-tweet\"><p>As <a href=\"https://twitter.com/Mbussonn\">@Mbussonn</a> requested... easieeeeer! Deploy your Nikola site with just a click in the IPython notebook! <a href=\"http://t.co/860sJunZvj\">http://t.co/860sJunZvj</a> cc <a href=\"https://twitter.com/ralsina\">@ralsina</a></p>&mdash; Damián Avila (@damian_avila) <a href=\"https://twitter.com/damian_avila/statuses/370306057828335616\">August 21, 2013</a></blockquote>\n",
Min RK
upate exmaple notebooks to nbformat v4
r18669 "</center>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### A few gotchas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jinja blocks use `{% %}`by default which does not play nicely with $\\LaTeX$, hence thoses are replaced by `((* *))` in latex templates."
]
Brian E. Granger
Moving things around.
r17497 }
Min RK
upate exmaple notebooks to nbformat v4
r18669 ],
Min RK
update nbconvert-as-library notebook...
r19938 "metadata": {
"kernelspec": {
"display_name": "Python 3",
Min RK
add kernel metadata to example notebooks
r20278 "language": "python",
Min RK
update nbconvert-as-library notebook...
r19938 "name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
Jonathan Frederic
A LOT OF CLEANUP IN examples/Notebook
r20536 "version": "3.4.3"
Min RK
update nbconvert-as-library notebook...
r19938 }
},
Min RK
upate exmaple notebooks to nbformat v4
r18669 "nbformat": 4,
"nbformat_minor": 0
Min RK
add kernel metadata to example notebooks
r20278 }