##// END OF EJS Templates
ENH: Extend the DisplayHook.compute_result_repr() and write_result_repr() methods to produce and consume the lists of extra formats. Use this capability in the ZMQ shell. Document the extension to the messaging format.
ENH: Extend the DisplayHook.compute_result_repr() and write_result_repr() methods to produce and consume the lists of extra formats. Use this capability in the ZMQ shell. Document the extension to the messaging format.

File last commit:

r3190:1d516fb1
r3215:668ed7c8
Show More
ipythonqt.py
273 lines | 11.7 KiB | text/x-python | PythonLexer
epatters
Created a proper IPython script from the console frontend demo.
r2801 """ A minimal application using the Qt console-style IPython frontend.
epatters
* The SVG payload matplotlib backend now works....
r2758 """
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 #-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
epatters
* The SVG payload matplotlib backend now works....
r2758 # Systemm library imports
Brian Granger
Merge branch 'newkernel' into upstream-newkernel...
r2873 from PyQt4 import QtGui
MinRK
exposed pygments styles as ipythonqt options
r3170 from pygments.styles import get_all_styles
epatters
* The SVG payload matplotlib backend now works....
r2758 # Local imports
from IPython.external.argparse import ArgumentParser
epatters
Created a proper IPython script from the console frontend demo.
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
MinRK
exposed pygments styles as ipythonqt options
r3170 from IPython.frontend.qt.console import styles
epatters
* The SVG payload matplotlib backend now works....
r2758 from IPython.frontend.qt.kernelmanager import QtKernelManager
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 #-----------------------------------------------------------------------------
MinRK
Possible fix for GH-169
r3144 # Network Constants
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 #-----------------------------------------------------------------------------
MinRK
Possible fix for GH-169
r3144 from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS
epatters
Added arguments to the Qt console frontend script for connecting to an existing kernel and for specifying an IP and specific ports.
r2823
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 #-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
class MainWindow(QtGui.QMainWindow):
#---------------------------------------------------------------------------
# 'object' interface
#---------------------------------------------------------------------------
MinRK
prevent remote frontends from closing the kernel
r3142 def __init__(self, app, frontend, existing=False, may_close=True):
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 """ Create a MainWindow for the specified FrontendWidget.
MinRK
review fixes
r3100
MinRK
comment/doc logic cleanup
r3105 The app is passed as an argument to allow for different
closing behavior depending on whether we are the Kernel's parent.
MinRK
prevent remote frontends from closing the kernel
r3142 If existing is True, then this Console does not own the Kernel.
If may_close is True, then this Console is permitted to close the kernel
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 """
super(MainWindow, self).__init__()
MinRK
changed close Console behavior to leave Kernel alive
r3104 self._app = app
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 self._frontend = frontend
MinRK
added shutdown notification handling to ipythonqt
r3090 self._existing = existing
MinRK
Possible fix for GH-169
r3144 if existing:
MinRK
prevent remote frontends from closing the kernel
r3142 self._may_close = may_close
else:
self._may_close = True
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 self._frontend.exit_requested.connect(self.close)
self.setCentralWidget(frontend)
#---------------------------------------------------------------------------
# QWidget interface
#---------------------------------------------------------------------------
def closeEvent(self, event):
Erik Tollerud
docstring fix in CloseEvent to conform to standards
r3190 """ Close the window and the kernel (if necessary).
This will prompt the user if they are finished with the kernel, and if
so, closes the kernel cleanly. Alternatively, if the exit magic is used,
it closes without prompt.
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 """
Erik Tollerud
__builtin__.exit and quit are now hidden - exit magic is now the only exit command
r3185 keepkernel = None #Use the prompt by default
if hasattr(self._frontend,'_keep_kernel_on_exit'): #set by exit magic
keepkernel = self._frontend._keep_kernel_on_exit
epatters
Fixed the segfaults on application exit. The BracketMatcher, CallTipWidget, and CompletionWidget were using the text control as their parents. This should not be a problem, but for some reason it resulted in problems during shutdown. I suspect that PyQt is bugged and was deleting the C++ objects a second time in the garbage collection phase after they had already been deleted automatically by the C++ layer of Qt.
r2982 kernel_manager = self._frontend.kernel_manager
Erik Tollerud
Expanded %exit magic in qtconsole to not exit without prompting and...
r3183
Erik Tollerud
__builtin__.exit and quit are now hidden - exit magic is now the only exit command
r3185 if keepkernel is None: #show prompt
Erik Tollerud
Expanded %exit magic in qtconsole to not exit without prompting and...
r3183 if kernel_manager and kernel_manager.channels_running:
title = self.window().windowTitle()
cancel = QtGui.QMessageBox.Cancel
okay = QtGui.QMessageBox.Ok
if self._may_close:
msg = "You are closing this Console window."
info = "Would you like to quit the Kernel and all attached Consoles as well?"
justthis = QtGui.QPushButton("&No, just this Console", self)
justthis.setShortcut('N')
closeall = QtGui.QPushButton("&Yes, quit everything", self)
closeall.setShortcut('Y')
box = QtGui.QMessageBox(QtGui.QMessageBox.Question, title, msg)
box.setInformativeText(info)
box.addButton(cancel)
box.addButton(justthis, QtGui.QMessageBox.NoRole)
box.addButton(closeall, QtGui.QMessageBox.YesRole)
box.setDefaultButton(closeall)
box.setEscapeButton(cancel)
reply = box.exec_()
if reply == 1: # close All
kernel_manager.shutdown_kernel()
#kernel_manager.stop_channels()
event.accept()
elif reply == 0: # close Console
if not self._existing:
# Have kernel: don't quit, just close the window
self._app.setQuitOnLastWindowClosed(False)
self.deleteLater()
event.accept()
else:
event.ignore()
MinRK
prevent remote frontends from closing the kernel
r3142 else:
Erik Tollerud
Expanded %exit magic in qtconsole to not exit without prompting and...
r3183 reply = QtGui.QMessageBox.question(self, title,
"Are you sure you want to close this Console?"+
"\nThe Kernel and other Consoles will remain active.",
okay|cancel,
defaultButton=okay
)
if reply == okay:
event.accept()
else:
event.ignore()
Erik Tollerud
__builtin__.exit and quit are now hidden - exit magic is now the only exit command
r3185 elif keepkernel: #close console but leave kernel running (no prompt)
Erik Tollerud
Expanded %exit magic in qtconsole to not exit without prompting and...
r3183 if kernel_manager and kernel_manager.channels_running:
if not self._existing:
# I have the kernel: don't quit, just close the window
self._app.setQuitOnLastWindowClosed(False)
event.accept()
Erik Tollerud
__builtin__.exit and quit are now hidden - exit magic is now the only exit command
r3185 else: #close console and kernel (no prompt)
Erik Tollerud
Expanded %exit magic in qtconsole to not exit without prompting and...
r3183 if kernel_manager and kernel_manager.channels_running:
kernel_manager.shutdown_kernel()
event.accept()
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961
#-----------------------------------------------------------------------------
# Main entry point
#-----------------------------------------------------------------------------
epatters
* The SVG payload matplotlib backend now works....
r2758
def main():
epatters
Created a proper IPython script from the console frontend demo.
r2801 """ Entry point for application.
epatters
* The SVG payload matplotlib backend now works....
r2758 """
# Parse command line arguments.
parser = ArgumentParser()
epatters
Added option to ipythonqt.py for setting the paging style and grouped the options into kernel settings/widget settings.
r2871 kgroup = parser.add_argument_group('kernel options')
kgroup.add_argument('-e', '--existing', action='store_true',
epatters
Added arguments to the Qt console frontend script for connecting to an existing kernel and for specifying an IP and specific ports.
r2823 help='connect to an existing kernel')
epatters
Added option to ipythonqt.py for setting the paging style and grouped the options into kernel settings/widget settings.
r2871 kgroup.add_argument('--ip', type=str, default=LOCALHOST,
MinRK
added more info to --ip in ipython-qtconsole -h
r3152 help=\
"set the kernel\'s IP address [default localhost].\
If the IP address is something other than localhost, then \
Consoles on other machines will be able to connect\
to the Kernel, so be careful!")
epatters
Added option to ipythonqt.py for setting the paging style and grouped the options into kernel settings/widget settings.
r2871 kgroup.add_argument('--xreq', type=int, metavar='PORT', default=0,
epatters
Added arguments to the Qt console frontend script for connecting to an existing kernel and for specifying an IP and specific ports.
r2823 help='set the XREQ channel port [default random]')
epatters
Added option to ipythonqt.py for setting the paging style and grouped the options into kernel settings/widget settings.
r2871 kgroup.add_argument('--sub', type=int, metavar='PORT', default=0,
epatters
Added arguments to the Qt console frontend script for connecting to an existing kernel and for specifying an IP and specific ports.
r2823 help='set the SUB channel port [default random]')
epatters
Added option to ipythonqt.py for setting the paging style and grouped the options into kernel settings/widget settings.
r2871 kgroup.add_argument('--rep', type=int, metavar='PORT', default=0,
epatters
Added arguments to the Qt console frontend script for connecting to an existing kernel and for specifying an IP and specific ports.
r2823 help='set the REP channel port [default random]')
Brian Granger
Added heartbeat support.
r2910 kgroup.add_argument('--hb', type=int, metavar='PORT', default=0,
MinRK
exposed pygments styles as ipythonqt options
r3170 help='set the heartbeat port [default random]')
epatters
* Fixed regression from last commit: syntax highlighting works robustly again....
r2866
epatters
Added option to ipythonqt.py for setting the paging style and grouped the options into kernel settings/widget settings.
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
Merge branch 'newkernel' into upstream-newkernel...
r2873 egroup.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
Brian Granger
GUI support for wx, qt and tk.
r2872 const='auto', help = \
epatters
* Added support for Ctrl-L per Fernando's request....
r2880 "Pre-load matplotlib and numpy for interactive use. If GUI is not \
given, the GUI backend is matplotlib's, otherwise use one of: \
Fernando Perez
Renamed 'payload-svg' matplotlib backend to 'inline'.
r2991 ['tk', 'gtk', 'qt', 'wx', 'inline'].")
epatters
Added option to ipythonqt.py for setting the paging style and grouped the options into kernel settings/widget settings.
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')
epatters
Changed the default completion style to text. Text completion now uses the pager.
r2917 wgroup.add_argument('--gui-completion', action='store_true',
help='use a GUI widget for tab completion')
MinRK
exposed pygments styles as ipythonqt options
r3170 wgroup.add_argument('--style', type=str,
MinRK
added --colors flag to ipythonqt
r3171 choices = list(get_all_styles()),
help='specify a pygments style for by name.')
MinRK
exposed pygments styles as ipythonqt options
r3170 wgroup.add_argument('--stylesheet', type=str,
help="path to a custom CSS stylesheet.")
MinRK
added --colors flag to ipythonqt
r3171 wgroup.add_argument('--colors', type=str,
MinRK
color settings from ipythonqt propagate down to the ZMQInteractiveShell in the Kernel
r3173 help="Set the color scheme (LightBG,Linux,NoColor). This is guessed\
MinRK
added --colors flag to ipythonqt
r3171 based on the pygments style if not set.")
Brian Granger
Merge branch 'newkernel' into upstream-newkernel...
r2873
epatters
Added arguments to the Qt console frontend script for connecting to an existing kernel and for specifying an IP and specific ports.
r2823 args = parser.parse_args()
Brian Granger
Merge branch 'newkernel' into upstream-newkernel...
r2873
MinRK
color settings from ipythonqt propagate down to the ZMQInteractiveShell in the Kernel
r3173 # parse the colors arg down to current known labels
if args.colors:
colors=args.colors.lower()
if colors in ('lightbg', 'light'):
colors='lightbg'
elif colors in ('dark', 'linux'):
colors='linux'
else:
colors='nocolor'
elif args.style:
if args.style=='bw':
colors='nocolor'
elif styles.dark_style(args.style):
colors='linux'
else:
colors='lightbg'
else:
colors=None
epatters
* The SVG payload matplotlib backend now works....
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
Added arguments to the Qt console frontend script for connecting to an existing kernel and for specifying an IP and specific ports.
r2823 kernel_manager = QtKernelManager(xreq_address=(args.ip, args.xreq),
sub_address=(args.ip, args.sub),
Brian Granger
Added heartbeat support.
r2910 rep_address=(args.ip, args.rep),
hb_address=(args.ip, args.hb))
MinRK
prevent remote frontends from closing the kernel
r3142 if not args.existing:
MinRK
Possible fix for GH-169
r3144 # if not args.ip in LOCAL_IPS+ALL_ALIAS:
# raise ValueError("Must bind a local ip, such as: %s"%LOCAL_IPS)
MinRK
color settings from ipythonqt propagate down to the ZMQInteractiveShell in the Kernel
r3173
MinRK
Possible fix for GH-169
r3144 kwargs = dict(ip=args.ip)
epatters
Added arguments to the Qt console frontend script for connecting to an existing kernel and for specifying an IP and specific ports.
r2823 if args.pure:
MinRK
Possible fix for GH-169
r3144 kwargs['ipython']=False
MinRK
color settings from ipythonqt propagate down to the ZMQInteractiveShell in the Kernel
r3173 else:
kwargs['colors']=colors
if args.pylab:
kwargs['pylab']=args.pylab
MinRK
Possible fix for GH-169
r3144 kernel_manager.start_kernel(**kwargs)
epatters
* The SVG payload matplotlib backend now works....
r2758 kernel_manager.start_channels()
MinRK
local_kernel based on LOCAL_IPS instead of LOCALHOST
r3145 local_kernel = (not args.existing) or args.ip in LOCAL_IPS
epatters
* Tab completion now uses the correct cursor position....
r2841 # Create the widget.
epatters
* The SVG payload matplotlib backend now works....
r2758 app = QtGui.QApplication([])
epatters
Added arguments to the Qt console frontend script for connecting to an existing kernel and for specifying an IP and specific ports.
r2823 if args.pure:
kind = 'rich' if args.rich else 'plain'
MinRK
tweaked close dialog and added prompts to prevent silent remote close
r3129 widget = FrontendWidget(kind=kind, paging=args.paging, local_kernel=local_kernel)
Fernando Perez
Add support for simultaneous interactive and inline matplotlib plots....
r2987 elif args.rich or args.pylab:
MinRK
tweaked close dialog and added prompts to prevent silent remote close
r3129 widget = RichIPythonWidget(paging=args.paging, local_kernel=local_kernel)
epatters
* The SVG payload matplotlib backend now works....
r2758 else:
MinRK
tweaked close dialog and added prompts to prevent silent remote close
r3129 widget = IPythonWidget(paging=args.paging, local_kernel=local_kernel)
epatters
Changed the default completion style to text. Text completion now uses the pager.
r2917 widget.gui_completion = args.gui_completion
epatters
* The SVG payload matplotlib backend now works....
r2758 widget.kernel_manager = kernel_manager
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961
MinRK
exposed pygments styles as ipythonqt options
r3170 # configure the style:
if not args.pure: # only IPythonWidget supports styles
if args.style:
widget.syntax_style = args.style
MinRK
color settings from ipythonqt propagate down to the ZMQInteractiveShell in the Kernel
r3173 widget.style_sheet = styles.sheet_from_template(args.style, colors)
MinRK
exposed pygments styles as ipythonqt options
r3170 widget._syntax_style_changed()
widget._style_sheet_changed()
MinRK
added --colors flag to ipythonqt
r3171 elif colors:
# use a default style
widget.set_default_style(colors=colors)
MinRK
exposed pygments styles as ipythonqt options
r3170 else:
# this is redundant for now, but allows the widget's
# defaults to change
MinRK
added --colors flag to ipythonqt
r3171 widget.set_default_style()
MinRK
exposed pygments styles as ipythonqt options
r3170
if args.stylesheet:
# we got an expicit stylesheet
if os.path.isfile(args.stylesheet):
with open(args.stylesheet) as f:
sheet = f.read()
widget.style_sheet = sheet
widget._style_sheet_changed()
else:
raise IOError("Stylesheet %r not found."%args.stylesheet)
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 # Create the main window.
MinRK
prevent remote frontends from closing the kernel
r3142 window = MainWindow(app, widget, args.existing, may_close=local_kernel)
epatters
* Moved shutdown_kernel method from FrontendWidget to KernelManager....
r2961 window.setWindowTitle('Python' if args.pure else 'IPython')
window.show()
epatters
* Tab completion now uses the correct cursor position....
r2841
# Start the application main loop.
epatters
* The SVG payload matplotlib backend now works....
r2758 app.exec_()
if __name__ == '__main__':
main()