##// END OF EJS Templates
Merge branch 'epatters-qtfrontend' into kernelmanager...
Merge branch 'epatters-qtfrontend' into kernelmanager Conflicts: IPython/frontend/qt/kernelmanager.py IPython/frontend/qt/util.py

File last commit:

r2716:57bf73a1
r2726:7ecaeab5 merge
Show More
ipython_widget.py
164 lines | 5.8 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
Added banners to FrontendWidget and IPythonWidget.
r2714 from IPython.core.usage import default_banner
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.
"""
epatters
* IPythonWidget now has IPython-style prompts that are futher stylabla via CSS...
r2715 # The default stylesheet for prompts, colors, etc.
default_stylesheet = """
epatters
* Moved AnsiCodeProcessor to separate file, refactored its API, and added unit tests....
r2716 .error { color: red; }
epatters
* IPythonWidget now has IPython-style prompts that are futher stylabla via CSS...
r2715 .in-prompt { color: navy; }
.in-prompt-number { font-weight: bold; }
.out-prompt { color: darkred; }
.out-prompt-number { font-weight: bold; }
"""
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627 #---------------------------------------------------------------------------
epatters
Minor comment cleanup.
r2669 # 'QObject' interface
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627 #---------------------------------------------------------------------------
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
epatters
* IPythonWidget now has IPython-style prompts that are futher stylabla via CSS...
r2715 # Initialize protected variables.
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627 self._magic_overrides = {}
epatters
* IPythonWidget now has IPython-style prompts that are futher stylabla via CSS...
r2715 self._prompt_count = 0
# Set a default stylesheet.
self.set_style_sheet(self.default_stylesheet)
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627
epatters
Minor comment cleanup.
r2669 #---------------------------------------------------------------------------
epatters
* Refactored ConsoleWidget execution API for greater flexibility and clarity....
r2688 # 'ConsoleWidget' abstract interface
epatters
Minor comment cleanup.
r2669 #---------------------------------------------------------------------------
epatters
* Refactored ConsoleWidget execution API for greater flexibility and clarity....
r2688 def _execute(self, source, hidden):
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627 """ 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)
epatters
* IPythonWidget now has IPython-style prompts that are futher stylabla via CSS...
r2715 self._show_interpreter_prompt()
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627 else:
epatters
* Refactored ConsoleWidget execution API for greater flexibility and clarity....
r2688 super(IPythonWidget, self)._execute(source, hidden)
#---------------------------------------------------------------------------
# 'FrontendWidget' interface
#---------------------------------------------------------------------------
def execute_file(self, path, hidden=False):
""" Reimplemented to use the 'run' magic.
"""
self.execute('run %s' % path, hidden=hidden)
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627
#---------------------------------------------------------------------------
epatters
Added banners to FrontendWidget and IPythonWidget.
r2714 # 'FrontendWidget' protected interface
#---------------------------------------------------------------------------
def _get_banner(self):
epatters
* IPythonWidget now has IPython-style prompts that are futher stylabla via CSS...
r2715 """ Reimplemented to return IPython's default banner.
epatters
Added banners to FrontendWidget and IPythonWidget.
r2714 """
return default_banner
epatters
* IPythonWidget now has IPython-style prompts that are futher stylabla via CSS...
r2715 def _show_interpreter_prompt(self):
""" Reimplemented for IPython-style prompts.
"""
self._prompt_count += 1
prompt_template = '<span class="in-prompt">%s</span>'
prompt_body = '<br/>In [<span class="in-prompt-number">%i</span>]: '
prompt = (prompt_template % prompt_body) % self._prompt_count
self._show_prompt(prompt, html=True)
# Update continuation prompt to reflect (possibly) new prompt length.
cont_prompt_chars = '...: '
space_count = len(self._prompt.lstrip()) - len(cont_prompt_chars)
cont_prompt_body = '&nbsp;' * space_count + cont_prompt_chars
self._continuation_prompt_html = prompt_template % cont_prompt_body
#------ Signal handlers ----------------------------------------------------
epatters
* Moved AnsiCodeProcessor to separate file, refactored its API, and added unit tests....
r2716 def _handle_execute_error(self, reply):
""" Reimplemented for IPython-style traceback formatting.
"""
content = reply['content']
traceback_lines = content['traceback'][:]
traceback = ''.join(traceback_lines)
traceback = traceback.replace(' ', '&nbsp;')
traceback = traceback.replace('\n', '<br/>')
ename = content['ename']
ename_styled = '<span class="error">%s</span>' % ename
traceback = traceback.replace(ename, ename_styled)
self.appendHtml(traceback)
epatters
* IPythonWidget now has IPython-style prompts that are futher stylabla via CSS...
r2715 def _handle_pyout(self, omsg):
""" Reimplemented for IPython-style "display hook".
"""
prompt_template = '<span class="out-prompt">%s</span>'
prompt_body = 'Out[<span class="out-prompt-number">%i</span>]: '
prompt = (prompt_template % prompt_body) % self._prompt_count
self.appendHtml(prompt)
self.appendPlainText(omsg['content']['data'] + '\n')
epatters
Added banners to FrontendWidget and IPythonWidget.
r2714 #---------------------------------------------------------------------------
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627 # '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
epatters
* IPythonWidget now has IPython-style prompts that are futher stylabla via CSS...
r2715 def set_style_sheet(self, stylesheet):
""" Sets the style sheet.
"""
self.document().setDefaultStyleSheet(stylesheet)
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627
if __name__ == '__main__':
epatters
* Implemented a proper main() function for kernel.py that reads command line input....
r2667 from IPython.frontend.qt.kernelmanager import QtKernelManager
epatters
* Added a function for spawning a localhost kernel in a new process on random ports....
r2641
epatters
* Added 'independent' argument to 'launch_kernel' for setting subprocess persistence goals. The case for 'independent=False' is only partially implemented....
r2700 # Don't let Qt or ZMQ swallow KeyboardInterupts.
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
epatters
* Implemented a proper main() function for kernel.py that reads command line input....
r2667 # Create a KernelManager.
kernel_manager = QtKernelManager()
kernel_manager.start_kernel()
Brian Granger
Kernel manager is cleaned up and simplified. Still bugs though.
r2699 kernel_manager.start_channels()
epatters
* Created an IPythonWidget subclass of FrontendWidget to contain IPython specific functionality....
r2627
epatters
* Implemented a proper main() function for kernel.py that reads command line input....
r2667 # Launch the application.
epatters
* Added 'independent' argument to 'launch_kernel' for setting subprocess persistence goals. The case for 'independent=False' is only partially implemented....
r2700 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_()