qtconsoleapp.py
368 lines
| 12.6 KiB
| text/x-python
|
PythonLexer
epatters
|
r2801 | """ A minimal application using the Qt console-style IPython frontend. | ||
MinRK
|
r4021 | |||
This is not a complete console app, as subprocess will not be able to receive | ||||
input, there is no real readline support, among other limitations. | ||||
Authors: | ||||
* Evan Patterson | ||||
* Min RK | ||||
* Erik Tollerud | ||||
* Fernando Perez | ||||
MinRK
|
r5136 | * Bussonnier Matthias | ||
* Thomas Kluyver | ||||
Paul Ivanov
|
r5603 | * Paul Ivanov | ||
MinRK
|
r4021 | |||
epatters
|
r2758 | """ | ||
epatters
|
r2961 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r3971 | # stdlib imports | ||
MinRK
|
r4986 | import json | ||
MinRK
|
r3971 | import os | ||
import signal | ||||
import sys | ||||
MinRK
|
r5073 | import uuid | ||
MinRK
|
r3971 | |||
Mark Wiebe
|
r6168 | # If run on Windows, install an exception hook which pops up a | ||
# message box. Pythonw.exe hides the console, so without this | ||||
# the application silently fails to load. | ||||
# | ||||
# We always install this handler, because the expectation is for | ||||
# qtconsole to bring up a GUI even if called from the console. | ||||
# The old handler is called, so the exception is printed as well. | ||||
# If desired, check for pythonw with an additional condition | ||||
# (sys.executable.lower().find('pythonw.exe') >= 0). | ||||
if os.name == 'nt': | ||||
old_excepthook = sys.excepthook | ||||
def gui_excepthook(exctype, value, tb): | ||||
try: | ||||
import ctypes, traceback | ||||
MB_ICONERROR = 0x00000010L | ||||
title = u'Error starting IPython QtConsole' | ||||
msg = u''.join(traceback.format_exception(exctype, value, tb)) | ||||
ctypes.windll.user32.MessageBoxW(0, msg, title, MB_ICONERROR) | ||||
finally: | ||||
# Also call the old exception hook to let it do | ||||
# its thing too. | ||||
old_excepthook(exctype, value, tb) | ||||
sys.excepthook = gui_excepthook | ||||
MinRK
|
r3971 | # System library imports | ||
MinRK
|
r5609 | from IPython.external.qt import QtCore, QtGui | ||
Evan Patterson
|
r3304 | |||
epatters
|
r2758 | # Local imports | ||
MinRK
|
r5214 | from IPython.config.application import boolean_flag, catch_config_error | ||
MinRK
|
r4024 | from IPython.core.application import BaseIPythonApplication | ||
from IPython.core.profiledir import ProfileDir | ||||
MinRK
|
r4972 | from IPython.lib.kernel import tunnel_to_kernel, find_connection_file | ||
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 | ||||
MinRK
|
r3170 | from IPython.frontend.qt.console import styles | ||
MinRK
|
r5136 | from IPython.frontend.qt.console.mainwindow import MainWindow | ||
epatters
|
r2758 | from IPython.frontend.qt.kernelmanager import QtKernelManager | ||
MinRK
|
r4958 | from IPython.utils.path import filefind | ||
MinRK
|
r4967 | from IPython.utils.py3compat import str_to_bytes | ||
MinRK
|
r3971 | from IPython.utils.traitlets import ( | ||
MinRK
|
r5344 | Dict, List, Unicode, Integer, CaselessStrEnum, CBool, Any | ||
MinRK
|
r3971 | ) | ||
Paul Ivanov
|
r5603 | from IPython.zmq.ipkernel import IPKernelApp | ||
MinRK
|
r4962 | from IPython.zmq.session import Session, default_secure | ||
MinRK
|
r3971 | from IPython.zmq.zmqshell import ZMQInteractiveShell | ||
Paul Ivanov
|
r5604 | |||
MinRK
|
r5618 | from IPython.frontend.consoleapp import ( | ||
IPythonConsoleApp, app_aliases, app_flags, flags, aliases | ||||
Paul Ivanov
|
r5603 | ) | ||
MinRK
|
r3971 | |||
epatters
|
r2961 | #----------------------------------------------------------------------------- | ||
MinRK
|
r3144 | # Network Constants | ||
epatters
|
r2961 | #----------------------------------------------------------------------------- | ||
MinRK
|
r3144 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS | ||
epatters
|
r2823 | |||
epatters
|
r2961 | #----------------------------------------------------------------------------- | ||
Brian Granger
|
r4216 | # Globals | ||
#----------------------------------------------------------------------------- | ||||
_examples = """ | ||||
ipython qtconsole # start the qtconsole | ||||
ipython qtconsole --pylab=inline # start with pylab in inline plotting mode | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r3971 | # Aliases and Flags | ||
epatters
|
r2961 | #----------------------------------------------------------------------------- | ||
epatters
|
r2758 | |||
MinRK
|
r5610 | # start with copy of flags | ||
flags = dict(flags) | ||||
MinRK
|
r4247 | qt_flags = { | ||
MinRK
|
r6806 | 'plain' : ({'IPythonQtConsoleApp' : {'plain' : True}}, | ||
MinRK
|
r3971 | "Disable rich text support."), | ||
MinRK
|
r4247 | } | ||
Matthias BUSSONNIER
|
r7389 | |||
MinRK
|
r5610 | # and app_flags from the Console Mixin | ||
qt_flags.update(app_flags) | ||||
# add frontend flags to the full set | ||||
MinRK
|
r4247 | flags.update(qt_flags) | ||
MinRK
|
r3971 | |||
MinRK
|
r5610 | # start with copy of front&backend aliases list | ||
aliases = dict(aliases) | ||||
MinRK
|
r4247 | qt_aliases = dict( | ||
MinRK
|
r3971 | style = 'IPythonWidget.syntax_style', | ||
stylesheet = 'IPythonQtConsoleApp.stylesheet', | ||||
colors = 'ZMQInteractiveShell.colors', | ||||
editor = 'IPythonWidget.editor', | ||||
MinRK
|
r4222 | paging = 'ConsoleWidget.paging', | ||
MinRK
|
r4247 | ) | ||
MinRK
|
r5610 | # and app_aliases from the Console Mixin | ||
qt_aliases.update(app_aliases) | ||||
Matthias BUSSONNIER
|
r7389 | qt_aliases.update({'gui-completion':'ConsoleWidget.gui_completion'}) | ||
MinRK
|
r5610 | # add frontend aliases to the full set | ||
MinRK
|
r4247 | aliases.update(qt_aliases) | ||
MinRK
|
r4222 | |||
MinRK
|
r5610 | # get flags&aliases into sets, and remove a couple that | ||
# shouldn't be scrubbed from backend flags: | ||||
qt_aliases = set(qt_aliases.keys()) | ||||
qt_aliases.remove('colors') | ||||
qt_flags = set(qt_flags.keys()) | ||||
MinRK
|
r5136 | #----------------------------------------------------------------------------- | ||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r3971 | |||
#----------------------------------------------------------------------------- | ||||
# IPythonQtConsole | ||||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r4215 | |||
MinRK
|
r5618 | class IPythonQtConsoleApp(BaseIPythonApplication, IPythonConsoleApp): | ||
MinRK
|
r3971 | name = 'ipython-qtconsole' | ||
Matthias BUSSONNIER
|
r5060 | |||
MinRK
|
r4021 | description = """ | ||
The IPython QtConsole. | ||||
This launches a Console-style application using Qt. It is not a full | ||||
MinRK
|
r4222 | console, in that launched terminal subprocesses will not be able to accept | ||
input. | ||||
The QtConsole supports various extra features beyond the Terminal IPython | ||||
shell, such as inline plotting with matplotlib, via: | ||||
ipython qtconsole --pylab=inline | ||||
MinRK
|
r4021 | |||
MinRK
|
r4222 | as well as saving your session as HTML, and printing the output. | ||
MinRK
|
r4021 | |||
""" | ||||
Brian Granger
|
r4216 | examples = _examples | ||
Brian Granger
|
r4215 | |||
MinRK
|
r7291 | classes = [IPythonWidget] + IPythonConsoleApp.classes | ||
MinRK
|
r3971 | flags = Dict(flags) | ||
aliases = Dict(aliases) | ||||
MinRK
|
r5620 | frontend_flags = Any(qt_flags) | ||
frontend_aliases = Any(qt_aliases) | ||||
Paul Ivanov
|
r5603 | kernel_manager_class = QtKernelManager | ||
MinRK
|
r3971 | |||
stylesheet = Unicode('', config=True, | ||||
help="path to a custom CSS stylesheet") | ||||
MinRK
|
r3983 | plain = CBool(False, config=True, | ||
MinRK
|
r3976 | help="Use a plaintext widget instead of rich text (plain can't print/save).") | ||
MinRK
|
r3971 | |||
MinRK
|
r6806 | def _plain_changed(self, name, old, new): | ||
kind = 'plain' if new else 'rich' | ||||
MinRK
|
r3971 | self.config.ConsoleWidget.kind = kind | ||
MinRK
|
r6806 | if new: | ||
MinRK
|
r3971 | self.widget_factory = IPythonWidget | ||
MinRK
|
r3173 | else: | ||
MinRK
|
r3971 | self.widget_factory = RichIPythonWidget | ||
# the factory for creating a widget | ||||
widget_factory = Any(RichIPythonWidget) | ||||
def parse_command_line(self, argv=None): | ||||
super(IPythonQtConsoleApp, self).parse_command_line(argv) | ||||
MinRK
|
r5620 | self.build_kernel_argv(argv) | ||
Paul Ivanov
|
r5604 | |||
MinRK
|
r3971 | |||
MinRK
|
r5135 | def new_frontend_master(self): | ||
""" Create and return new frontend attached to new kernel, launched on localhost. | ||||
Matthias BUSSONNIER
|
r5039 | """ | ||
MinRK
|
r5073 | ip = self.ip if self.ip in LOCAL_IPS else LOCALHOST | ||
MinRK
|
r5849 | kernel_manager = self.kernel_manager_class( | ||
MinRK
|
r5073 | ip=ip, | ||
connection_file=self._new_connection_file(), | ||||
config=self.config, | ||||
Matthias BUSSONNIER
|
r5035 | ) | ||
# start the kernel | ||||
MinRK
|
r6806 | kwargs = dict() | ||
Matthias BUSSONNIER
|
r5039 | kwargs['extra_arguments'] = self.kernel_argv | ||
kernel_manager.start_kernel(**kwargs) | ||||
Matthias BUSSONNIER
|
r5035 | kernel_manager.start_channels() | ||
widget = self.widget_factory(config=self.config, | ||||
MinRK
|
r5073 | local_kernel=True) | ||
MinRK
|
r6056 | self.init_colors(widget) | ||
Matthias BUSSONNIER
|
r5035 | widget.kernel_manager = kernel_manager | ||
MinRK
|
r5138 | widget._existing = False | ||
widget._may_close = True | ||||
widget._confirm_exit = self.confirm_exit | ||||
MinRK
|
r5135 | return widget | ||
def new_frontend_slave(self, current_widget): | ||||
"""Create and return a new frontend attached to an existing kernel. | ||||
Parameters | ||||
---------- | ||||
current_widget : IPythonWidget | ||||
The IPythonWidget whose kernel this frontend is to share | ||||
""" | ||||
MinRK
|
r5849 | kernel_manager = self.kernel_manager_class( | ||
MinRK
|
r5073 | connection_file=current_widget.kernel_manager.connection_file, | ||
config = self.config, | ||||
Matthias BUSSONNIER
|
r5035 | ) | ||
MinRK
|
r5073 | kernel_manager.load_connection_file() | ||
Matthias BUSSONNIER
|
r5035 | kernel_manager.start_channels() | ||
widget = self.widget_factory(config=self.config, | ||||
MinRK
|
r5073 | local_kernel=False) | ||
MinRK
|
r6056 | self.init_colors(widget) | ||
MinRK
|
r5138 | widget._existing = True | ||
widget._may_close = False | ||||
widget._confirm_exit = False | ||||
Matthias BUSSONNIER
|
r5035 | widget.kernel_manager = kernel_manager | ||
MinRK
|
r5135 | return widget | ||
MinRK
|
r3971 | |||
def init_qt_elements(self): | ||||
# Create the widget. | ||||
self.app = QtGui.QApplication([]) | ||||
Matthias BUSSONNIER
|
r5053 | |||
base_path = os.path.abspath(os.path.dirname(__file__)) | ||||
icon_path = os.path.join(base_path, 'resources', 'icon', 'IPythonConsole.svg') | ||||
Matthias BUSSONNIER
|
r5055 | self.app.icon = QtGui.QIcon(icon_path) | ||
QtGui.QApplication.setWindowIcon(self.app.icon) | ||||
Matthias BUSSONNIER
|
r5028 | |||
MinRK
|
r3971 | local_kernel = (not self.existing) or self.ip in LOCAL_IPS | ||
self.widget = self.widget_factory(config=self.config, | ||||
local_kernel=local_kernel) | ||||
MinRK
|
r6056 | self.init_colors(self.widget) | ||
MinRK
|
r5138 | self.widget._existing = self.existing | ||
self.widget._may_close = not self.existing | ||||
self.widget._confirm_exit = self.confirm_exit | ||||
Matthias BUSSONNIER
|
r5038 | |||
MinRK
|
r3971 | self.widget.kernel_manager = self.kernel_manager | ||
MinRK
|
r5138 | self.window = MainWindow(self.app, | ||
MinRK
|
r5135 | confirm_exit=self.confirm_exit, | ||
new_frontend_factory=self.new_frontend_master, | ||||
slave_frontend_factory=self.new_frontend_slave, | ||||
) | ||||
Matthias BUSSONNIER
|
r5038 | self.window.log = self.log | ||
Matthias BUSSONNIER
|
r5051 | self.window.add_tab_with_frontend(self.widget) | ||
Matthias BUSSONNIER
|
r5052 | self.window.init_menu_bar() | ||
Matthias BUSSONNIER
|
r5393 | |||
MinRK
|
r6806 | self.window.setWindowTitle('IPython') | ||
MinRK
|
r3971 | |||
MinRK
|
r6056 | def init_colors(self, widget): | ||
MinRK
|
r3971 | """Configure the coloring of the widget""" | ||
# Note: This will be dramatically simplified when colors | ||||
# are removed from the backend. | ||||
# parse the colors arg down to current known labels | ||||
try: | ||||
colors = self.config.ZMQInteractiveShell.colors | ||||
except AttributeError: | ||||
colors = None | ||||
try: | ||||
MinRK
|
r5127 | style = self.config.IPythonWidget.syntax_style | ||
MinRK
|
r3971 | except AttributeError: | ||
style = None | ||||
MinRK
|
r6056 | try: | ||
sheet = self.config.IPythonWidget.style_sheet | ||||
except AttributeError: | ||||
sheet = None | ||||
MinRK
|
r3971 | |||
# find the value for colors: | ||||
if colors: | ||||
colors=colors.lower() | ||||
if colors in ('lightbg', 'light'): | ||||
colors='lightbg' | ||||
elif colors in ('dark', 'linux'): | ||||
colors='linux' | ||||
else: | ||||
colors='nocolor' | ||||
elif style: | ||||
if style=='bw': | ||||
colors='nocolor' | ||||
elif styles.dark_style(style): | ||||
colors='linux' | ||||
else: | ||||
colors='lightbg' | ||||
MinRK
|
r3173 | else: | ||
MinRK
|
r3971 | colors=None | ||
MinRK
|
r6056 | # Configure the style | ||
MinRK
|
r3971 | if style: | ||
widget.style_sheet = styles.sheet_from_template(style, colors) | ||||
widget.syntax_style = style | ||||
MinRK
|
r3170 | widget._syntax_style_changed() | ||
widget._style_sheet_changed() | ||||
MinRK
|
r3171 | elif colors: | ||
MinRK
|
r6056 | # use a default dark/light/bw style | ||
MinRK
|
r3171 | widget.set_default_style(colors=colors) | ||
MinRK
|
r3170 | |||
MinRK
|
r3971 | if self.stylesheet: | ||
MinRK
|
r6056 | # we got an explicit stylesheet | ||
MinRK
|
r3971 | if os.path.isfile(self.stylesheet): | ||
with open(self.stylesheet) as f: | ||||
MinRK
|
r3170 | sheet = f.read() | ||
else: | ||||
MinRK
|
r6056 | raise IOError("Stylesheet %r not found." % self.stylesheet) | ||
if sheet: | ||||
widget.style_sheet = sheet | ||||
widget._style_sheet_changed() | ||||
MinRK
|
r3971 | |||
MinRK
|
r5609 | def init_signal(self): | ||
"""allow clean shutdown on sigint""" | ||||
signal.signal(signal.SIGINT, lambda sig, frame: self.exit(-2)) | ||||
# need a timer, so that QApplication doesn't block until a real | ||||
# Qt event fires (can require mouse movement) | ||||
# timer trick from http://stackoverflow.com/q/4938723/938949 | ||||
timer = QtCore.QTimer() | ||||
# Let the interpreter run each 200 ms: | ||||
timer.timeout.connect(lambda: None) | ||||
timer.start(200) | ||||
# hold onto ref, so the timer doesn't get cleaned up | ||||
self._sigint_timer = timer | ||||
MinRK
|
r5214 | @catch_config_error | ||
MinRK
|
r3971 | def initialize(self, argv=None): | ||
super(IPythonQtConsoleApp, self).initialize(argv) | ||||
MinRK
|
r5618 | IPythonConsoleApp.initialize(self,argv) | ||
MinRK
|
r3971 | self.init_qt_elements() | ||
MinRK
|
r5609 | self.init_signal() | ||
MinRK
|
r3971 | |||
def start(self): | ||||
# draw the window | ||||
self.window.show() | ||||
Matthias BUSSONNIER
|
r5712 | self.window.raise_() | ||
MinRK
|
r3170 | |||
MinRK
|
r3971 | # Start the application main loop. | ||
self.app.exec_() | ||||
epatters
|
r2841 | |||
MinRK
|
r3971 | #----------------------------------------------------------------------------- | ||
# Main entry point | ||||
#----------------------------------------------------------------------------- | ||||
def main(): | ||||
app = IPythonQtConsoleApp() | ||||
app.initialize() | ||||
app.start() | ||||
epatters
|
r2758 | |||
if __name__ == '__main__': | ||||
main() | ||||