How to integrate a Ipython console in a PyQT appli

2019-04-28 13:31发布

问题:

I am developing PyQt software for my lab. In this software, I am loading different kind of RAW and analyzed data from a mySQL database (usually in arrays).

I would like to integrate an Iython console in a Widget, so that I could interact easily with these data.

I had some difficulties with Ipython 0.13 to do this.
Here is what I already have (The whole code is very long, so I just show the part containing the widget, the Ipython console and the corresponding import line, if you need more, just tell me):

##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()

So, as wanted, an Ipython console is launched, and seems to work, but I can not access the whole application variables ,arrays etc... I think the Ipython console is launched independently from my software, but here is my limit in programming... Does someone know how to launch Ipython within my application? Maybe a missing parameter, or a different way to integrate Ipython.

for information, this doesn't work: Embedding IPython Qt console in a PyQt application

Thank you for your help!!

回答1:

The mentioned link seems to be working flawless here:

#!/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_())