Skip to content

Instantly share code, notes, and snippets.

@thesamprice
Created April 9, 2013 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thesamprice/5343088 to your computer and use it in GitHub Desktop.
Save thesamprice/5343088 to your computer and use it in GitHub Desktop.
If I import matplot and plot a frame with an IPython console window open IPython goes crazy and seg faults my app. I tried configuring my matplotlibrc to use Qt4 as the backend but no joy :(. Not sure how to chase down why IPython is crashing. Im using the 1.0-dev build. and 1.2.1 of matplotlib Any Help?
import os
from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.frontend.qt.console.ipython_widget import IPythonWidget
from IPython.frontend.qt.kernelmanager import QtKernelManager
from IPython.frontend.qt.inprocess_kernelmanager import QtInProcessKernelManager
from IPython.kernel.zmq.ipkernel import Kernel
from IPython.kernel.inprocess.ipkernel import InProcessKernel
from IPython.lib import guisupport
def print_process_id():
print 'Process ID is:', os.getpid()
def main(arguments={}):
# Print the ID of the main process
#print_process_id()
app = guisupport.get_app_qt4()
# Create an in-process kernel
# >>> print_process_id()
# will print the same process ID as the main process
kernel = InProcessKernel(gui='qt4')
kernel.shell.push(arguments)
kernel_manager = QtInProcessKernelManager(kernel=kernel)
# Uncomment these lines to use a kernel on a separate process
# >>> import os; print os.getpid()
# will give a different process ID
#kernel = Kernel(gui='qt4')
#kernel.shell.push(arguments)
#kernel_manager = QtKernelManager(kernel=kernel)
kernel_manager.start_kernel()
kernel_manager.start_channels()
def stop():
kernel_manager.shutdown_kernel()
kernel_manager.stop_channels()
app.exit()
control = RichIPythonWidget()
#control = IPythonWidget()
control.kernel_manager = kernel_manager
control.exit_requested.connect(stop)
control.show()
#guisupport.start_event_loop_qt4(app)
if __name__ == '__main__':
main()
import sys
import console_ipython
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt
import random
from PyQt4.QtCore import QT_VERSION_STR
from PyQt4.pyqtconfig import Configuration
print("Qt version:", QT_VERSION_STR)
cfg = Configuration()
print("SIP version:", cfg.sip_version_str)
print("PyQt version:", cfg.pyqt_version_str)
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
# a figure instance to plot on
self.figure = plt.figure()
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure)
# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)
# Just some button connected to `plot` method
self.button = QtGui.QPushButton('Plot')
self.button.clicked.connect(self.plot)
# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.button)
self.setLayout(layout)
console_ipython.main()
def plot(self):
console_ipython.main()
''' plot some random stuff '''
# random data
data = [random.random() for i in range(10)]
# create an axis
ax = self.figure.add_subplot(111)
# discards the old graph
ax.hold(False)
# plot data
ax.plot(data, '*-')
# refresh canvas
self.canvas.draw()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment