| 1 |
"""An in-process qt console app.""" |
| 2 |
|
| 3 |
import os |
| 4 |
|
| 5 |
import tornado |
| 6 |
from IPython.lib import guisupport |
| 7 |
from qtconsole.inprocess import QtInProcessKernelManager |
| 8 |
from qtconsole.rich_ipython_widget import RichIPythonWidget |
| 9 |
|
| 10 |
assert tornado.version_info >= (6, 1) |
| 11 |
|
| 12 |
|
| 13 |
def print_process_id(): |
| 14 |
"""Print the process id.""" |
| 15 |
print("Process ID is:", os.getpid()) |
| 16 |
|
| 17 |
|
| 18 |
def main(): |
| 19 |
"""The main entry point.""" |
| 20 |
# Print the ID of the main process |
| 21 |
print_process_id() |
| 22 |
|
| 23 |
app = guisupport.get_app_qt4() |
| 24 |
|
| 25 |
# Create an in-process kernel |
| 26 |
# >>> print_process_id() |
| 27 |
# will print the same process ID as the main process |
| 28 |
kernel_manager = QtInProcessKernelManager() |
| 29 |
kernel_manager.start_kernel() |
| 30 |
kernel = kernel_manager.kernel |
| 31 |
kernel.gui = "qt4" |
| 32 |
kernel.shell.push({"foo": 43, "print_process_id": print_process_id}) |
| 33 |
|
| 34 |
kernel_client = kernel_manager.client() |
| 35 |
kernel_client.start_channels() |
| 36 |
|
| 37 |
def stop(): |
| 38 |
kernel_client.stop_channels() |
| 39 |
kernel_manager.shutdown_kernel() |
| 40 |
app.exit() |
| 41 |
|
| 42 |
control = RichIPythonWidget() |
| 43 |
control.kernel_manager = kernel_manager |
| 44 |
control.kernel_client = kernel_client |
| 45 |
control.exit_requested.connect(stop) |
| 46 |
control.show() |
| 47 |
|
| 48 |
guisupport.start_event_loop_qt4(app) |
| 49 |
|
| 50 |
|
| 51 |
if __name__ == "__main__": |
| 52 |
main() |