From 095e73ab7386af077968a3bb6c28e5d88d9abe60 2014-05-07 05:41:23 From: MinRK Date: 2014-05-07 05:41:23 Subject: [PATCH] pager payload is a mime-bundle --- diff --git a/IPython/core/page.py b/IPython/core/page.py index 23fedb5..c39569e 100644 --- a/IPython/core/page.py +++ b/IPython/core/page.py @@ -152,6 +152,10 @@ def page(strng, start=0, screen_lines=0, pager_cmd=None): If no system pager works, the string is sent through a 'dumb pager' written in python, very simplistic. """ + + # for compatibility with mime-bundle form: + if isinstance(strng, dict): + strng = strng['text/plain'] # Some routines may auto-compute start offsets incorrectly and pass a # negative value. Offset to 0 for robustness. diff --git a/IPython/core/payloadpage.py b/IPython/core/payloadpage.py index b1d07a6..682fd9b 100644 --- a/IPython/core/payloadpage.py +++ b/IPython/core/payloadpage.py @@ -1,41 +1,16 @@ # encoding: utf-8 -""" -A payload based version of page. +"""A payload based version of page.""" -Authors: +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. -* Brian Granger -* Fernando Perez -""" - -#----------------------------------------------------------------------------- -# Copyright (C) 2008-2011 The IPython Development Team -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- - -# Third-party -try: - from docutils.core import publish_string -except ImportError: - # html paging won't be available, but we don't raise any errors. It's a - # purely optional feature. - pass - -# Our own -from IPython.core.interactiveshell import InteractiveShell +from IPython.core.getipython import get_ipython #----------------------------------------------------------------------------- # Classes and functions #----------------------------------------------------------------------------- -def page(strng, start=0, screen_lines=0, pager_cmd=None, - html=None, auto_html=False): +def page(strng, start=0, screen_lines=0, pager_cmd=None): """Print a string, piping through a pager. This version ignores the screen_lines and pager_cmd arguments and uses @@ -43,49 +18,28 @@ def page(strng, start=0, screen_lines=0, pager_cmd=None, Parameters ---------- - strng : str - Text to page. + strng : str or mime-dict + Text to page, or a mime-type keyed dict of already formatted data. start : int Starting line at which to place the display. - - html : str, optional - If given, an html string to send as well. - - auto_html : bool, optional - If true, the input string is assumed to be valid reStructuredText and is - converted to HTML with docutils. Note that if docutils is not found, - this option is silently ignored. - - Notes - ----- - - Only one of the ``html`` and ``auto_html`` options can be given, not - both. """ # Some routines may auto-compute start offsets incorrectly and pass a # negative value. Offset to 0 for robustness. start = max(0, start) - shell = InteractiveShell.instance() - - if auto_html: - try: - # These defaults ensure user configuration variables for docutils - # are not loaded, only our config is used here. - defaults = {'file_insertion_enabled': 0, - 'raw_enabled': 0, - '_disable_config': 1} - html = publish_string(strng, writer_name='html', - settings_overrides=defaults) - except: - pass - + shell = get_ipython() + + if isinstance(strng, dict): + data = strng + else: + data = {'text/plain' : strng} payload = dict( source='page', + data=data, text=strng, - html=html, - start_line_number=start + start=start, + screen_lines=screen_lines, ) shell.payload_manager.write_payload(payload) diff --git a/IPython/html/static/notebook/js/pager.js b/IPython/html/static/notebook/js/pager.js index aac9bfb..dd4ead8 100644 --- a/IPython/html/static/notebook/js/pager.js +++ b/IPython/html/static/notebook/js/pager.js @@ -1,9 +1,5 @@ -//---------------------------------------------------------------------------- -// Copyright (C) 2008-2011 The IPython Development Team -// -// Distributed under the terms of the BSD License. The full license is in -// the file COPYING, distributed as part of this software. -//---------------------------------------------------------------------------- +// Copyright (c) IPython Development Team. +// Distributed under the terms of the Modified BSD License. //============================================================================ // Pager @@ -81,12 +77,18 @@ var IPython = (function (IPython) { var that = this; this.pager_element.bind('collapse_pager', function (event, extrap) { - var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; + var time = 'fast'; + if (extrap && extrap.duration) { + time = extrap.duration; + } that.pager_element.hide(time); }); this.pager_element.bind('expand_pager', function (event, extrap) { - var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast'; + var time = 'fast'; + if (extrap && extrap.duration) { + time = extrap.duration; + } that.pager_element.show(time); }); @@ -103,12 +105,13 @@ var IPython = (function (IPython) { that.toggle(); }); - $([IPython.events]).on('open_with_text.Pager', function (event, data) { - if (data.text.trim() !== '') { + $([IPython.events]).on('open_with_text.Pager', function (event, payload) { + // FIXME: support other mime types + if (payload.data['text/plain'] && payload.data['text/plain'] !== "") { that.clear(); that.expand(); - that.append_text(data.text); - }; + that.append_text(payload.data['text/plain']); + } }); }; @@ -117,7 +120,7 @@ var IPython = (function (IPython) { if (this.expanded === true) { this.expanded = false; this.pager_element.add($('div#notebook')).trigger('collapse_pager', extrap); - }; + } }; @@ -125,7 +128,7 @@ var IPython = (function (IPython) { if (this.expanded !== true) { this.expanded = true; this.pager_element.add($('div#notebook')).trigger('expand_pager', extrap); - }; + } }; @@ -134,7 +137,7 @@ var IPython = (function (IPython) { this.collapse(); } else { this.expand(); - }; + } }; @@ -160,8 +163,7 @@ var IPython = (function (IPython) { pager_body.append(this.pager_element.clone().children()); w.document.close(); this.collapse(); - - } + }; Pager.prototype.append_text = function (text) { // The only user content injected with this HTML call is escaped by diff --git a/IPython/qt/console/ipython_widget.py b/IPython/qt/console/ipython_widget.py index 5913dce..1f51dbc 100644 --- a/IPython/qt/console/ipython_widget.py +++ b/IPython/qt/console/ipython_widget.py @@ -532,10 +532,11 @@ class IPythonWidget(FrontendWidget): # 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) + data = item['data'] + if 'text/html' in data and self.kind == 'rich': + self._page(data['text/html'], html=True) else: - self._page(item['text'], html=False) + self._page(data['text/plain'], html=False) #------ Trait change handlers -------------------------------------------- diff --git a/IPython/terminal/console/interactiveshell.py b/IPython/terminal/console/interactiveshell.py index e91378a..1a298f8 100644 --- a/IPython/terminal/console/interactiveshell.py +++ b/IPython/terminal/console/interactiveshell.py @@ -206,11 +206,13 @@ class ZMQTerminalInteractiveShell(TerminalInteractiveShell): self.write('Aborted\n') return elif status == 'ok': - # print execution payloads as well: + # handle payloads for item in content["payload"]: - text = item.get('text', None) - if text: - page.page(text) + source = item['source'] + if source == 'page': + page.page(item['data']['text/plain']) + elif source == 'set_next_input': + self.set_next_input(item['text']) elif status == 'error': for frame in content["traceback"]: