##// END OF EJS Templates
Backport PR #10974: IPython breaks import matplotlib.pyplot when there is no display
Matthias Bussonnier -
Show More
@@ -1,35 +1,49 b''
1 import sys
1 import sys
2 import os
2 from IPython.external.qt_for_kernel import QtCore, QtGui
3 from IPython.external.qt_for_kernel import QtCore, QtGui
3
4
4 # If we create a QApplication, keep a reference to it so that it doesn't get
5 # If we create a QApplication, keep a reference to it so that it doesn't get
5 # garbage collected.
6 # garbage collected.
6 _appref = None
7 _appref = None
7
8 _already_warned = False
8
9
9 def inputhook(context):
10 def inputhook(context):
10 global _appref
11 global _appref
11 app = QtCore.QCoreApplication.instance()
12 app = QtCore.QCoreApplication.instance()
12 if not app:
13 if not app:
14 if sys.platform == 'linux':
15 if not os.environ.get('DISPLAY') \
16 and not os.environ.get('WAYLAND_DISPLAY'):
17 import warnings
18 global _already_warned
19 if not _already_warned:
20 _already_warned = True
21 warnings.warn(
22 'The DISPLAY or WAYLAND_DISPLAY enviroment variable is '
23 'not set or empty and Qt5 requires this enviroment '
24 'variable. Deactivate Qt5 code.'
25 )
26 return
13 _appref = app = QtGui.QApplication([" "])
27 _appref = app = QtGui.QApplication([" "])
14 event_loop = QtCore.QEventLoop(app)
28 event_loop = QtCore.QEventLoop(app)
15
29
16 if sys.platform == 'win32':
30 if sys.platform == 'win32':
17 # The QSocketNotifier method doesn't appear to work on Windows.
31 # The QSocketNotifier method doesn't appear to work on Windows.
18 # Use polling instead.
32 # Use polling instead.
19 timer = QtCore.QTimer()
33 timer = QtCore.QTimer()
20 timer.timeout.connect(event_loop.quit)
34 timer.timeout.connect(event_loop.quit)
21 while not context.input_is_ready():
35 while not context.input_is_ready():
22 timer.start(50) # 50 ms
36 timer.start(50) # 50 ms
23 event_loop.exec_()
37 event_loop.exec_()
24 timer.stop()
38 timer.stop()
25 else:
39 else:
26 # On POSIX platforms, we can use a file descriptor to quit the event
40 # On POSIX platforms, we can use a file descriptor to quit the event
27 # loop when there is input ready to read.
41 # loop when there is input ready to read.
28 notifier = QtCore.QSocketNotifier(context.fileno(),
42 notifier = QtCore.QSocketNotifier(context.fileno(),
29 QtCore.QSocketNotifier.Read)
43 QtCore.QSocketNotifier.Read)
30 # connect the callback we care about before we turn it on
44 # connect the callback we care about before we turn it on
31 notifier.activated.connect(event_loop.exit)
45 notifier.activated.connect(event_loop.exit)
32 notifier.setEnabled(True)
46 notifier.setEnabled(True)
33 # only start the event loop we are not already flipped
47 # only start the event loop we are not already flipped
34 if not context.input_is_ready():
48 if not context.input_is_ready():
35 event_loop.exec_()
49 event_loop.exec_()
General Comments 0
You need to be logged in to leave comments. Login now