public
Last active — forked from epatters/embedded_qtconsole.py

  • Download Gist
embedded_qtconsole.py
Python
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
import os
 
from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.frontend.qt.kernelmanager import QtKernelManager
from IPython.frontend.qt.inprocess_kernelmanager 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_manager.start_channels()
 
def stop():
kernel_manager.shutdown_kernel()
kernel_manager.stop_channels()
app.exit()
 
control = RichIPythonWidget()
control.kernel_manager = kernel_manager
control.exit_requested.connect(stop)
control.show()
 
guisupport.start_event_loop_qt4(app)
 
 
if __name__ == '__main__':
main()
embedded_terminal.py
Python
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
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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.