[IPython-dev] Printing support enabled

Mark Voorhies mark.voorhies at ucsf.edu
Sun Oct 10 23:30:50 EDT 2010

On Saturday, October 09, 2010 07:05:19 pm Mark Voorhies wrote:
> On Saturday, October 09, 2010 06:39:20 pm Evan Patterson wrote:
> > Fernando has the right idea. Look at the _process_execute_payload, _add_image, and _get_image methods of RichIPythonWidget to see how QImages and SVG data are indexed. Since the QTextDocument does not have a mechanism for retrieving all of it's resources, the image IDs will have to be stored manually in '_add_image'.
> > 
> > If you prefer, I can implement this some time in the next week or so, but feel free to take a stab at it.
> > 
> > Evan
> Yes, looks like format.name() inside of _process_execute_payload gives the ID that I want to map from.
> I think I can do up a decent first pass this weekend.

Okay, I've got my first pass for HTML export up on github at

I tried three approaches (available as three context menu options):

1) Export HTML (external PNGs):
   This mimics Firefox's "Save as Web Page, complete" behavior.
   Saving "mypath/test.htm" gives an HTML file with links to PNGs in
   "mypath/test_files/".  The PNGs are named relative to format.name()
   to avoid collisions.

   Works in Firefox 3.6.10, Konqueror 4.4.2/KHTML, and Konqueror 4.4.2/WebKit

2) Export HTML (inline PNGs):
   Saves a single HTML file with images as inline base64 PNGs
   (c.f. http://en.wikipedia.org/wiki/Data_URI_scheme#HTML)

   Works in Firefox 3.6.10, Konqueror 4.4.2/KHTML, and Konqueror 4.4.2/WebKit

3) Export XHTML (inline SVGs):
   Saves a single XHTML file with images as inline SVG.  The "XML" is generated
   by overwriting the Qt-generated document header, so it is not guaranteed to
   be valid XML (but Firefox does validate my test case).

   Works in Firefox 3.6.10 and Konqueror 4.4.2/WebKit.
   Image placement is incorrect for Konqueror 4.4.2/KHTML.

(all tests run on a Dell Latitude D630 w/ Kubuntu Lucid:
mvoorhie at virgil:~$ uname -a
Linux virgil 2.6.32-24-generic #43-Ubuntu SMP Thu Sep 16 14:17:33 UTC 2010 i686 GNU/Linux)

It may be possible to link external SVG images via an <embed> or <object> tag,
but I couldn't find a clean/portable way to do this.

Current issues:
* I'm doing lots of string coercion in order to use re.sub on Qt's HTML.  I mostly
  get away with it, but we wind up with a few bad character encodings in the
  output (e.g., the tabs for multi-line inputs).  Would be good for someone who
  knows more about unicode to take a look at this...

* The file name generation for "Export HTML (external PNGs)" is a bit hacky.  Should
  probably be rewritten to use os.path.

* Haven't tested with anything other than the Qt front end.  In theory, other front
  ends will export HTML with the images stripped, unless they implement their own
  version of imagetag().

Feel free to take/hack what you like and ditch the rest.

Happy hacking,


More information about the IPython-dev mailing list