1

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

1 Answers1

1

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")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Accurate as always! Thanks eyllanesc, I still wonder how come I didn't realize that earlier! – Trifon Trifonov Dec 16 '18 at 21:12
  • OK eyllanesc, maybe is not the place, but since this problem is solved can you advice how to "pull" variables/objects fro the "console_widget" to the main widget? For example if I type in the Jupyter shell "fit.epoch = 0" to get that in the gui? – Trifon Trifonov Dec 16 '18 at 21:18
  • 1
    @TrifonTrifonov that depends on qtconsole, and unfortunately I'm not an expert, in conclusion: qtconsole must have a method to get the objects you enter by console, after that is to parse the data and display it in the GUI. – eyllanesc Dec 16 '18 at 21:21
  • Dear eyllanesc, Can you help once again with this thread? I realized that the qtconsole is hanging my GUI when I start a more time demanding job. I even open a thread, see here: https://stackoverflow.com/questions/54583405/qtconsole-hangs-pyqt5-gui-untill-job-is-finished but I do not have a response, yet. I am sure it should be done with a QThread, but so far, I can not make it work. – Trifon Trifonov Feb 09 '19 at 19:02