ipython_widget.py
559 lines
| 23.3 KiB
| text/x-python
|
PythonLexer
|
r2850 | """ A FrontendWidget that emulates the interface of the console IPython and | ||
supports the additional functionality provided by the IPython kernel. | ||||
""" | ||||
|
r2916 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r2793 | # Standard library imports | ||
|
r2883 | from collections import namedtuple | ||
|
r3797 | import os.path | ||
|
r2920 | import re | ||
|
r2793 | from subprocess import Popen | ||
|
r3797 | import sys | ||
|
r4701 | import time | ||
|
r3036 | from textwrap import dedent | ||
|
r2793 | |||
|
r2630 | # System library imports | ||
|
r3304 | from IPython.external.qt import QtCore, QtGui | ||
|
r2630 | |||
# Local imports | ||||
|
r2971 | from IPython.core.inputsplitter import IPythonInputSplitter, \ | ||
transform_ipy_prompt | ||||
|
r4046 | from IPython.utils.traitlets import Bool, Unicode | ||
|
r2627 | from frontend_widget import FrontendWidget | ||
|
r3971 | import styles | ||
|
r2627 | |||
|
r2916 | #----------------------------------------------------------------------------- | ||
# Constants | ||||
#----------------------------------------------------------------------------- | ||||
|
r2926 | # Default strings to build and display input and output prompts (and separators | ||
# in between) | ||||
|
r2884 | default_in_prompt = 'In [<span class="in-prompt-number">%i</span>]: ' | ||
default_out_prompt = 'Out[<span class="out-prompt-number">%i</span>]: ' | ||||
|
r2926 | default_input_sep = '\n' | ||
default_output_sep = '' | ||||
default_output_sep2 = '' | ||||
|
r2884 | |||
|
r3041 | # Base path for most payload sources. | ||
zmq_shell_source = 'IPython.zmq.zmqshell.ZMQInteractiveShell' | ||||
|
r3977 | if sys.platform.startswith('win'): | ||
default_editor = 'notepad' | ||||
else: | ||||
default_editor = '' | ||||
|
r2916 | #----------------------------------------------------------------------------- | ||
# IPythonWidget class | ||||
#----------------------------------------------------------------------------- | ||||
|
r2627 | |||
class IPythonWidget(FrontendWidget): | ||||
""" A FrontendWidget for an IPython kernel. | ||||
""" | ||||
|
r2884 | # If set, the 'custom_edit_requested(str, int)' signal will be emitted when | ||
# an editor is needed for a file. This overrides 'editor' and 'editor_line' | ||||
# settings. | ||||
custom_edit = Bool(False) | ||||
|
r3304 | custom_edit_requested = QtCore.Signal(object, object) | ||
|
r2793 | |||
|
r3977 | editor = Unicode(default_editor, config=True, | ||
|
r3971 | help=""" | ||
A command for invoking a system text editor. If the string contains a | ||||
|
r4056 | {filename} format specifier, it will be used. Otherwise, the filename | ||
will be appended to the end the command. | ||||
|
r3971 | """) | ||
editor_line = Unicode(config=True, | ||||
help=""" | ||||
The editor command to use when a specific line number is requested. The | ||||
string should contain two format specifiers: {line} and {filename}. If | ||||
|
r4056 | this parameter is not specified, the line number option to the %edit | ||
magic will be ignored. | ||||
|
r3971 | """) | ||
style_sheet = Unicode(config=True, | ||||
help=""" | ||||
A CSS stylesheet. The stylesheet can contain classes for: | ||||
1. Qt: QPlainTextEdit, QFrame, QWidget, etc | ||||
2. Pygments: .c, .k, .o, etc. (see PygmentsHighlighter) | ||||
3. IPython: .error, .in-prompt, .out-prompt, etc | ||||
""") | ||||
|
r4872 | |||
|
r4046 | syntax_style = Unicode(config=True, | ||
|
r3971 | help=""" | ||
|
r4056 | If not empty, use this Pygments style for syntax highlighting. | ||
Otherwise, the style sheet is queried for Pygments style | ||||
information. | ||||
|
r3971 | """) | ||
|
r2733 | |||
|
r2884 | # Prompts. | ||
|
r4046 | in_prompt = Unicode(default_in_prompt, config=True) | ||
out_prompt = Unicode(default_out_prompt, config=True) | ||||
input_sep = Unicode(default_input_sep, config=True) | ||||
output_sep = Unicode(default_output_sep, config=True) | ||||
output_sep2 = Unicode(default_output_sep2, config=True) | ||||
|
r2883 | |||
|
r2835 | # FrontendWidget protected class variables. | ||
|
r2861 | _input_splitter_class = IPythonInputSplitter | ||
|
r5647 | _transform_prompt = staticmethod(transform_ipy_prompt) | ||
|
r2800 | |||
|
r2835 | # IPythonWidget protected class variables. | ||
|
r2884 | _PromptBlock = namedtuple('_PromptBlock', ['block', 'length', 'number']) | ||
|
r3041 | _payload_source_edit = zmq_shell_source + '.edit_magic' | ||
_payload_source_exit = zmq_shell_source + '.ask_exit' | ||||
|
r3864 | _payload_source_next_input = zmq_shell_source + '.set_next_input' | ||
|
r2835 | _payload_source_page = 'IPython.zmq.page.page' | ||
|
r4701 | _retrying_history_request = False | ||
|
r2835 | |||
|
r2627 | #--------------------------------------------------------------------------- | ||
|
r2736 | # 'object' interface | ||
|
r2627 | #--------------------------------------------------------------------------- | ||
|
r4872 | |||
|
r2736 | def __init__(self, *args, **kw): | ||
super(IPythonWidget, self).__init__(*args, **kw) | ||||
|
r2627 | |||
|
r2789 | # IPythonWidget protected variables. | ||
|
r4872 | self._payload_handlers = { | ||
|
r2950 | self._payload_source_edit : self._handle_payload_edit, | ||
self._payload_source_exit : self._handle_payload_exit, | ||||
|
r3036 | self._payload_source_page : self._handle_payload_page, | ||
|
r3864 | self._payload_source_next_input : self._handle_payload_next_input } | ||
|
r2961 | self._previous_prompt_obj = None | ||
|
r3183 | self._keep_kernel_on_exit = None | ||
|
r2715 | |||
|
r2884 | # Initialize widget styling. | ||
|
r2916 | if self.style_sheet: | ||
self._style_sheet_changed() | ||||
self._syntax_style_changed() | ||||
else: | ||||
self.set_default_style() | ||||
|
r2688 | |||
#--------------------------------------------------------------------------- | ||||
|
r2770 | # 'BaseFrontendMixin' abstract interface | ||
#--------------------------------------------------------------------------- | ||||
|
r2867 | def _handle_complete_reply(self, rep): | ||
""" Reimplemented to support IPython's improved completion machinery. | ||||
""" | ||||
|
r4793 | self.log.debug("complete: %s", rep.get('content', '')) | ||
|
r2867 | cursor = self._get_cursor() | ||
|
r2934 | info = self._request_info.get('complete') | ||
if info and info.id == rep['parent_header']['msg_id'] and \ | ||||
info.pos == cursor.position(): | ||||
|
r2920 | matches = rep['content']['matches'] | ||
|
r2867 | text = rep['content']['matched_text'] | ||
|
r2939 | offset = len(text) | ||
# Clean up matches with period and path separators if the matched | ||||
# text has not been transformed. This is done by truncating all | ||||
# but the last component and then suitably decreasing the offset | ||||
# between the current cursor position and the start of completion. | ||||
if len(matches) > 1 and matches[0][:offset] == text: | ||||
|
r4872 | parts = re.split(r'[./\\]', text) | ||
|
r2939 | sep_count = len(parts) - 1 | ||
if sep_count: | ||||
chop_length = sum(map(len, parts[:sep_count])) + sep_count | ||||
matches = [ match[chop_length:] for match in matches ] | ||||
offset -= chop_length | ||||
|
r2920 | |||
# Move the cursor to the start of the match and complete. | ||||
|
r2939 | cursor.movePosition(QtGui.QTextCursor.Left, n=offset) | ||
|
r2920 | self._complete_with_items(cursor, matches) | ||
|
r2867 | |||
|
r2934 | def _handle_execute_reply(self, msg): | ||
""" Reimplemented to support prompt requests. | ||||
""" | ||||
|
r5520 | msg_id = msg['parent_header'].get('msg_id') | ||
info = self._request_info['execute'].get(msg_id) | ||||
if info and info.kind == 'prompt': | ||||
number = msg['content']['execution_count'] + 1 | ||||
self._show_interpreter_prompt(number) | ||||
self._request_info['execute'].pop(msg_id) | ||||
else: | ||||
super(IPythonWidget, self)._handle_execute_reply(msg) | ||||
|
r2934 | |||
|
r3820 | def _handle_history_reply(self, msg): | ||
|
r3397 | """ Implemented to handle history tail replies, which are only supported | ||
by the IPython kernel. | ||||
|
r2844 | """ | ||
|
r4793 | self.log.debug("history: %s", msg.get('content', '')) | ||
|
r4700 | content = msg['content'] | ||
if 'history' not in content: | ||||
self.log.error("History request failed: %r"%content) | ||||
|
r4701 | if content.get('status', '') == 'aborted' and \ | ||
not self._retrying_history_request: | ||||
# a *different* action caused this request to be aborted, so | ||||
# we should try again. | ||||
self.log.error("Retrying aborted history request") | ||||
# prevent multiple retries of aborted requests: | ||||
self._retrying_history_request = True | ||||
# wait out the kernel's queue flush, which is currently timed at 0.1s | ||||
time.sleep(0.25) | ||||
self.kernel_manager.shell_channel.history(hist_access_type='tail',n=1000) | ||||
else: | ||||
self._retrying_history_request = False | ||||
|
r4700 | return | ||
|
r4701 | # reset retry flag | ||
self._retrying_history_request = False | ||||
|
r4700 | history_items = content['history'] | ||
|
r5310 | items = [] | ||
last_cell = u"" | ||||
for _, _, cell in history_items: | ||||
cell = cell.rstrip() | ||||
if cell != last_cell: | ||||
items.append(cell) | ||||
last_cell = cell | ||||
|
r2844 | self._set_history(items) | ||
|
r2806 | def _handle_pyout(self, msg): | ||
|
r2770 | """ Reimplemented for IPython-style "display hook". | ||
""" | ||||
|
r4793 | self.log.debug("pyout: %s", msg.get('content', '')) | ||
|
r2824 | if not self._hidden and self._is_from_this_session(msg): | ||
content = msg['content'] | ||||
|
r2926 | prompt_number = content['execution_count'] | ||
|
r3278 | data = content['data'] | ||
if data.has_key('text/html'): | ||||
|
r4056 | self._append_plain_text(self.output_sep, True) | ||
self._append_html(self._make_out_prompt(prompt_number), True) | ||||
|
r3278 | html = data['text/html'] | ||
|
r4056 | self._append_plain_text('\n', True) | ||
self._append_html(html + self.output_sep2, True) | ||||
|
r3278 | elif data.has_key('text/plain'): | ||
|
r4056 | self._append_plain_text(self.output_sep, True) | ||
self._append_html(self._make_out_prompt(prompt_number), True) | ||||
|
r3278 | text = data['text/plain'] | ||
|
r3932 | # If the repr is multiline, make sure we start on a new line, | ||
# so that its lines are aligned. | ||||
if "\n" in text and not self.output_sep.endswith("\n"): | ||||
|
r4056 | self._append_plain_text('\n', True) | ||
self._append_plain_text(text + self.output_sep2, True) | ||||
|
r2770 | |||
|
r3277 | def _handle_display_data(self, msg): | ||
""" The base handler for the ``display_data`` message. | ||||
""" | ||||
|
r4793 | self.log.debug("display: %s", msg.get('content', '')) | ||
|
r4872 | # For now, we don't display data from other frontends, but we | ||
|
r3277 | # eventually will as this allows all frontends to monitor the display | ||
# data. But we need to figure out how to handle this in the GUI. | ||||
if not self._hidden and self._is_from_this_session(msg): | ||||
source = msg['content']['source'] | ||||
data = msg['content']['data'] | ||||
metadata = msg['content']['metadata'] | ||||
# In the regular IPythonWidget, we simply print the plain text | ||||
# representation. | ||||
|
r3278 | if data.has_key('text/html'): | ||
html = data['text/html'] | ||||
|
r4057 | self._append_html(html, True) | ||
|
r3278 | elif data.has_key('text/plain'): | ||
text = data['text/plain'] | ||||
|
r4057 | self._append_plain_text(text, True) | ||
|
r3279 | # This newline seems to be needed for text and html output. | ||
|
r4057 | self._append_plain_text(u'\n', True) | ||
|
r3277 | |||
|
r2844 | def _started_channels(self): | ||
""" Reimplemented to make a history request. | ||||
""" | ||||
super(IPythonWidget, self)._started_channels() | ||||
|
r4056 | self.kernel_manager.shell_channel.history(hist_access_type='tail', | ||
n=1000) | ||||
|
r2770 | #--------------------------------------------------------------------------- | ||
|
r2971 | # 'ConsoleWidget' public interface | ||
#--------------------------------------------------------------------------- | ||||
#--------------------------------------------------------------------------- | ||||
# 'FrontendWidget' public interface | ||||
|
r2688 | #--------------------------------------------------------------------------- | ||
def execute_file(self, path, hidden=False): | ||||
""" Reimplemented to use the 'run' magic. | ||||
""" | ||||
|
r3797 | # Use forward slashes on Windows to avoid escaping each separator. | ||
if sys.platform == 'win32': | ||||
path = os.path.normpath(path).replace('\\', '/') | ||||
|
r4716 | # Perhaps we should not be using %run directly, but while we | ||
|
r5697 | # are, it is necessary to quote or escape filenames containing spaces | ||
|
r5699 | # or quotes. | ||
# In earlier code here, to minimize escaping, we sometimes quoted the | ||||
# filename with single quotes. But to do this, this code must be | ||||
# platform-aware, because run uses shlex rather than python string | ||||
# parsing, so that: | ||||
# * In Win: single quotes can be used in the filename without quoting, | ||||
# and we cannot use single quotes to quote the filename. | ||||
# * In *nix: we can escape double quotes in a double quoted filename, | ||||
# but can't escape single quotes in a single quoted filename. | ||||
# So to keep this code non-platform-specific and simple, we now only | ||||
# use double quotes to quote filenames, and escape when needed: | ||||
if ' ' in path or "'" in path or '"' in path: | ||||
|
r5698 | path = '"%s"' % path.replace('"', '\\"') | ||
|
r2838 | self.execute('%%run %s' % path, hidden=hidden) | ||
|
r2627 | |||
#--------------------------------------------------------------------------- | ||||
|
r2714 | # 'FrontendWidget' protected interface | ||
#--------------------------------------------------------------------------- | ||||
|
r2867 | def _complete(self): | ||
""" Reimplemented to support IPython's improved completion machinery. | ||||
""" | ||||
# We let the kernel split the input line, so we *always* send an empty | ||||
# text field. Readline-based frontends do get a real text field which | ||||
# they can use. | ||||
text = '' | ||||
|
r4872 | |||
|
r2867 | # Send the completion request to the kernel | ||
|
r3974 | msg_id = self.kernel_manager.shell_channel.complete( | ||
|
r2867 | text, # text | ||
self._get_input_buffer_cursor_line(), # line | ||||
self._get_input_buffer_cursor_column(), # cursor_pos | ||||
|
r4872 | self.input_buffer) # block | ||
|
r2934 | pos = self._get_cursor().position() | ||
info = self._CompletionRequest(msg_id, pos) | ||||
self._request_info['complete'] = info | ||||
|
r2867 | |||
|
r2770 | def _process_execute_error(self, msg): | ||
""" Reimplemented for IPython-style traceback formatting. | ||||
""" | ||||
content = msg['content'] | ||||
|
r2841 | traceback = '\n'.join(content['traceback']) + '\n' | ||
if False: | ||||
# FIXME: For now, tracebacks come as plain text, so we can't use | ||||
|
r2838 | # the html renderer yet. Once we refactor ultratb to produce | ||
# properly styled tracebacks, this branch should be the default | ||||
traceback = traceback.replace(' ', ' ') | ||||
traceback = traceback.replace('\n', '<br/>') | ||||
ename = content['ename'] | ||||
ename_styled = '<span class="error">%s</span>' % ename | ||||
traceback = traceback.replace(ename, ename_styled) | ||||
self._append_html(traceback) | ||||
else: | ||||
# This is the fallback for now, using plain text with ansi escapes | ||||
|
r4872 | self._append_plain_text(traceback) | ||
|
r2950 | |||
|
r2835 | def _process_execute_payload(self, item): | ||
|
r2961 | """ Reimplemented to dispatch payloads to handler methods. | ||
|
r2835 | """ | ||
|
r2950 | handler = self._payload_handlers.get(item['source']) | ||
if handler is None: | ||||
# We have no handler for this type of payload, simply ignore it | ||||
|
r2835 | return False | ||
|
r2950 | else: | ||
handler(item) | ||||
return True | ||||
|
r4872 | |||
|
r2926 | def _show_interpreter_prompt(self, number=None): | ||
|
r2715 | """ Reimplemented for IPython-style prompts. | ||
""" | ||||
|
r2844 | # If a number was not specified, make a prompt number request. | ||
|
r2797 | if number is None: | ||
|
r3974 | msg_id = self.kernel_manager.shell_channel.execute('', silent=True) | ||
|
r2934 | info = self._ExecutionRequest(msg_id, 'prompt') | ||
|
r5506 | self._request_info['execute'][msg_id] = info | ||
|
r2934 | return | ||
|
r2797 | |||
|
r2806 | # Show a new prompt and save information about it so that it can be | ||
# updated later if the prompt number turns out to be wrong. | ||||
|
r2926 | self._prompt_sep = self.input_sep | ||
|
r2797 | self._show_prompt(self._make_in_prompt(number), html=True) | ||
|
r2806 | block = self._control.document().lastBlock() | ||
length = len(self._prompt) | ||||
|
r2883 | self._previous_prompt_obj = self._PromptBlock(block, length, number) | ||
|
r2715 | |||
# Update continuation prompt to reflect (possibly) new prompt length. | ||||
|
r2733 | self._set_continuation_prompt( | ||
self._make_continuation_prompt(self._prompt), html=True) | ||||
|
r2715 | |||
|
r2797 | def _show_interpreter_prompt_for_reply(self, msg): | ||
""" Reimplemented for IPython-style prompts. | ||||
""" | ||||
# Update the old prompt number if necessary. | ||||
content = msg['content'] | ||||
|
r5188 | # abort replies do not have any keys: | ||
if content['status'] == 'aborted': | ||||
if self._previous_prompt_obj: | ||||
previous_prompt_number = self._previous_prompt_obj.number | ||||
else: | ||||
previous_prompt_number = 0 | ||||
else: | ||||
previous_prompt_number = content['execution_count'] | ||||
|
r2797 | if self._previous_prompt_obj and \ | ||
self._previous_prompt_obj.number != previous_prompt_number: | ||||
block = self._previous_prompt_obj.block | ||||
|
r2846 | |||
# Make sure the prompt block has not been erased. | ||||
|
r3307 | if block.isValid() and block.text(): | ||
|
r2800 | |||
# Remove the old prompt and insert a new prompt. | ||||
|
r2797 | cursor = QtGui.QTextCursor(block) | ||
cursor.movePosition(QtGui.QTextCursor.Right, | ||||
|
r4872 | QtGui.QTextCursor.KeepAnchor, | ||
|
r2797 | self._previous_prompt_obj.length) | ||
prompt = self._make_in_prompt(previous_prompt_number) | ||||
|
r2800 | self._prompt = self._insert_html_fetching_plain_text( | ||
cursor, prompt) | ||||
|
r2825 | # When the HTML is inserted, Qt blows away the syntax | ||
# highlighting for the line, so we need to rehighlight it. | ||||
|
r2800 | self._highlighter.rehighlightBlock(cursor.block()) | ||
|
r2797 | self._previous_prompt_obj = None | ||
# Show a new prompt with the kernel's estimated prompt number. | ||||
|
r3041 | self._show_interpreter_prompt(previous_prompt_number + 1) | ||
|
r2797 | |||
|
r2714 | #--------------------------------------------------------------------------- | ||
|
r2916 | # 'IPythonWidget' interface | ||
#--------------------------------------------------------------------------- | ||||
|
r3173 | def set_default_style(self, colors='lightbg'): | ||
|
r2916 | """ Sets the widget style to the class defaults. | ||
Parameters: | ||||
----------- | ||||
|
r3173 | colors : str, optional (default lightbg) | ||
|
r2916 | Whether to use the default IPython light background or dark | ||
|
r3171 | background or B&W style. | ||
|
r2916 | """ | ||
|
r3173 | colors = colors.lower() | ||
if colors=='lightbg': | ||||
|
r3971 | self.style_sheet = styles.default_light_style_sheet | ||
self.syntax_style = styles.default_light_syntax_style | ||||
|
r3173 | elif colors=='linux': | ||
|
r3971 | self.style_sheet = styles.default_dark_style_sheet | ||
self.syntax_style = styles.default_dark_syntax_style | ||||
|
r3173 | elif colors=='nocolor': | ||
|
r3971 | self.style_sheet = styles.default_bw_style_sheet | ||
self.syntax_style = styles.default_bw_syntax_style | ||||
|
r3173 | else: | ||
raise KeyError("No such color scheme: %s"%colors) | ||||
|
r2916 | |||
#--------------------------------------------------------------------------- | ||||
|
r2733 | # 'IPythonWidget' protected interface | ||
#--------------------------------------------------------------------------- | ||||
|
r2850 | def _edit(self, filename, line=None): | ||
""" Opens a Python script for editing. | ||||
Parameters: | ||||
----------- | ||||
filename : str | ||||
A path to a local system file. | ||||
line : int, optional | ||||
A line of interest in the file. | ||||
""" | ||||
|
r2884 | if self.custom_edit: | ||
|
r2850 | self.custom_edit_requested.emit(filename, line) | ||
|
r3977 | elif not self.editor: | ||
self._append_plain_text('No default editor available.\n' | ||||
|
r4056 | 'Specify a GUI text editor in the `IPythonWidget.editor` ' | ||
'configurable to enable the %edit magic') | ||||
|
r2850 | else: | ||
try: | ||||
filename = '"%s"' % filename | ||||
|
r2884 | if line and self.editor_line: | ||
command = self.editor_line.format(filename=filename, | ||||
line=line) | ||||
|
r2850 | else: | ||
try: | ||||
|
r2884 | command = self.editor.format() | ||
|
r2850 | except KeyError: | ||
|
r2884 | command = self.editor.format(filename=filename) | ||
|
r2850 | else: | ||
command += ' ' + filename | ||||
except KeyError: | ||||
self._append_plain_text('Invalid editor command.\n') | ||||
else: | ||||
try: | ||||
Popen(command, shell=True) | ||||
except OSError: | ||||
msg = 'Opening editor with command "%s" failed.\n' | ||||
self._append_plain_text(msg % command) | ||||
|
r2733 | def _make_in_prompt(self, number): | ||
""" Given a prompt number, returns an HTML In prompt. | ||||
""" | ||||
|
r4478 | try: | ||
body = self.in_prompt % number | ||||
except TypeError: | ||||
# allow in_prompt to leave out number, e.g. '>>> ' | ||||
body = self.in_prompt | ||||
|
r2733 | return '<span class="in-prompt">%s</span>' % body | ||
def _make_continuation_prompt(self, prompt): | ||||
""" Given a plain text version of an In prompt, returns an HTML | ||||
continuation prompt. | ||||
""" | ||||
end_chars = '...: ' | ||||
space_count = len(prompt.lstrip('\n')) - len(end_chars) | ||||
body = ' ' * space_count + end_chars | ||||
return '<span class="in-prompt">%s</span>' % body | ||||
|
r4872 | |||
|
r2733 | def _make_out_prompt(self, number): | ||
""" Given a prompt number, returns an HTML Out prompt. | ||||
""" | ||||
body = self.out_prompt % number | ||||
return '<span class="out-prompt">%s</span>' % body | ||||
|
r2884 | |||
|
r2961 | #------ Payload handlers -------------------------------------------------- | ||
# Payload handlers with a generic interface: each takes the opaque payload | ||||
# dict, unpacks it and calls the underlying functions with the necessary | ||||
# arguments. | ||||
def _handle_payload_edit(self, item): | ||||
self._edit(item['filename'], item['line_number']) | ||||
def _handle_payload_exit(self, item): | ||||
|
r3183 | self._keep_kernel_on_exit = item['keepkernel'] | ||
|
r5035 | self.exit_requested.emit(self) | ||
|
r2961 | |||
|
r3864 | def _handle_payload_next_input(self, item): | ||
|
r3931 | self.input_buffer = dedent(item['text'].rstrip()) | ||
|
r3041 | |||
|
r2961 | def _handle_payload_page(self, item): | ||
|
r3014 | # Since the plain text widget supports only a very small subset of HTML | ||
# and we have no control over the HTML source, we only page HTML | ||||
# payloads in the rich text widget. | ||||
if item['html'] and self.kind == 'rich': | ||||
self._page(item['html'], html=True) | ||||
else: | ||||
self._page(item['text'], html=False) | ||||
|
r2961 | |||
|
r3277 | #------ Trait change handlers -------------------------------------------- | ||
|
r2884 | |||
def _style_sheet_changed(self): | ||||
""" Set the style sheets of the underlying widgets. | ||||
""" | ||||
self.setStyleSheet(self.style_sheet) | ||||
self._control.document().setDefaultStyleSheet(self.style_sheet) | ||||
if self._page_control: | ||||
self._page_control.document().setDefaultStyleSheet(self.style_sheet) | ||||
|
r3305 | bg_color = self._control.palette().window().color() | ||
|
r2884 | self._ansi_processor.set_background_color(bg_color) | ||
|
r3971 | |||
|
r2884 | def _syntax_style_changed(self): | ||
""" Set the style for the syntax highlighter. | ||||
""" | ||||
|
r3971 | if self._highlighter is None: | ||
# ignore premature calls | ||||
return | ||||
|
r2884 | if self.syntax_style: | ||
self._highlighter.set_style(self.syntax_style) | ||||
else: | ||||
self._highlighter.set_style_sheet(self.style_sheet) | ||||
|
r3277 | |||
|
r4689 | #------ Trait default initializers ----------------------------------------- | ||
def _banner_default(self): | ||||
from IPython.core.usage import default_gui_banner | ||||
return default_gui_banner | ||||