如何在PyQT应用中集成Ipython控制台

6
我正在为实验室开发PyQt软件。在该软件中,我从mySQL数据库中加载不同类型的原始数据和分析数据(通常是数组)。
我想将Ipython控制台集成到小部件中,以便可以轻松地与这些数据交互。
我在使用Ipython 0.13时遇到了一些困难。以下是我已经拥有的代码(整个代码非常长,因此我只展示包含小部件、Ipython控制台和相应导入行的部分,如果需要更多信息,请告诉我):
##I load everything useful to my application, including the following line
from IPython.frontend.qt.console.qtconsoleapp import IPythonQtConsoleApp

##then is my whole software
##here is a class containing the Graphical User Interface elements. A button call the following function. self.Shell_Widget is the widget containing the Ipython console, self.MainWindow is the application mainwindow
def EmbeddedIpython(self):
    """
    This function should launch an Ipython console
    """


    self.Shell_Widget = QtGui.QDockWidget(self.MainWindow) #Widget creation
    self.MainWindow.addDockWidget(4,self.Shell_Widget)
    self.Shell_Widget.setMinimumSize(400,420)

    console = IPythonQtConsoleApp() #Console Creation
    console.initialize()
    console.start()


    self.Shell_Widget.show()

所以,按照要求,已经启动了一个Ipython控制台,看起来可以正常工作,但我无法访问整个应用程序的变量、数组等。我认为Ipython控制台是独立于我的软件启动的,但这是我在编程方面的限制......有人知道如何在我的应用程序中启动Ipython吗?也许是缺少参数,或者是集成Ipython的不同方法。
另外,这个链接并不能解决问题: 在PyQt应用程序中嵌入IPython Qt控制台 感谢您的帮助!
1个回答

2

这个链接 在这里似乎完美地工作:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import atexit

from IPython.zmq.ipkernel import IPKernelApp
from IPython.lib.kernel import find_connection_file
from IPython.frontend.qt.kernelmanager import QtKernelManager
from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.utils.traitlets import TraitError

from PyQt4 import QtGui, QtCore

def event_loop(kernel):
    kernel.timer = QtCore.QTimer()
    kernel.timer.timeout.connect(kernel.do_one_iteration)
    kernel.timer.start(1000*kernel._poll_interval)

def default_kernel_app():
    app = IPKernelApp.instance()
    app.initialize(['python', '--pylab=qt'])
    app.kernel.eventloop = event_loop
    return app

def default_manager(kernel):
    connection_file = find_connection_file(kernel.connection_file)
    manager = QtKernelManager(connection_file=connection_file)
    manager.load_connection_file()
    manager.start_channels()
    atexit.register(manager.cleanup_connection_file)
    return manager

def console_widget(manager):
    try: # Ipython v0.13
        widget = RichIPythonWidget(gui_completion='droplist')
    except TraitError:  # IPython v0.12
        widget = RichIPythonWidget(gui_completion=True)
    widget.kernel_manager = manager
    return widget

def terminal_widget(**kwargs):
    kernel_app = default_kernel_app()
    manager = default_manager(kernel_app)
    widget = console_widget(manager)

    #update namespace                                                           
    kernel_app.shell.user_ns.update(kwargs)

    kernel_app.start()
    return widget

class mainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(mainWindow, self).__init__(parent)

        self.console = terminal_widget(testing=123)

        print "\nAn Embeded Ipython Console!"

        self.textEdit = QtGui.QTextEdit()

        self.dockShell = QtGui.QDockWidget(self)
        self.dockShell.setWidget(self.textEdit)

        self.addDockWidget(4, self.dockShell)
        self.setCentralWidget(self.console)

if __name__ == "__main__":
    import  sys

    app  = QtGui.QApplication(sys.argv)
    main = mainWindow()
    main.show()
    sys.exit(app.exec_())

好的例子。我看到mainWindow通过terminal_widget运行default_kernel_app,它创建了app = IPKernelApp.instance(),其中包含良好的app.kernel.connection_file,可以在外部使用。问题是:如何将变量从if __name__ == "__main__"上下文共享,以便在交互模式下使用它?例如:从ipython控制台处理app = QtGui.QApplication(sys.argv) - Jo Ja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接