ipythonqt.py
99 lines
| 3.9 KiB
| text/x-python
|
PythonLexer
epatters
|
r2801 | #!/usr/bin/env python | ||
""" A minimal application using the Qt console-style IPython frontend. | ||||
epatters
|
r2758 | """ | ||
# Systemm library imports | ||||
Brian Granger
|
r2873 | from PyQt4 import QtGui | ||
epatters
|
r2758 | |||
# Local imports | ||||
from IPython.external.argparse import ArgumentParser | ||||
epatters
|
r2801 | from IPython.frontend.qt.console.frontend_widget import FrontendWidget | ||
from IPython.frontend.qt.console.ipython_widget import IPythonWidget | ||||
from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget | ||||
epatters
|
r2758 | from IPython.frontend.qt.kernelmanager import QtKernelManager | ||
epatters
|
r2823 | # Constants | ||
LOCALHOST = '127.0.0.1' | ||||
epatters
|
r2758 | |||
def main(): | ||||
epatters
|
r2801 | """ Entry point for application. | ||
epatters
|
r2758 | """ | ||
# Parse command line arguments. | ||||
parser = ArgumentParser() | ||||
epatters
|
r2871 | kgroup = parser.add_argument_group('kernel options') | ||
kgroup.add_argument('-e', '--existing', action='store_true', | ||||
epatters
|
r2823 | help='connect to an existing kernel') | ||
epatters
|
r2871 | kgroup.add_argument('--ip', type=str, default=LOCALHOST, | ||
epatters
|
r2823 | help='set the kernel\'s IP address [default localhost]') | ||
epatters
|
r2871 | kgroup.add_argument('--xreq', type=int, metavar='PORT', default=0, | ||
epatters
|
r2823 | help='set the XREQ channel port [default random]') | ||
epatters
|
r2871 | kgroup.add_argument('--sub', type=int, metavar='PORT', default=0, | ||
epatters
|
r2823 | help='set the SUB channel port [default random]') | ||
epatters
|
r2871 | kgroup.add_argument('--rep', type=int, metavar='PORT', default=0, | ||
epatters
|
r2823 | help='set the REP channel port [default random]') | ||
epatters
|
r2866 | |||
epatters
|
r2871 | egroup = kgroup.add_mutually_exclusive_group() | ||
egroup.add_argument('--pure', action='store_true', help = \ | ||||
'use a pure Python kernel instead of an IPython kernel') | ||||
Brian Granger
|
r2873 | egroup.add_argument('--pylab', type=str, metavar='GUI', nargs='?', | ||
Brian Granger
|
r2872 | const='auto', help = \ | ||
epatters
|
r2880 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ | ||
given, the GUI backend is matplotlib's, otherwise use one of: \ | ||||
['tk', 'gtk', 'qt', 'wx', 'payload-svg'].") | ||||
epatters
|
r2871 | |||
wgroup = parser.add_argument_group('widget options') | ||||
wgroup.add_argument('--paging', type=str, default='inside', | ||||
choices = ['inside', 'hsplit', 'vsplit', 'none'], | ||||
help='set the paging style [default inside]') | ||||
wgroup.add_argument('--rich', action='store_true', | ||||
help='enable rich text support') | ||||
wgroup.add_argument('--tab-simple', action='store_true', | ||||
help='do tab completion ala a Unix terminal') | ||||
Brian Granger
|
r2873 | |||
epatters
|
r2823 | args = parser.parse_args() | ||
Brian Granger
|
r2873 | |||
epatters
|
r2758 | # Don't let Qt or ZMQ swallow KeyboardInterupts. | ||
import signal | ||||
signal.signal(signal.SIGINT, signal.SIG_DFL) | ||||
# Create a KernelManager and start a kernel. | ||||
epatters
|
r2823 | kernel_manager = QtKernelManager(xreq_address=(args.ip, args.xreq), | ||
sub_address=(args.ip, args.sub), | ||||
rep_address=(args.ip, args.rep)) | ||||
if args.ip == LOCALHOST and not args.existing: | ||||
if args.pure: | ||||
kernel_manager.start_kernel(ipython=False) | ||||
elif args.pylab: | ||||
if args.rich: | ||||
kernel_manager.start_kernel(pylab='payload-svg') | ||||
else: | ||||
Brian Granger
|
r2872 | if args.pylab == 'auto': | ||
kernel_manager.start_kernel(pylab='qt4') | ||||
else: | ||||
kernel_manager.start_kernel(pylab=args.pylab) | ||||
epatters
|
r2758 | else: | ||
epatters
|
r2823 | kernel_manager.start_kernel() | ||
epatters
|
r2758 | kernel_manager.start_channels() | ||
epatters
|
r2841 | # Create the widget. | ||
epatters
|
r2758 | app = QtGui.QApplication([]) | ||
epatters
|
r2823 | if args.pure: | ||
kind = 'rich' if args.rich else 'plain' | ||||
epatters
|
r2871 | widget = FrontendWidget(kind=kind, paging=args.paging) | ||
epatters
|
r2823 | elif args.rich: | ||
epatters
|
r2871 | widget = RichIPythonWidget(paging=args.paging) | ||
epatters
|
r2758 | else: | ||
epatters
|
r2871 | widget = IPythonWidget(paging=args.paging) | ||
epatters
|
r2866 | widget.gui_completion = not args.tab_simple | ||
epatters
|
r2758 | widget.kernel_manager = kernel_manager | ||
epatters
|
r2823 | widget.setWindowTitle('Python' if args.pure else 'IPython') | ||
epatters
|
r2758 | widget.show() | ||
epatters
|
r2841 | |||
# Start the application main loop. | ||||
epatters
|
r2758 | app.exec_() | ||
if __name__ == '__main__': | ||||
main() | ||||