-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPythonEmbeded.py
68 lines (52 loc) · 1.86 KB
/
PythonEmbeded.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# this code is based on
# stackoverflow.com/questions/11513132/embedding-ipython-qt-console-in-a-pyqt-application
# stackoverflow.com/questions/26666583/embedding-ipython-qtconsole-and-passing-objects
#
# version 1.0 -- 2020-04-24 -- JLC -- Initial revision
#
from qtconsole.qt import QtGui
from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager
class ConsoleWidget(RichJupyterWidget):
def __init__(self, customBanner=None, *args, **kwargs):
super().__init__(*args, **kwargs)
if customBanner is not None:
self.banner = customBanner
self.font_size = 6
self.kernel_manager = QtInProcessKernelManager()
self.kernel_manager.start_kernel(show_banner=False)
self.kernel_manager.kernel.gui = 'qt'
self.kernel_client = self.kernel_manager.client()
self.kernel_client.start_channels()
def stop():
self.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()
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_()