From 307b083bb3101fd226dced2c366b8c4ab9a78d40 2011-09-07 11:24:11 From: Grahame Bowland Date: 2011-09-07 11:24:11 Subject: [PATCH] Make PNG images in the Qt console work in Python 3. --- diff --git a/IPython/frontend/qt/console/rich_ipython_widget.py b/IPython/frontend/qt/console/rich_ipython_widget.py index 771c004..d137618 100644 --- a/IPython/frontend/qt/console/rich_ipython_widget.py +++ b/IPython/frontend/qt/console/rich_ipython_widget.py @@ -83,7 +83,7 @@ class RichIPythonWidget(IPythonWidget): self._append_html(self._make_out_prompt(prompt_number), True) # This helps the output to look nice. self._append_plain_text('\n', True) - self._append_png(decodestring(data['image/png']), True) + self._append_png(decodestring(data['image/png'].encode('ascii')), True) self._append_html(self.output_sep2, True) else: # Default back to the plain text representation. @@ -104,7 +104,7 @@ class RichIPythonWidget(IPythonWidget): elif data.has_key('image/png'): # PNG data is base64 encoded as it passes over the network # in a JSON structure so we decode it. - png = decodestring(data['image/png']) + png = decodestring(data['image/png'].encode('ascii')) self._append_png(png, True) else: # Default back to the plain text representation. diff --git a/IPython/lib/pylabtools.py b/IPython/lib/pylabtools.py index 48f93f3..9db8b93 100644 --- a/IPython/lib/pylabtools.py +++ b/IPython/lib/pylabtools.py @@ -19,7 +19,7 @@ Authors # Imports #----------------------------------------------------------------------------- -from cStringIO import StringIO +from io import BytesIO from IPython.utils.decorators import flag_calls @@ -99,11 +99,11 @@ def print_figure(fig, fmt='png'): fig.set_facecolor('white') fig.set_edgecolor('white') try: - string_io = StringIO() + bytes_io = BytesIO() # use 72 dpi to match QTConsole's dpi - fig.canvas.print_figure(string_io, format=fmt, dpi=72, + fig.canvas.print_figure(bytes_io, format=fmt, dpi=72, bbox_inches='tight') - data = string_io.getvalue() + data = bytes_io.getvalue() finally: fig.set_facecolor(fc) fig.set_edgecolor(ec) diff --git a/IPython/zmq/displayhook.py b/IPython/zmq/displayhook.py index cc70604..2cf5ff5 100644 --- a/IPython/zmq/displayhook.py +++ b/IPython/zmq/displayhook.py @@ -30,10 +30,10 @@ class ZMQDisplayHook(object): def _encode_binary(format_dict): pngdata = format_dict.get('image/png') if pngdata is not None: - format_dict['image/png'] = encodestring(pngdata) + format_dict['image/png'] = encodestring(pngdata).decode('ascii') jpegdata = format_dict.get('image/jpeg') if jpegdata is not None: - format_dict['image/jpeg'] = encodestring(jpegdata) + format_dict['image/jpeg'] = encodestring(jpegdata).decode('ascii') class ZMQShellDisplayHook(DisplayHook):