我希望在我的应用程序中嵌入一个 IPython Qt 控制台,使用RichJupyterWidget
. 对于如何设置控制台,我似乎有两种选择:
- 使用
QtInProcessKernelManager
(如下)。在这种情况下,内核在执行不理想的代码时会阻塞,因为我希望用户运行的某些命令最多需要一分钟。 - 使用
QtKernelManager
(如在此GitHub 示例中)创建作为子进程启动的普通 IPython 内核。在这种情况下,我无法将主进程中的命名空间传递到内核,并且内核无法与主应用程序通信。这两个都是要求。
有可能在这里两全其美吗?我想要一个非阻塞控制台,它包含与我的主进程相同的命名空间,并且可以向主应用程序发送信号。waszil在此处的评论中提出了类似的问题。使用QThread
withQtInProcessKernelManager
是可能的,但我不确定我应该使用哪种方法。
from qtconsole.qt import QtGui
from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager
class ConsoleWidget(RichJupyterWidget):
def __init__(self, namespace={}, customBanner=None, *args, **kwargs):
super(ConsoleWidget, self).__init__(*args, **kwargs)
if customBanner is not None:
self.banner = customBanner
self.font_size = 6
self.kernel_manager = kernel_manager = QtInProcessKernelManager()
kernel_manager.start_kernel(show_banner=False)
kernel_manager.kernel.gui = 'qt'
self.kernel_client = kernel_client = self._kernel_manager.client()
kernel_client.start_channels()
self.push_vars(namespace)
def stop():
kernel_client.stop_channels()
kernel_manager.shutdown_kernel()
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)
def execute_command(self, command):
"""
Execute a command in the frame of the console widget
"""
self._execute(command, False)
if __name__ == '__main__':
app = QtGui.QApplication([])
widget = ConsoleWidget()
widget.show()
app.exec_()