Search code examples
pythonpyqtpyqt5qtconsole

embedded qtconsole in pyqt5 aplication does not works as expected


I have a rather large project with an embedded Jupyter QtConsole 4.4.2.

For more details see: https://github.com/3fon3fonov/trifon

Now, while some of the processes are running/finished I am trying to send some text to the Qtconsole and use it as an output screen (and vice-versa I want to the Jupyter to be able to take control over the GUI if needed, but this is another problem I have to deal later on).

The problem is that some intrinsic to the "ConsoleWidget" functions do not seems to work, and I cannotfind the reason for that...

For example in my main GUI code:

ConsoleWidget_embed().push_vars({'fit':fit}) <-- WORKS!   

ConsoleWidget_embed().clear() <-- Does not work!
ConsoleWidget_embed().print_text("Test string")  <-- Does not work!

Here is the code I am embedding in a Qtab.

import numpy as np
import sys #,os
from PyQt5 import QtCore, QtGui, QtWidgets, uic

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


class ConsoleWidget_embed(RichJupyterWidget,ConsoleWidget):
    global fit

    def __init__(self, customBanner=None, *args, **kwargs):
        super(ConsoleWidget_embed, self).__init__(*args, **kwargs)

        if customBanner is not None:
            self.banner = customBanner

        #self.font_size = 4
        self.kernel_manager =   QtInProcessKernelManager()
        self.kernel_manager.start_kernel(show_banner=True)
        self.kernel_manager.kernel.gui = 'qt'
        self.kernel = self.kernel_manager.kernel
        self.kernel_client = self._kernel_manager.client()
        self.kernel_client.start_channels()

        #self._execute("kernel = %s"%fit, False) 

        def stop():
            self.kernel_client.stop_channels()
            self.kernel_manager.shutdown_kernel()
            self.guisupport.get_app_qt().exit()

        self.exit_requested.connect(stop)


    def push_vars(self, variableDict):
        """
        Given a dictionary containing name / value pairs, push those variables
        to the Jupyter console widget
        """
        self.kernel_manager.kernel.shell.push(variableDict)

    def clear(self):
        """
        Clears the terminal
        """
        self._control.clear()

        # self.kernel_manager

    def print_text(self, text):
        """
        Prints some plain text to the console
        """
        self._append_plain_text(text, before_prompt=False)

    def execute_command(self, command):
        """
        Execute a command in the frame of the console widget
        """
        self._execute(command, False)




if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main = mainWindow()
    main.show()
    sys.exit(app.exec_())  

Any ideas will be highly appreciated !

All the best, Trifon


Solution

  • The problem in your case is that you are creating a ConsoleWidget_embed every time you type ConsoleWidget_embed(), instead create an object of that class and set it as an attribute of the class:

        self.console_widget = ConsoleWidget_embed()
        self.terminal_embeded.addTab(self.console_widget, "Jupyter")
    
        # ...
    
    def jupiter_push_vars(self):
        global fit        
        self.console_widget.push_vars({'fit':fit})   
        self.console_widget.clear()
        self.console_widget.print_text("Test string")