[cairo] Cairo And IPython

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Wed Nov 18 16:04:46 PST 2015

Just a note, for those wanting to have a simple testbed for trying out
Cairo snippets (or any other kind of snippets): IPython
<http://ipython.org/> can be very useful for this kind of thing. It
offers a notebook-style interface, where you can insert any number of
lines of code into a single cell, and execute that cell with
shift-enter. The notebook preserves its context as long as the kernel
instance is running, so you can incrementally execute additional code
that uses definitions set up by previous code.

With Python, you can display graphics, using the ImageWidget class:

    from IPython.html.widgets import ImageWidget

This takes a PNG stream, and inserts the resulting graphic into the
notebook display. For example, reading from a PNG file:

    ImageWidget(value = open("/path/to/example.png", "rb").read())

Or, having rendered an image to a Qahirah ImageSurface called, say
“pix”, you can display it as easily as

    ImageWidget(value = pix.to_png_bytes())

More information about the cairo mailing list