##// END OF EJS Templates
Backport PR #2384: Adapt inline backend to changes in matplotlib...
Backport PR #2384: Adapt inline backend to changes in matplotlib Matplotlib recently merged https://github.com/matplotlib/matplotlib/pull/1125 that makes it simpler to use objective oriented figure creation by automatically creating the right canvas for the backend. To solve that all backends must provide a backend_xxx.FigureCanvas. This is obviosly missing from the inline backend. The change is needed to make the inline backend work with mpl's 1.2.x branch which is due to released soon. Simply setting the default canvas equal to a Agg canvas appears to work for both svg and png figures but I'm not sure weather that is the right approach. Should the canvas depend on the figure format and provide a svg canvas for a svg figure? (Note that before this change to matplotlib the canvas from a plt.figure call seams to be a agg type in all cases) Edit: I made the pull request against 0.13.1 since it would be good to have this in the stable branch for when mpl is released. Just let me know and I can rebase it against master

File last commit:

r4259:657a41be
r8562:7d16877a
Show More
qt_for_kernel.py
87 lines | 2.8 KiB | text/x-python | PythonLexer
""" Import Qt in a manner suitable for an IPython kernel.
This is the import used for the `gui=qt` or `pylab=qt` initialization.
Import Priority:
if matplotlib has been imported and doesn't support v2 (<= 1.0.1):
use PyQt4 @v1
Next, ask ETS' QT_API env variable
if QT_API not set:
ask matplotlib via rcParams['backend.qt4']
if it said PyQt:
use PyQt4 @v1
elif it said PySide:
use PySide
else: (matplotlib said nothing)
# this is the default path - nobody told us anything
try:
PyQt @v1
except:
fallback on PySide
else:
use PyQt @v2 or PySide, depending on QT_API
because ETS doesn't work with PyQt @v1.
"""
import os
import sys
from IPython.utils.warn import warn
matplotlib = sys.modules.get('matplotlib')
if matplotlib and matplotlib.__version__ <= '1.0.1':
# 1.0.1 doesn't support pyside or v2, so stick with PyQt @v1,
# and ignore everything else
from PyQt4 import QtCore, QtGui
else:
# ask QT_API ETS variable *first*
QT_API = os.environ.get('QT_API', None)
if QT_API is None:
# QT_API not set, ask matplotlib if it was imported (e.g. `pylab=qt`)
if matplotlib:
mpqt = matplotlib.rcParams.get('backend.qt4', None)
else:
mpqt = None
if mpqt is None:
# matplotlib not imported or had nothing to say.
try:
# default to unconfigured PyQt4
from PyQt4 import QtCore, QtGui
except ImportError:
# fallback on PySide
try:
from PySide import QtCore, QtGui
except ImportError:
raise ImportError('Cannot import PySide or PyQt4')
elif mpqt.lower() == 'pyqt4':
# import PyQt4 unconfigured
from PyQt4 import QtCore, QtGui
elif mpqt.lower() == 'pyside':
from PySide import QtCore, QtGui
else:
raise ImportError("unhandled value for backend.qt4 from matplotlib: %r"%mpqt)
else:
# QT_API specified, use PySide or PyQt+v2 API from external.qt
# this means ETS is likely to be used, which requires v2
try:
from IPython.external.qt import QtCore, QtGui
except ValueError as e:
if 'API' in str(e):
# PyQt4 already imported, and APIv2 couldn't be set
# Give more meaningful message, and warn instead of raising
warn("""
Assigning the ETS variable `QT_API=pyqt` implies PyQt's v2 API for
QString and QVariant, but PyQt has already been imported
with v1 APIs. You should unset QT_API to work with PyQt4
in its default mode.
""")
# allow it to still work
from PyQt4 import QtCore, QtGui
else:
raise