Skip to content

Instantly share code, notes, and snippets.

@stephanh42
Forked from pberkes/embedded_qtconsole.py
Last active August 29, 2015 14:10
Show Gist options
  • Save stephanh42/a8db0dc2e9cc5aaf4214 to your computer and use it in GitHub Desktop.
Save stephanh42/a8db0dc2e9cc5aaf4214 to your computer and use it in GitHub Desktop.
import os
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.qt.manager import QtKernelManager
from IPython.qt.inprocess import QtInProcessKernelManager
from IPython.kernel.zmq.ipkernel import Kernel
from IPython.kernel.inprocess.ipkernel import InProcessKernel
from IPython.lib import guisupport
def print_process_id():
print('Process ID is:', os.getpid())
def main():
# Print the ID of the main process
print_process_id()
app = guisupport.get_app_qt4()
# Create an in-process kernel
# >>> print_process_id()
# will print the same process ID as the main process
kernel = InProcessKernel(gui='qt4')
kernel.shell.push({'foo': 43, 'print_process_id': print_process_id})
kernel_manager = QtInProcessKernelManager(kernel=kernel)
# Uncomment these lines to use a kernel on a separate process
# >>> import os; print os.getpid()
# will give a different process ID
#kernel = Kernel(gui='qt4')
#kernel_manager = QtKernelManager(kernel=kernel)
kernel_manager.start_kernel()
kernel_client = kernel_manager.client()
kernel_client.start_channels()
def stop():
kernel_manager.shutdown_kernel()
kernel_client.stop_channels()
app.exit()
control = RichIPythonWidget()
control.kernel_manager = kernel_manager
control.kernel_client = kernel_client
control.exit_requested.connect(stop)
control.show()
guisupport.start_event_loop_qt4(app)
if __name__ == '__main__':
main()
import os
from IPython.kernel.blockingkernelmanager import BlockingKernelManager
from IPython.kernel.inprocess.blockingkernelmanager import BlockingInProcessKernelManager
from IPython.kernel.zmq.ipkernel import Kernel
from IPython.kernel.inprocess.ipkernel import InProcessKernel
# Note: ZMQTerminalInteractiveShell does not actually depend on ZMQ.
from IPython.frontend.terminal.console.interactiveshell import \
ZMQTerminalInteractiveShell
def print_process_id():
print 'Process ID is:', os.getpid()
def main():
print_process_id()
# Create an in-process kernel
# >>> print_process_id()
# will print the same process ID as the main process
kernel = InProcessKernel(gui='qt4')
kernel.shell.push({'foo': 43, 'print_process_id': print_process_id})
kernel_manager = BlockingInProcessKernelManager(kernel=kernel)
# Uncomment these lines to use a kernel on a separate process
# >>> import os; print os.getpid()
# will give a different process ID
#kernel = Kernel(gui='qt4')
#kernel_manager = BlockingKernelManager()
kernel_manager.start_kernel()
kernel_manager.start_channels()
shell = ZMQTerminalInteractiveShell(kernel_manager=kernel_manager)
shell.mainloop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment