ipython/ipython — pull request #4448 by dbarbeau (2013-10-28 13:07:53+00:00)
Title: JPG compression and lightweight html nbconvert template for blogs

These commits make publishing on blog sites (eg: blogger) easier.

# Rationale
Using "ipython nbconvert" produces self contained html (with data URIs for images) which is very handy for blog posts : one just copies the body a pastes it in the new blog post. However, hosts limit the size per post so, for this purpose, a more compact html representations of the notebook is desired.

# Strategy
Two places where bytes can be easily saved have currently been identified:

- Data URIs for matplotlib figures are currently PNG by default, or SVG optionnaly. Both can get bigger than data-uri encoded JPG quickly.
- Code cells are pre-highlighted, which introduces many tags for even small code blocks.

The solution works in the scenario where the user starts ipython notebook, creates and saves notebooks, then uses nbconvert to convert the notebook to html. Only the body is useful.

## JPG compression for images
By starting the notebook with "--InlineBackend.figure_format=jpg" the figures are transported in the JPG format. The user also has access to "--InlineBackend.quality=XX" to set the level of compression. Of course, JPG is lossy but many figures can do with some compression. This is only enabled if PIL is installed.

This can also be useful in the general use case, to save some bandwidth.

## In browser code highlighting
Syntax highlighting in html introduces many tags which eat many precious bytes. The strategy retained here is to convert code blocks to "html escaped ascii" code blocks (raw code) and assign them css classes that enable google's prettyfier to run on the code, in the browser. This is done by using a new template for nbconvert: "lightweight_blog"

# Doubts
The user must still edit his blog's template settings to add the required JS and CSS to make the blog post look as much as possible like the notebook. The generated html includes instructions on what should go where but those bits shouldn't be included in the blog post to save bytes. As they are common to many blog posts, it is better to put those resources into the template than into each post. Are there ways to remove the need for user intervention? This is unlikely with the constraints given. Are there better ways to tell the user to edit his blog's template (an nbconvert post-processor that prints an info message)?