##// END OF EJS Templates
Merge branch 'master' of git://github.com/ipython/ipython into qtfrontend and fix conflicts in setupbase.py....
Merge branch 'master' of git://github.com/ipython/ipython into qtfrontend and fix conflicts in setupbase.py. Conflicts: setupbase.py

File last commit:

r2643:422a62e7
r2664:6080a84e merge
Show More
ipython_widget.py
96 lines | 3.4 KiB | text/x-python | PythonLexer
epatters
* Updated FrontendWidget to use BlockBreaker for parsing input...
r2630 # System library imports
from PyQt4 import QtCore, QtGui
# Local imports
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627 from frontend_widget import FrontendWidget
class IPythonWidget(FrontendWidget):
""" A FrontendWidget for an IPython kernel.
"""
#---------------------------------------------------------------------------
# 'FrontendWidget' interface
#---------------------------------------------------------------------------
epatters
* Added 'started_listening' and 'stopped_listening' signals to QtKernelManager. The FrontendWidget listens for these signals....
r2643 def __init__(self, parent=None):
super(IPythonWidget, self).__init__(parent)
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627
self._magic_overrides = {}
def execute_source(self, source, hidden=False, interactive=False):
""" Reimplemented to override magic commands.
"""
magic_source = source.strip()
if magic_source.startswith('%'):
magic_source = magic_source[1:]
magic, sep, arguments = magic_source.partition(' ')
if not magic:
magic = magic_source
callback = self._magic_overrides.get(magic)
if callback:
output = callback(arguments)
if output:
self.appendPlainText(output)
self._show_prompt('>>> ')
return True
else:
return super(IPythonWidget, self).execute_source(source, hidden,
interactive)
#---------------------------------------------------------------------------
# 'IPythonWidget' interface
#---------------------------------------------------------------------------
def set_magic_override(self, magic, callback):
""" Overrides an IPython magic command. This magic will be intercepted
by the frontend rather than passed on to the kernel and 'callback'
will be called with a single argument: a string of argument(s) for
the magic. The callback can (optionally) return text to print to the
console.
"""
self._magic_overrides[magic] = callback
def remove_magic_override(self, magic):
""" Removes the override for the specified magic, if there is one.
"""
try:
del self._magic_overrides[magic]
except KeyError:
pass
if __name__ == '__main__':
epatters
* Added a function for spawning a localhost kernel in a new process on random ports....
r2641 from IPython.external.argparse import ArgumentParser
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627 from IPython.frontend.qt.kernelmanager import QtKernelManager
epatters
* Added a function for spawning a localhost kernel in a new process on random ports....
r2641 # Don't let Qt swallow KeyboardInterupts.
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
# Parse command line arguments.
parser = ArgumentParser()
parser.add_argument('--ip', type=str, default='127.0.0.1',
help='set the kernel\'s IP address [default localhost]')
parser.add_argument('--xreq', type=int, metavar='PORT', default=5575,
help='set the XREQ Channel port [default %(default)i]')
parser.add_argument('--sub', type=int, metavar='PORT', default=5576,
help='set the SUB Channel port [default %(default)i]')
namespace = parser.parse_args()
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627 # Create KernelManager
epatters
* Added a function for spawning a localhost kernel in a new process on random ports....
r2641 ip = namespace.ip
kernel_manager = QtKernelManager(xreq_address = (ip, namespace.xreq),
sub_address = (ip, namespace.sub))
kernel_manager.start_listening()
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627
# Launch application
epatters
* Added a function for spawning a localhost kernel in a new process on random ports....
r2641 app = QtGui.QApplication([])
epatters
* Added 'started_listening' and 'stopped_listening' signals to QtKernelManager. The FrontendWidget listens for these signals....
r2643 widget = IPythonWidget()
widget.kernel_manager = kernel_manager
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627 widget.setWindowTitle('Python')
widget.resize(640, 480)
widget.show()
epatters
* Added a function for spawning a localhost kernel in a new process on random ports....
r2641 app.exec_()
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627