##// END OF EJS Templates
ask_yes_no on %%file overwrite
ask_yes_no on %%file overwrite

File last commit:

r4259:657a41be
r7305:234309d5
Show More
qt_for_kernel.py
87 lines | 2.8 KiB | text/x-python | PythonLexer
epatters
Support v2 PyQt4 APIs and PySide in kernel's GUI support....
r4149 """ Import Qt in a manner suitable for an IPython kernel.
MinRK
reorder qt support in kernel...
r4191
This is the import used for the `gui=qt` or `pylab=qt` initialization.
MinRK
update qt import priority per recent discussion...
r4192 Import Priority:
MinRK
reorder qt support in kernel...
r4191
MinRK
update qt import priority per recent discussion...
r4192 if matplotlib has been imported and doesn't support v2 (<= 1.0.1):
use PyQt4 @v1
MinRK
reorder qt support in kernel...
r4191
MinRK
update qt import priority per recent discussion...
r4192 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
MinRK
reorder qt support in kernel...
r4191
MinRK
update qt import priority per recent discussion...
r4192 else: (matplotlib said nothing)
# this is the default path - nobody told us anything
MinRK
reorder qt support in kernel...
r4191 try:
PyQt @v1
except:
fallback on PySide
MinRK
update qt import priority per recent discussion...
r4192 else:
use PyQt @v2 or PySide, depending on QT_API
because ETS doesn't work with PyQt @v1.
epatters
Support v2 PyQt4 APIs and PySide in kernel's GUI support....
r4149 """
MinRK
reorder qt support in kernel...
r4191 import os
epatters
Support v2 PyQt4 APIs and PySide in kernel's GUI support....
r4149 import sys
MinRK
demote sip API failure to warning from ImportError
r4259 from IPython.utils.warn import warn
epatters
Support v2 PyQt4 APIs and PySide in kernel's GUI support....
r4149 matplotlib = sys.modules.get('matplotlib')
MinRK
update qt import priority per recent discussion...
r4192 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
MinRK
reorder qt support in kernel...
r4191 else:
MinRK
update qt import priority per recent discussion...
r4192 # ask QT_API ETS variable *first*
MinRK
reorder qt support in kernel...
r4191 QT_API = os.environ.get('QT_API', None)
if QT_API is None:
MinRK
update qt import priority per recent discussion...
r4192 # 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.
MinRK
reorder qt support in kernel...
r4191 try:
MinRK
update qt import priority per recent discussion...
r4192 # default to unconfigured PyQt4
from PyQt4 import QtCore, QtGui
MinRK
reorder qt support in kernel...
r4191 except ImportError:
MinRK
update qt import priority per recent discussion...
r4192 # 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)
MinRK
reorder qt support in kernel...
r4191 else:
# QT_API specified, use PySide or PyQt+v2 API from external.qt
# this means ETS is likely to be used, which requires v2
MinRK
update qt import priority per recent discussion...
r4192 try:
from IPython.external.qt import QtCore, QtGui
except ValueError as e:
if 'API' in str(e):
MinRK
demote sip API failure to warning from ImportError
r4259 # PyQt4 already imported, and APIv2 couldn't be set
# Give more meaningful message, and warn instead of raising
warn("""
MinRK
update qt import priority per recent discussion...
r4192 Assigning the ETS variable `QT_API=pyqt` implies PyQt's v2 API for
QString and QVariant, but PyQt has already been imported
MinRK
demote sip API failure to warning from ImportError
r4259 with v1 APIs. You should unset QT_API to work with PyQt4
MinRK
update qt import priority per recent discussion...
r4192 in its default mode.
""")
MinRK
demote sip API failure to warning from ImportError
r4259 # allow it to still work
from PyQt4 import QtCore, QtGui
MinRK
update qt import priority per recent discussion...
r4192 else:
raise
MinRK
reorder qt support in kernel...
r4191