backend_inline.py
81 lines
| 2.5 KiB
| text/x-python
|
PythonLexer
Fernando Perez
|
r2890 | """Produce SVG versions of active plots for display by the rich Qt frontend. | ||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r2987 | from __future__ import print_function | ||
Fernando Perez
|
r2890 | |||
epatters
|
r2756 | # Standard library imports | ||
Fernando Perez
|
r3731 | import sys | ||
epatters
|
r2756 | |||
Fernando Perez
|
r3731 | # Third-party imports | ||
Fernando Perez
|
r2987 | import matplotlib | ||
epatters
|
r2756 | from matplotlib.backends.backend_svg import new_figure_manager | ||
from matplotlib._pylab_helpers import Gcf | ||||
# Local imports. | ||||
Brian Granger
|
r3277 | from IPython.core.displaypub import publish_display_data | ||
Brian Granger
|
r3280 | from IPython.lib.pylabtools import figure_to_svg | ||
epatters
|
r2756 | |||
Fernando Perez
|
r2890 | #----------------------------------------------------------------------------- | ||
# Functions | ||||
#----------------------------------------------------------------------------- | ||||
epatters
|
r2756 | |||
Fernando Perez
|
r3733 | def show(close=True): | ||
Fernando Perez
|
r2987 | """Show all figures as SVG payloads sent to the IPython clients. | ||
Parameters | ||||
---------- | ||||
close : bool, optional | ||||
If true, a ``plt.close('all')`` call is automatically issued after | ||||
Brian Granger
|
r3280 | sending all the SVG figures. If this is set, the figures will entirely | ||
removed from the internal list of figures. | ||||
epatters
|
r2756 | """ | ||
Fernando Perez
|
r2890 | for figure_manager in Gcf.get_all_fig_managers(): | ||
Brian Granger
|
r3280 | send_svg_figure(figure_manager.canvas.figure) | ||
Fernando Perez
|
r2987 | if close: | ||
matplotlib.pyplot.close('all') | ||||
Brian Granger
|
r3280 | |||
Fernando Perez
|
r2987 | # This flag will be reset by draw_if_interactive when called | ||
show._draw_called = False | ||||
def draw_if_interactive(): | ||||
""" | ||||
Is called after every pylab drawing command | ||||
""" | ||||
# We simply flag we were called and otherwise do nothing. At the end of | ||||
# the code execution, a separate call to show_close() will act upon this. | ||||
show._draw_called = True | ||||
def flush_svg(): | ||||
"""Call show, close all open figures, sending all SVG images. | ||||
This is meant to be called automatically and will call show() if, during | ||||
prior code execution, there had been any calls to draw_if_interactive. | ||||
""" | ||||
if show._draw_called: | ||||
Brian Granger
|
r3280 | show() | ||
Fernando Perez
|
r2987 | show._draw_called = False | ||
Brian Granger
|
r3280 | |||
def send_svg_figure(fig): | ||||
"""Draw the current figure and send it as an SVG payload. | ||||
""" | ||||
Fernando Perez
|
r3731 | # 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 | ||||
Brian Granger
|
r3280 | svg = figure_to_svg(fig) | ||
Fernando Perez
|
r3731 | # 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() | ||||
Brian Granger
|
r3280 | publish_display_data( | ||
'IPython.zmq.pylab.backend_inline.send_svg_figure', | ||||
'Matplotlib Plot', | ||||
{'image/svg+xml' : svg} | ||||
) | ||||