ipython/ipython — pull request #4077 by andreabedini (2013-08-21 01:50:34+00:00)
Title: Interactive matplotlib backend for notebook

Hi,

this is matplotlib web backend tailored to work with ipython's notebook. The code is mostly taken from matplotlib's backend_webagg but it's been reworked, simplified and adapted to the notebook workflow.

#### How it works

Essentially it works this way:

- at instantiation it creates a httpserver hooked up to the same event loop the kernel uses
- this serves a page which displays the rendered canvas communicating with the backend through a websocket. this logic is identical to backend_webagg
- the figure manager has been slightly adapted to work like backend_inline. It keeps a list of figures the user has been working on and on show() it displays them.
- The embedding of a figure in the notebook is done with an iframe to the page served by the backend.
- The iframe automatically resizes itself sending a message to the parent frame.
- I tested it with few matplotlib examples for the gallery: Zoom/Pan/3D rotation/Animation/Interactivity/Widgets all work fine.

to load the interactive backend just do
```
import matplotlib
matplotlib.use('module://IPython.kernel.zmq.pylab.backend_interactive')
import matplotlib.pyplot as plt
```

Then you can `%load` something from the gallery at http://matplotlib.org/examples/index.html

At this time the code works well, at least for local use.

#### Limits:

- The configuration is not integrated with IPython's because I still understand very little about how it works.
- A bunch of machinery IPython already implements to deal with matplotlib is ignored but it can be integrated back if useful.
- At the moment the backend binds to a random port on the local interface, because I couldn't think of anything else. The backend runs on the kernel, but, ideally, it should be able to serve pages on the same interface the notebook does (although on a different port).
- It is not entirely clear to me which kind of interactivity is most appropriate. At this time, the user can display the same figure more than once (and this works just fine), but changes will be propagated to all visualizations which might not be what the user expects. Perhaps it would be better to have a way of duplicating a figure.
- Of course when the notebook is saved, the iframe is saved too but the backend state is not. Maybe we can save a screenshot. I don't know.

Let me know what you think of this.