##// END OF EJS Templates
pager payload is a mime-bundle
MinRK -
Show More
@@ -153,6 +153,10 b' def page(strng, start=0, screen_lines=0, pager_cmd=None):'
153 written in python, very simplistic.
153 written in python, very simplistic.
154 """
154 """
155
155
156 # for compatibility with mime-bundle form:
157 if isinstance(strng, dict):
158 strng = strng['text/plain']
159
156 # Some routines may auto-compute start offsets incorrectly and pass a
160 # Some routines may auto-compute start offsets incorrectly and pass a
157 # negative value. Offset to 0 for robustness.
161 # negative value. Offset to 0 for robustness.
158 start = max(0, start)
162 start = max(0, start)
@@ -1,41 +1,16 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """A payload based version of page."""
3 A payload based version of page.
4
5 Authors:
6
3
7 * Brian Granger
4 # Copyright (c) IPython Development Team.
8 * Fernando Perez
5 # Distributed under the terms of the Modified BSD License.
9 """
10
6
11 #-----------------------------------------------------------------------------
7 from IPython.core.getipython import get_ipython
12 # Copyright (C) 2008-2011 The IPython Development Team
13 #
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
17
18 #-----------------------------------------------------------------------------
19 # Imports
20 #-----------------------------------------------------------------------------
21
22 # Third-party
23 try:
24 from docutils.core import publish_string
25 except ImportError:
26 # html paging won't be available, but we don't raise any errors. It's a
27 # purely optional feature.
28 pass
29
30 # Our own
31 from IPython.core.interactiveshell import InteractiveShell
32
8
33 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
34 # Classes and functions
10 # Classes and functions
35 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
36
12
37 def page(strng, start=0, screen_lines=0, pager_cmd=None,
13 def page(strng, start=0, screen_lines=0, pager_cmd=None):
38 html=None, auto_html=False):
39 """Print a string, piping through a pager.
14 """Print a string, piping through a pager.
40
15
41 This version ignores the screen_lines and pager_cmd arguments and uses
16 This version ignores the screen_lines and pager_cmd arguments and uses
@@ -43,49 +18,28 b' def page(strng, start=0, screen_lines=0, pager_cmd=None,'
43
18
44 Parameters
19 Parameters
45 ----------
20 ----------
46 strng : str
21 strng : str or mime-dict
47 Text to page.
22 Text to page, or a mime-type keyed dict of already formatted data.
48
23
49 start : int
24 start : int
50 Starting line at which to place the display.
25 Starting line at which to place the display.
51
52 html : str, optional
53 If given, an html string to send as well.
54
55 auto_html : bool, optional
56 If true, the input string is assumed to be valid reStructuredText and is
57 converted to HTML with docutils. Note that if docutils is not found,
58 this option is silently ignored.
59
60 Notes
61 -----
62
63 Only one of the ``html`` and ``auto_html`` options can be given, not
64 both.
65 """
26 """
66
27
67 # Some routines may auto-compute start offsets incorrectly and pass a
28 # Some routines may auto-compute start offsets incorrectly and pass a
68 # negative value. Offset to 0 for robustness.
29 # negative value. Offset to 0 for robustness.
69 start = max(0, start)
30 start = max(0, start)
70 shell = InteractiveShell.instance()
31 shell = get_ipython()
71
72 if auto_html:
73 try:
74 # These defaults ensure user configuration variables for docutils
75 # are not loaded, only our config is used here.
76 defaults = {'file_insertion_enabled': 0,
77 'raw_enabled': 0,
78 '_disable_config': 1}
79 html = publish_string(strng, writer_name='html',
80 settings_overrides=defaults)
81 except:
82 pass
83
32
33 if isinstance(strng, dict):
34 data = strng
35 else:
36 data = {'text/plain' : strng}
84 payload = dict(
37 payload = dict(
85 source='page',
38 source='page',
39 data=data,
86 text=strng,
40 text=strng,
87 html=html,
41 start=start,
88 start_line_number=start
42 screen_lines=screen_lines,
89 )
43 )
90 shell.payload_manager.write_payload(payload)
44 shell.payload_manager.write_payload(payload)
91
45
@@ -1,9 +1,5 b''
1 //----------------------------------------------------------------------------
1 // Copyright (c) IPython Development Team.
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Distributed under the terms of the Modified BSD License.
3 //
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
7
3
8 //============================================================================
4 //============================================================================
9 // Pager
5 // Pager
@@ -81,12 +77,18 b' var IPython = (function (IPython) {'
81 var that = this;
77 var that = this;
82
78
83 this.pager_element.bind('collapse_pager', function (event, extrap) {
79 this.pager_element.bind('collapse_pager', function (event, extrap) {
84 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
80 var time = 'fast';
81 if (extrap && extrap.duration) {
82 time = extrap.duration;
83 }
85 that.pager_element.hide(time);
84 that.pager_element.hide(time);
86 });
85 });
87
86
88 this.pager_element.bind('expand_pager', function (event, extrap) {
87 this.pager_element.bind('expand_pager', function (event, extrap) {
89 var time = (extrap != undefined) ? ((extrap.duration != undefined ) ? extrap.duration : 'fast') : 'fast';
88 var time = 'fast';
89 if (extrap && extrap.duration) {
90 time = extrap.duration;
91 }
90 that.pager_element.show(time);
92 that.pager_element.show(time);
91 });
93 });
92
94
@@ -103,12 +105,13 b' var IPython = (function (IPython) {'
103 that.toggle();
105 that.toggle();
104 });
106 });
105
107
106 $([IPython.events]).on('open_with_text.Pager', function (event, data) {
108 $([IPython.events]).on('open_with_text.Pager', function (event, payload) {
107 if (data.text.trim() !== '') {
109 // FIXME: support other mime types
110 if (payload.data['text/plain'] && payload.data['text/plain'] !== "") {
108 that.clear();
111 that.clear();
109 that.expand();
112 that.expand();
110 that.append_text(data.text);
113 that.append_text(payload.data['text/plain']);
111 };
114 }
112 });
115 });
113 };
116 };
114
117
@@ -117,7 +120,7 b' var IPython = (function (IPython) {'
117 if (this.expanded === true) {
120 if (this.expanded === true) {
118 this.expanded = false;
121 this.expanded = false;
119 this.pager_element.add($('div#notebook')).trigger('collapse_pager', extrap);
122 this.pager_element.add($('div#notebook')).trigger('collapse_pager', extrap);
120 };
123 }
121 };
124 };
122
125
123
126
@@ -125,7 +128,7 b' var IPython = (function (IPython) {'
125 if (this.expanded !== true) {
128 if (this.expanded !== true) {
126 this.expanded = true;
129 this.expanded = true;
127 this.pager_element.add($('div#notebook')).trigger('expand_pager', extrap);
130 this.pager_element.add($('div#notebook')).trigger('expand_pager', extrap);
128 };
131 }
129 };
132 };
130
133
131
134
@@ -134,7 +137,7 b' var IPython = (function (IPython) {'
134 this.collapse();
137 this.collapse();
135 } else {
138 } else {
136 this.expand();
139 this.expand();
137 };
140 }
138 };
141 };
139
142
140
143
@@ -160,8 +163,7 b' var IPython = (function (IPython) {'
160 pager_body.append(this.pager_element.clone().children());
163 pager_body.append(this.pager_element.clone().children());
161 w.document.close();
164 w.document.close();
162 this.collapse();
165 this.collapse();
163
166 };
164 }
165
167
166 Pager.prototype.append_text = function (text) {
168 Pager.prototype.append_text = function (text) {
167 // The only user content injected with this HTML call is escaped by
169 // The only user content injected with this HTML call is escaped by
@@ -532,10 +532,11 b' class IPythonWidget(FrontendWidget):'
532 # Since the plain text widget supports only a very small subset of HTML
532 # Since the plain text widget supports only a very small subset of HTML
533 # and we have no control over the HTML source, we only page HTML
533 # and we have no control over the HTML source, we only page HTML
534 # payloads in the rich text widget.
534 # payloads in the rich text widget.
535 if item['html'] and self.kind == 'rich':
535 data = item['data']
536 self._page(item['html'], html=True)
536 if 'text/html' in data and self.kind == 'rich':
537 self._page(data['text/html'], html=True)
537 else:
538 else:
538 self._page(item['text'], html=False)
539 self._page(data['text/plain'], html=False)
539
540
540 #------ Trait change handlers --------------------------------------------
541 #------ Trait change handlers --------------------------------------------
541
542
@@ -206,11 +206,13 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
206 self.write('Aborted\n')
206 self.write('Aborted\n')
207 return
207 return
208 elif status == 'ok':
208 elif status == 'ok':
209 # print execution payloads as well:
209 # handle payloads
210 for item in content["payload"]:
210 for item in content["payload"]:
211 text = item.get('text', None)
211 source = item['source']
212 if text:
212 if source == 'page':
213 page.page(text)
213 page.page(item['data']['text/plain'])
214 elif source == 'set_next_input':
215 self.set_next_input(item['text'])
214
216
215 elif status == 'error':
217 elif status == 'error':
216 for frame in content["traceback"]:
218 for frame in content["traceback"]:
General Comments 0
You need to be logged in to leave comments. Login now