From 57d3cb1a0bc42af3ded66fe5061635f5581af72b 2011-04-11 07:22:11 From: Fernando Perez Date: 2011-04-11 07:22:11 Subject: [PATCH] Do not generate output for empty figures in Qt console. Before, calling figure() would produce a big blank area. This ensures output is only returned if the figure has content. Also added basic testing for pylabtools, for which we had none. --- diff --git a/IPython/lib/pylabtools.py b/IPython/lib/pylabtools.py index 071b8b8..a7afc26 100644 --- a/IPython/lib/pylabtools.py +++ b/IPython/lib/pylabtools.py @@ -77,6 +77,11 @@ def figsize(sizex, sizey): def figure_to_svg(fig): """Convert a figure to svg for inline display.""" + # When there's an empty figure, we shouldn't return anything, otherwise we + # get big blank areas in the qt console. + if not fig.axes: + return + fc = fig.get_facecolor() ec = fig.get_edgecolor() fig.set_facecolor('white') @@ -240,7 +245,6 @@ def import_pylab(user_ns, backend, import_all=True, shell=None): # The old pastefig function has been replaced by display # Always add this svg formatter so display works. - from IPython.zmq.pylab.backend_inline import figure_to_svg from IPython.core.display import display, display_svg svg_formatter = shell.display_formatter.formatters['image/svg+xml'] svg_formatter.for_type_by_name( diff --git a/IPython/testing/iptest.py b/IPython/testing/iptest.py index e887815..bc96126 100644 --- a/IPython/testing/iptest.py +++ b/IPython/testing/iptest.py @@ -101,11 +101,12 @@ def test_for(mod, min_version=None): have = {} have['curses'] = test_for('_curses') +have['matplotlib'] = test_for('matplotlib') +have['pexpect'] = test_for('pexpect') +have['pymongo'] = test_for('pymongo') have['wx'] = test_for('wx') have['wx.aui'] = test_for('wx.aui') -have['pexpect'] = test_for('pexpect') have['zmq'] = test_for('zmq', '2.1.4') -have['pymongo'] = test_for('pymongo') #----------------------------------------------------------------------------- # Functions and classes @@ -191,6 +192,11 @@ def make_exclude(): if not have['pymongo']: exclusions.append(ipjoin('parallel', 'controller', 'mongodb')) + if not have['matplotlib']: + exclusions.extend([ipjoin('lib', 'pylabtools'), + ipjoin('lib', 'pylabtools', + 'tests', 'test_pylabtools')]) + # This is needed for the reg-exp to match on win32 in the ipdoctest plugin. if sys.platform == 'win32': exclusions = [s.replace('\\','\\\\') for s in exclusions] diff --git a/IPython/zmq/pylab/backend_inline.py b/IPython/zmq/pylab/backend_inline.py index 8a42357..3037e9d 100644 --- a/IPython/zmq/pylab/backend_inline.py +++ b/IPython/zmq/pylab/backend_inline.py @@ -6,7 +6,9 @@ from __future__ import print_function # Standard library imports +import sys +# Third-party imports import matplotlib from matplotlib.backends.backend_svg import new_figure_manager from matplotlib._pylab_helpers import Gcf @@ -64,7 +66,15 @@ def flush_svg(): def send_svg_figure(fig): """Draw the current figure and send it as an SVG payload. """ + # For an empty figure, don't even bother calling figure_to_svg, to avoid + # big blank spaces in the qt console + if not fig.axes: + return + svg = figure_to_svg(fig) + # flush text streams before sending figures, helps a little with output + # synchronization in the console (though it's a bandaid, not a real sln) + sys.stdout.flush(); sys.stderr.flush() publish_display_data( 'IPython.zmq.pylab.backend_inline.send_svg_figure', 'Matplotlib Plot',