dmon
dmon

Reputation: 1422

Adding a Python/IPython console widget to Qt application

I have searched and searched for good information on this but I have been unable to find a solution.

I am working on a Qt application which has an embedded python interpreter - all working nicely! The user may drive the application via python code processed by the embedded interpreter.

My problem is that the "console" is little more than a line edit widget which essentially allows the user input text to the embedded interpreter.

What I really need is python console widget driving my embedded python interpreter, with tab complete. Tab complete is virtually essential. Text highlighting would be a bonus. If I could even integrate a normal python console I could start ipython using the "from IPython import embed; embed()" trick.

There may be a hundred ways to do this, and it may be obvious to some, but it honestly has me beat! Any assistance would be greatly appreciated.

Thanks :)

Upvotes: 0

Views: 1943

Answers (1)

alvaro
alvaro

Reputation: 11

I had the same problem. There was a compability problem. Firstly I downloaded Pycharm (a editor) and I used this code like a widget. It works.

import sip

sip.setapi(u'QDate', 2)
sip.setapi(u'QDateTime', 2)
sip.setapi(u'QString', 2)
sip.setapi(u'QTextStream', 2)
sip.setapi(u'QTime', 2)
sip.setapi(u'QUrl', 2)
sip.setapi(u'QVariant', 2)


from IPython.lib import guisupport

from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager

class IPythonWidget(RichJupyterWidget):
    def __init__(self,customBanner=None,*args,**kwargs):
        if not customBanner is None: self.banner=customBanner
        super(IPythonWidget, self).__init__(*args,**kwargs)
        self.kernel_manager = kernel_manager = QtInProcessKernelManager()
        kernel_manager.start_kernel()
        kernel_manager.kernel.gui = 'qt4'
        self.kernel_client = kernel_client = self._kernel_manager.client()
        kernel_client.start_channels()

        def stop():
            kernel_client.stop_channels()
            kernel_manager.shutdown_kernel()
            guisupport.get_app_qt4().exit()
        self.exit_requested.connect(stop)

    def pushVariables(self,variableDict):
        """ Given a dictionary containing name / value pairs, push those variables to the IPython console widget """
        self.kernel_manager.kernel.shell.push(variableDict)
    def clearTerminal(self):
        """ Clears the terminal """
        self._control.clear()

    def printText(self,text):
        """ Prints some plain text to the console """
        #self._append_plain_text(text)
        self.append_stream(text)
    def executeCommand(self,command):
        """ Execute a command in the frame of the console widget """
      #  self._execute(command,False)
        self.execute(command,False)

Upvotes: 1

Related Questions