Advertisement
Guest User

Untitled

a guest
Sep 10th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. import atexit
  2.  
  3. from PySide.QtCore import QTimer
  4.  
  5. from IPython.zmq.ipkernel import IPKernelApp
  6. from IPython.lib.kernel import find_connection_file
  7. from IPython.frontend.qt.kernelmanager import QtKernelManager
  8. from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
  9. from IPython.config.application import catch_config_error
  10.  
  11.  
  12. class QIPythonWidget(RichIPythonWidget):
  13.  
  14.     class KernelApp(IPKernelApp):
  15.         @catch_config_error
  16.         def initialize(self, argv=[]):
  17.             super(QIPythonWidget.KernelApp, self).initialize(argv)
  18.             self.kernel.eventloop = self.loop_qt4_nonblocking
  19.             self.kernel.start()
  20.             self.start()
  21.  
  22.         def loop_qt4_nonblocking(self, kernel):
  23.             kernel.timer = QTimer()
  24.             kernel.timer.timeout.connect(kernel.do_one_iteration)
  25.             kernel.timer.start(1000*kernel._poll_interval)
  26.  
  27.         def get_connection_file(self):
  28.             return self.connection_file
  29.  
  30.         def get_user_namespace(self):
  31.             return self.kernel.shell.user_ns
  32.  
  33.     def __init__(self, parent=None, colors='linux', instance_args=[]):
  34.         super(QIPythonWidget, self).__init__()
  35.         self.app = self.KernelApp.instance(argv=instance_args)
  36.        
  37.     def initialize(self, colors='linux'):
  38.         self.app.initialize()
  39.         self.set_default_style(colors=colors)
  40.         self.connect_kernel(self.app.get_connection_file())
  41.  
  42.     def connect_kernel(self, conn, heartbeat=False):
  43.         km = QtKernelManager(connection_file=find_connection_file(conn))
  44.         km.load_connection_file()
  45.         km.start_channels(hb=heartbeat)
  46.         self.kernel_manager = km
  47.         atexit.register(self.kernel_manager.cleanup_connection_file)
  48.  
  49.     def get_user_namespace(self):
  50.         return self.app.get_user_namespace()
  51.  
  52.     def run_cell(self, *args, **kwargs):
  53.         return self.app.shell.run_cell(*args, **kwargs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement