Show More
@@ -0,0 +1,41 b'' | |||
|
1 | // Copyright (c) IPython Development Team. | |
|
2 | // Distributed under the terms of the Modified BSD License. | |
|
3 | ||
|
4 | define([ | |
|
5 | "widgets/js/widget", | |
|
6 | "jquery", | |
|
7 | 'notebook/js/outputarea', | |
|
8 | ], function(widget, $, outputarea){ | |
|
9 | ||
|
10 | var OutputView = widget.DOMWidgetView.extend({ | |
|
11 | initialize: function (parameters) { | |
|
12 | // Public constructor | |
|
13 | OutputView.__super__.initialize.apply(this, [parameters]); | |
|
14 | this.model.on('msg:custom', this._handle_route_msg, this); | |
|
15 | }, | |
|
16 | ||
|
17 | render: function(){ | |
|
18 | // Called when view is rendered. | |
|
19 | this.output_area = new outputarea.OutputArea({ | |
|
20 | selector: this.$el, | |
|
21 | prompt_area: false, | |
|
22 | events: this.model.widget_manager.notebook.events, | |
|
23 | keyboard_manager: this.model.widget_manager.keyboard_manager }); | |
|
24 | }, | |
|
25 | ||
|
26 | _handle_route_msg: function(content) { | |
|
27 | var cell = this.options.cell; | |
|
28 | if (content && cell) { | |
|
29 | if (content.method == 'push') { | |
|
30 | cell.push_output_area(this.output_area); | |
|
31 | } else if (content.method == 'pop') { | |
|
32 | cell.pop_output_area(this.output_area); | |
|
33 | } | |
|
34 | } | |
|
35 | }, | |
|
36 | }); | |
|
37 | ||
|
38 | return { | |
|
39 | 'OutputView': OutputView, | |
|
40 | }; | |
|
41 | }); |
@@ -0,0 +1,32 b'' | |||
|
1 | """Output class. | |
|
2 | ||
|
3 | Represents a widget that can be used to display output within the widget area. | |
|
4 | """ | |
|
5 | ||
|
6 | # Copyright (c) IPython Development Team. | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | ||
|
9 | from .widget import DOMWidget | |
|
10 | import sys | |
|
11 | from IPython.utils.traitlets import Unicode, List | |
|
12 | from IPython.display import clear_output | |
|
13 | ||
|
14 | class Output(DOMWidget): | |
|
15 | """Displays multiple widgets in a group.""" | |
|
16 | _view_name = Unicode('OutputView', sync=True) | |
|
17 | ||
|
18 | def clear_output(self, *pargs, **kwargs): | |
|
19 | with self: | |
|
20 | clear_output(*pargs, **kwargs) | |
|
21 | ||
|
22 | def __enter__(self): | |
|
23 | self._flush() | |
|
24 | self.send({'method': 'push'}) | |
|
25 | ||
|
26 | def __exit__(self, exception_type, exception_value, traceback): | |
|
27 | self._flush() | |
|
28 | self.send({'method': 'pop'}) | |
|
29 | ||
|
30 | def _flush(self): | |
|
31 | sys.stdout.flush() | |
|
32 | sys.stderr.flush() |
@@ -1,531 +1,565 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | /** |
|
4 | 4 | * |
|
5 | 5 | * |
|
6 | 6 | * @module codecell |
|
7 | 7 | * @namespace codecell |
|
8 | 8 | * @class CodeCell |
|
9 | 9 | */ |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | define([ |
|
13 | 13 | 'base/js/namespace', |
|
14 | 14 | 'jquery', |
|
15 | 15 | 'base/js/utils', |
|
16 | 16 | 'base/js/keyboard', |
|
17 | 17 | 'notebook/js/cell', |
|
18 | 18 | 'notebook/js/outputarea', |
|
19 | 19 | 'notebook/js/completer', |
|
20 | 20 | 'notebook/js/celltoolbar', |
|
21 | 21 | 'codemirror/lib/codemirror', |
|
22 | 22 | 'codemirror/mode/python/python', |
|
23 | 23 | 'notebook/js/codemirror-ipython' |
|
24 | 24 | ], function(IPython, $, utils, keyboard, cell, outputarea, completer, celltoolbar, CodeMirror, cmpython, cmip) { |
|
25 | 25 | "use strict"; |
|
26 | ||
|
26 | 27 | var Cell = cell.Cell; |
|
27 | 28 | |
|
28 | 29 | /* local util for codemirror */ |
|
29 | 30 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}; |
|
30 | 31 | |
|
31 | 32 | /** |
|
32 | 33 | * |
|
33 | 34 | * function to delete until previous non blanking space character |
|
34 | 35 | * or first multiple of 4 tabstop. |
|
35 | 36 | * @private |
|
36 | 37 | */ |
|
37 | 38 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ |
|
38 | 39 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); |
|
39 | 40 | if (!posEq(from, to)) { cm.replaceRange("", from, to); return; } |
|
40 | 41 | var cur = cm.getCursor(), line = cm.getLine(cur.line); |
|
41 | 42 | var tabsize = cm.getOption('tabSize'); |
|
42 | 43 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; |
|
43 | 44 | from = {ch:cur.ch-chToPrevTabStop,line:cur.line}; |
|
44 | 45 | var select = cm.getRange(from,cur); |
|
45 | 46 | if( select.match(/^\ +$/) !== null){ |
|
46 | 47 | cm.replaceRange("",from,cur); |
|
47 | 48 | } else { |
|
48 | 49 | cm.deleteH(-1,"char"); |
|
49 | 50 | } |
|
50 | 51 | }; |
|
51 | 52 | |
|
52 | 53 | var keycodes = keyboard.keycodes; |
|
53 | 54 | |
|
54 | 55 | var CodeCell = function (kernel, options) { |
|
55 | 56 | // Constructor |
|
56 | 57 | // |
|
57 | 58 | // A Cell conceived to write code. |
|
58 | 59 | // |
|
59 | 60 | // Parameters: |
|
60 | 61 | // kernel: Kernel instance |
|
61 | 62 | // The kernel doesn't have to be set at creation time, in that case |
|
62 | 63 | // it will be null and set_kernel has to be called later. |
|
63 | 64 | // options: dictionary |
|
64 | 65 | // Dictionary of keyword arguments. |
|
65 | 66 | // events: $(Events) instance |
|
66 | 67 | // config: dictionary |
|
67 | 68 | // keyboard_manager: KeyboardManager instance |
|
68 | 69 | // notebook: Notebook instance |
|
69 | 70 | // tooltip: Tooltip instance |
|
70 | 71 | this.kernel = kernel || null; |
|
71 | 72 | this.notebook = options.notebook; |
|
72 | 73 | this.collapsed = false; |
|
73 | 74 | this.events = options.events; |
|
74 | 75 | this.tooltip = options.tooltip; |
|
75 | 76 | this.config = options.config; |
|
76 | 77 | |
|
77 | 78 | // create all attributed in constructor function |
|
78 | 79 | // even if null for V8 VM optimisation |
|
79 | 80 | this.input_prompt_number = null; |
|
80 | 81 | this.celltoolbar = null; |
|
81 | 82 | this.output_area = null; |
|
83 | this.active_output_area = []; | |
|
82 | 84 | this.last_msg_id = null; |
|
83 | 85 | this.completer = null; |
|
84 | 86 | |
|
85 | 87 | |
|
86 | 88 | var config = utils.mergeopt(CodeCell, this.config); |
|
87 | 89 | Cell.apply(this,[{ |
|
88 | 90 | config: config, |
|
89 | 91 | keyboard_manager: options.keyboard_manager, |
|
90 | 92 | events: this.events}]); |
|
91 | 93 | |
|
92 | 94 | // Attributes we want to override in this subclass. |
|
93 | 95 | this.cell_type = "code"; |
|
94 | 96 | |
|
95 | 97 | var that = this; |
|
96 | 98 | this.element.focusout( |
|
97 | 99 | function() { that.auto_highlight(); } |
|
98 | 100 | ); |
|
99 | 101 | }; |
|
100 | 102 | |
|
101 | 103 | CodeCell.options_default = { |
|
102 | 104 | cm_config : { |
|
103 | 105 | extraKeys: { |
|
104 | 106 | "Tab" : "indentMore", |
|
105 | 107 | "Shift-Tab" : "indentLess", |
|
106 | 108 | "Backspace" : "delSpaceToPrevTabStop", |
|
107 | 109 | "Cmd-/" : "toggleComment", |
|
108 | 110 | "Ctrl-/" : "toggleComment" |
|
109 | 111 | }, |
|
110 | 112 | mode: 'ipython', |
|
111 | 113 | theme: 'ipython', |
|
112 | 114 | matchBrackets: true |
|
113 | 115 | } |
|
114 | 116 | }; |
|
115 | 117 | |
|
116 | 118 | CodeCell.msg_cells = {}; |
|
117 | 119 | |
|
118 | 120 | CodeCell.prototype = Object.create(Cell.prototype); |
|
119 | 121 | |
|
120 | 122 | /** |
|
123 | * @method get_output_area | |
|
124 | */ | |
|
125 | CodeCell.prototype.get_output_area = function () { | |
|
126 | if (this.active_output_area && this.active_output_area.length > 0) { | |
|
127 | return this.active_output_area[this.active_output_area.length-1]; | |
|
128 | } else { | |
|
129 | return this.output_area; | |
|
130 | } | |
|
131 | }; | |
|
132 | ||
|
133 | /** | |
|
134 | * @method push_output_area | |
|
135 | */ | |
|
136 | CodeCell.prototype.push_output_area = function (output_area) { | |
|
137 | this.active_output_area.push(output_area); | |
|
138 | }; | |
|
139 | ||
|
140 | /** | |
|
141 | * @method pop_output_area | |
|
142 | */ | |
|
143 | CodeCell.prototype.pop_output_area = function () { | |
|
144 | this.active_output_area.pop(); | |
|
145 | }; | |
|
146 | ||
|
147 | /** | |
|
121 | 148 | * @method auto_highlight |
|
122 | 149 | */ |
|
123 | 150 | CodeCell.prototype.auto_highlight = function () { |
|
124 | 151 | this._auto_highlight(this.config.cell_magic_highlight); |
|
125 | 152 | }; |
|
126 | 153 | |
|
127 | 154 | /** @method create_element */ |
|
128 | 155 | CodeCell.prototype.create_element = function () { |
|
129 | 156 | Cell.prototype.create_element.apply(this, arguments); |
|
130 | 157 | |
|
131 | 158 | var cell = $('<div></div>').addClass('cell code_cell'); |
|
132 | 159 | cell.attr('tabindex','2'); |
|
133 | 160 | |
|
134 | 161 | var input = $('<div></div>').addClass('input'); |
|
135 | 162 | var prompt = $('<div/>').addClass('prompt input_prompt'); |
|
136 | 163 | var inner_cell = $('<div/>').addClass('inner_cell'); |
|
137 | 164 | this.celltoolbar = new celltoolbar.CellToolbar({ |
|
138 | 165 | cell: this, |
|
139 | 166 | notebook: this.notebook}); |
|
140 | 167 | inner_cell.append(this.celltoolbar.element); |
|
141 | 168 | var input_area = $('<div/>').addClass('input_area'); |
|
142 | 169 | this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config); |
|
143 | 170 | this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this)) |
|
144 | 171 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); |
|
145 | 172 | inner_cell.append(input_area); |
|
146 | 173 | input.append(prompt).append(inner_cell); |
|
147 | 174 | |
|
148 | 175 | var widget_area = $('<div/>') |
|
149 | 176 | .addClass('widget-area') |
|
150 | 177 | .hide(); |
|
151 | 178 | this.widget_area = widget_area; |
|
152 | 179 | var widget_prompt = $('<div/>') |
|
153 | 180 | .addClass('prompt') |
|
154 | 181 | .appendTo(widget_area); |
|
155 | 182 | var widget_subarea = $('<div/>') |
|
156 | 183 | .addClass('widget-subarea') |
|
157 | 184 | .appendTo(widget_area); |
|
158 | 185 | this.widget_subarea = widget_subarea; |
|
159 | 186 | var widget_clear_buton = $('<button />') |
|
160 | 187 | .addClass('close') |
|
161 | 188 | .html('×') |
|
162 | 189 | .click(function() { |
|
163 | 190 | widget_area.slideUp('', function(){ widget_subarea.html(''); }); |
|
164 | 191 | }) |
|
165 | 192 | .appendTo(widget_prompt); |
|
166 | 193 | |
|
167 | 194 | var output = $('<div></div>'); |
|
168 | 195 | cell.append(input).append(widget_area).append(output); |
|
169 | 196 | this.element = cell; |
|
170 | 197 | this.output_area = new outputarea.OutputArea({ |
|
171 | 198 | selector: output, |
|
172 | 199 | prompt_area: true, |
|
173 | 200 | events: this.events, |
|
174 | 201 | keyboard_manager: this.keyboard_manager}); |
|
175 | 202 | this.completer = new completer.Completer(this, this.events); |
|
176 | 203 | }; |
|
177 | 204 | |
|
178 | 205 | /** @method bind_events */ |
|
179 | 206 | CodeCell.prototype.bind_events = function () { |
|
180 | 207 | Cell.prototype.bind_events.apply(this); |
|
181 | 208 | var that = this; |
|
182 | 209 | |
|
183 | 210 | this.element.focusout( |
|
184 | 211 | function() { that.auto_highlight(); } |
|
185 | 212 | ); |
|
186 | 213 | }; |
|
187 | 214 | |
|
188 | 215 | |
|
189 | 216 | /** |
|
190 | 217 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
191 | 218 | * handlers and is used to provide custom key handling. Its return |
|
192 | 219 | * value is used to determine if CodeMirror should ignore the event: |
|
193 | 220 | * true = ignore, false = don't ignore. |
|
194 | 221 | * @method handle_codemirror_keyevent |
|
195 | 222 | */ |
|
196 | 223 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
197 | 224 | |
|
198 | 225 | var that = this; |
|
199 | 226 | // whatever key is pressed, first, cancel the tooltip request before |
|
200 | 227 | // they are sent, and remove tooltip if any, except for tab again |
|
201 | 228 | var tooltip_closed = null; |
|
202 | 229 | if (event.type === 'keydown' && event.which != keycodes.tab ) { |
|
203 | 230 | tooltip_closed = this.tooltip.remove_and_cancel_tooltip(); |
|
204 | 231 | } |
|
205 | 232 | |
|
206 | 233 | var cur = editor.getCursor(); |
|
207 | 234 | if (event.keyCode === keycodes.enter){ |
|
208 | 235 | this.auto_highlight(); |
|
209 | 236 | } |
|
210 | 237 | |
|
211 | 238 | if (event.which === keycodes.down && event.type === 'keypress' && this.tooltip.time_before_tooltip >= 0) { |
|
212 | 239 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform |
|
213 | 240 | // browser and keyboard layout ! |
|
214 | 241 | // Pressing '(' , request tooltip, don't forget to reappend it |
|
215 | 242 | // The second argument says to hide the tooltip if the docstring |
|
216 | 243 | // is actually empty |
|
217 | 244 | this.tooltip.pending(that, true); |
|
218 | 245 | } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') { |
|
219 | 246 | // If tooltip is active, cancel it. The call to |
|
220 | 247 | // remove_and_cancel_tooltip above doesn't pass, force=true. |
|
221 | 248 | // Because of this it won't actually close the tooltip |
|
222 | 249 | // if it is in sticky mode. Thus, we have to check again if it is open |
|
223 | 250 | // and close it with force=true. |
|
224 | 251 | if (!this.tooltip._hidden) { |
|
225 | 252 | this.tooltip.remove_and_cancel_tooltip(true); |
|
226 | 253 | } |
|
227 | 254 | // If we closed the tooltip, don't let CM or the global handlers |
|
228 | 255 | // handle this event. |
|
229 | 256 | event.codemirrorIgnore = true; |
|
230 | 257 | event.preventDefault(); |
|
231 | 258 | return true; |
|
232 | 259 | } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) { |
|
233 | 260 | if (editor.somethingSelected() || editor.getSelections().length !== 1){ |
|
234 | 261 | var anchor = editor.getCursor("anchor"); |
|
235 | 262 | var head = editor.getCursor("head"); |
|
236 | 263 | if( anchor.line != head.line){ |
|
237 | 264 | return false; |
|
238 | 265 | } |
|
239 | 266 | } |
|
240 | 267 | this.tooltip.request(that); |
|
241 | 268 | event.codemirrorIgnore = true; |
|
242 | 269 | event.preventDefault(); |
|
243 | 270 | return true; |
|
244 | 271 | } else if (event.keyCode === keycodes.tab && event.type == 'keydown') { |
|
245 | 272 | // Tab completion. |
|
246 | 273 | this.tooltip.remove_and_cancel_tooltip(); |
|
247 | 274 | |
|
248 | 275 | // completion does not work on multicursor, it might be possible though in some cases |
|
249 | 276 | if (editor.somethingSelected() || editor.getSelections().length > 1) { |
|
250 | 277 | return false; |
|
251 | 278 | } |
|
252 | 279 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); |
|
253 | 280 | if (pre_cursor.trim() === "") { |
|
254 | 281 | // Don't autocomplete if the part of the line before the cursor |
|
255 | 282 | // is empty. In this case, let CodeMirror handle indentation. |
|
256 | 283 | return false; |
|
257 | 284 | } else { |
|
258 | 285 | event.codemirrorIgnore = true; |
|
259 | 286 | event.preventDefault(); |
|
260 | 287 | this.completer.startCompletion(); |
|
261 | 288 | return true; |
|
262 | 289 | } |
|
263 | 290 | } |
|
264 | 291 | |
|
265 | 292 | // keyboard event wasn't one of those unique to code cells, let's see |
|
266 | 293 | // if it's one of the generic ones (i.e. check edit mode shortcuts) |
|
267 | 294 | return Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event]); |
|
268 | 295 | }; |
|
269 | 296 | |
|
270 | 297 | // Kernel related calls. |
|
271 | 298 | |
|
272 | 299 | CodeCell.prototype.set_kernel = function (kernel) { |
|
273 | 300 | this.kernel = kernel; |
|
274 | 301 | }; |
|
275 | 302 | |
|
276 | 303 | /** |
|
277 | 304 | * Execute current code cell to the kernel |
|
278 | 305 | * @method execute |
|
279 | 306 | */ |
|
280 | 307 | CodeCell.prototype.execute = function () { |
|
281 | 308 | if (!this.kernel || !this.kernel.is_connected()) { |
|
282 | 309 | console.log("Can't execute, kernel is not connected."); |
|
283 | 310 | return; |
|
284 | 311 | } |
|
285 | ||
|
286 | this.output_area.clear_output(); | |
|
312 | ||
|
313 | this.get_output_area().clear_output(); | |
|
287 | 314 | |
|
288 | 315 | // Clear widget area |
|
289 | 316 | this.widget_subarea.html(''); |
|
290 | 317 | this.widget_subarea.height(''); |
|
291 | 318 | this.widget_area.height(''); |
|
292 | 319 | this.widget_area.hide(); |
|
293 | 320 | |
|
294 | 321 | this.set_input_prompt('*'); |
|
295 | 322 | this.element.addClass("running"); |
|
296 | 323 | if (this.last_msg_id) { |
|
297 | 324 | this.kernel.clear_callbacks_for_msg(this.last_msg_id); |
|
298 | 325 | } |
|
299 | 326 | var callbacks = this.get_callbacks(); |
|
300 | 327 | |
|
301 | 328 | var old_msg_id = this.last_msg_id; |
|
302 | 329 | this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true}); |
|
303 | 330 | if (old_msg_id) { |
|
304 | 331 | delete CodeCell.msg_cells[old_msg_id]; |
|
305 | 332 | } |
|
306 | 333 | CodeCell.msg_cells[this.last_msg_id] = this; |
|
307 | 334 | this.render(); |
|
308 | 335 | }; |
|
309 | 336 | |
|
310 | 337 | /** |
|
311 | 338 | * Construct the default callbacks for |
|
312 | 339 | * @method get_callbacks |
|
313 | 340 | */ |
|
314 | 341 | CodeCell.prototype.get_callbacks = function () { |
|
342 | var that = this; | |
|
315 | 343 | return { |
|
316 | 344 | shell : { |
|
317 | 345 | reply : $.proxy(this._handle_execute_reply, this), |
|
318 | 346 | payload : { |
|
319 | 347 | set_next_input : $.proxy(this._handle_set_next_input, this), |
|
320 | 348 | page : $.proxy(this._open_with_pager, this) |
|
321 | 349 | } |
|
322 | 350 | }, |
|
323 | 351 | iopub : { |
|
324 | output : $.proxy(this.output_area.handle_output, this.output_area), | |
|
325 | clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area), | |
|
352 | output : function() { | |
|
353 | var output_area = that.get_output_area(); | |
|
354 | output_area.handle_output.apply(output_area, arguments); | |
|
355 | }, | |
|
356 | clear_output : function() { | |
|
357 | var output_area = that.get_output_area(); | |
|
358 | output_area.handle_clear_output.apply(output_area, arguments); | |
|
359 | }, | |
|
326 | 360 | }, |
|
327 | 361 | input : $.proxy(this._handle_input_request, this) |
|
328 | 362 | }; |
|
329 | 363 | }; |
|
330 | 364 | |
|
331 | 365 | CodeCell.prototype._open_with_pager = function (payload) { |
|
332 | 366 | this.events.trigger('open_with_text.Pager', payload); |
|
333 | 367 | }; |
|
334 | 368 | |
|
335 | 369 | /** |
|
336 | 370 | * @method _handle_execute_reply |
|
337 | 371 | * @private |
|
338 | 372 | */ |
|
339 | 373 | CodeCell.prototype._handle_execute_reply = function (msg) { |
|
340 | 374 | this.set_input_prompt(msg.content.execution_count); |
|
341 | 375 | this.element.removeClass("running"); |
|
342 | 376 | this.events.trigger('set_dirty.Notebook', {value: true}); |
|
343 | 377 | }; |
|
344 | 378 | |
|
345 | 379 | /** |
|
346 | 380 | * @method _handle_set_next_input |
|
347 | 381 | * @private |
|
348 | 382 | */ |
|
349 | 383 | CodeCell.prototype._handle_set_next_input = function (payload) { |
|
350 | 384 | var data = {'cell': this, 'text': payload.text}; |
|
351 | 385 | this.events.trigger('set_next_input.Notebook', data); |
|
352 | 386 | }; |
|
353 | 387 | |
|
354 | 388 | /** |
|
355 | 389 | * @method _handle_input_request |
|
356 | 390 | * @private |
|
357 | 391 | */ |
|
358 | 392 | CodeCell.prototype._handle_input_request = function (msg) { |
|
359 | this.output_area.append_raw_input(msg); | |
|
393 | this.get_output_area().append_raw_input(msg); | |
|
360 | 394 | }; |
|
361 | 395 | |
|
362 | 396 | |
|
363 | 397 | // Basic cell manipulation. |
|
364 | 398 | |
|
365 | 399 | CodeCell.prototype.select = function () { |
|
366 | 400 | var cont = Cell.prototype.select.apply(this); |
|
367 | 401 | if (cont) { |
|
368 | 402 | this.code_mirror.refresh(); |
|
369 | 403 | this.auto_highlight(); |
|
370 | 404 | } |
|
371 | 405 | return cont; |
|
372 | 406 | }; |
|
373 | 407 | |
|
374 | 408 | CodeCell.prototype.render = function () { |
|
375 | 409 | var cont = Cell.prototype.render.apply(this); |
|
376 | 410 | // Always execute, even if we are already in the rendered state |
|
377 | 411 | return cont; |
|
378 | 412 | }; |
|
379 | 413 | |
|
380 | 414 | CodeCell.prototype.select_all = function () { |
|
381 | 415 | var start = {line: 0, ch: 0}; |
|
382 | 416 | var nlines = this.code_mirror.lineCount(); |
|
383 | 417 | var last_line = this.code_mirror.getLine(nlines-1); |
|
384 | 418 | var end = {line: nlines-1, ch: last_line.length}; |
|
385 | 419 | this.code_mirror.setSelection(start, end); |
|
386 | 420 | }; |
|
387 | 421 | |
|
388 | 422 | |
|
389 | 423 | CodeCell.prototype.collapse_output = function () { |
|
390 | 424 | this.output_area.collapse(); |
|
391 | 425 | }; |
|
392 | 426 | |
|
393 | 427 | |
|
394 | 428 | CodeCell.prototype.expand_output = function () { |
|
395 | 429 | this.output_area.expand(); |
|
396 | 430 | this.output_area.unscroll_area(); |
|
397 | 431 | }; |
|
398 | 432 | |
|
399 | 433 | CodeCell.prototype.scroll_output = function () { |
|
400 | 434 | this.output_area.expand(); |
|
401 | 435 | this.output_area.scroll_if_long(); |
|
402 | 436 | }; |
|
403 | 437 | |
|
404 | 438 | CodeCell.prototype.toggle_output = function () { |
|
405 | 439 | this.output_area.toggle_output(); |
|
406 | 440 | }; |
|
407 | 441 | |
|
408 | 442 | CodeCell.prototype.toggle_output_scroll = function () { |
|
409 | 443 | this.output_area.toggle_scroll(); |
|
410 | 444 | }; |
|
411 | 445 | |
|
412 | 446 | |
|
413 | 447 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { |
|
414 | 448 | var ns; |
|
415 | 449 | if (prompt_value === undefined || prompt_value === null) { |
|
416 | 450 | ns = " "; |
|
417 | 451 | } else { |
|
418 | 452 | ns = encodeURIComponent(prompt_value); |
|
419 | 453 | } |
|
420 | 454 | return 'In [' + ns + ']:'; |
|
421 | 455 | }; |
|
422 | 456 | |
|
423 | 457 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { |
|
424 | 458 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; |
|
425 | 459 | for(var i=1; i < lines_number; i++) { |
|
426 | 460 | html.push(['...:']); |
|
427 | 461 | } |
|
428 | 462 | return html.join('<br/>'); |
|
429 | 463 | }; |
|
430 | 464 | |
|
431 | 465 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; |
|
432 | 466 | |
|
433 | 467 | |
|
434 | 468 | CodeCell.prototype.set_input_prompt = function (number) { |
|
435 | 469 | var nline = 1; |
|
436 | 470 | if (this.code_mirror !== undefined) { |
|
437 | 471 | nline = this.code_mirror.lineCount(); |
|
438 | 472 | } |
|
439 | 473 | this.input_prompt_number = number; |
|
440 | 474 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); |
|
441 | 475 | // This HTML call is okay because the user contents are escaped. |
|
442 | 476 | this.element.find('div.input_prompt').html(prompt_html); |
|
443 | 477 | }; |
|
444 | 478 | |
|
445 | 479 | |
|
446 | 480 | CodeCell.prototype.clear_input = function () { |
|
447 | 481 | this.code_mirror.setValue(''); |
|
448 | 482 | }; |
|
449 | 483 | |
|
450 | 484 | |
|
451 | 485 | CodeCell.prototype.get_text = function () { |
|
452 | 486 | return this.code_mirror.getValue(); |
|
453 | 487 | }; |
|
454 | 488 | |
|
455 | 489 | |
|
456 | 490 | CodeCell.prototype.set_text = function (code) { |
|
457 | 491 | return this.code_mirror.setValue(code); |
|
458 | 492 | }; |
|
459 | 493 | |
|
460 | 494 | |
|
461 | 495 | CodeCell.prototype.clear_output = function (wait) { |
|
462 | this.output_area.clear_output(wait); | |
|
496 | this.get_output_area().clear_output(wait); | |
|
463 | 497 | this.set_input_prompt(); |
|
464 | 498 | }; |
|
465 | 499 | |
|
466 | 500 | |
|
467 | 501 | // JSON serialization |
|
468 | 502 | |
|
469 | 503 | CodeCell.prototype.fromJSON = function (data) { |
|
470 | 504 | Cell.prototype.fromJSON.apply(this, arguments); |
|
471 | 505 | if (data.cell_type === 'code') { |
|
472 | 506 | if (data.source !== undefined) { |
|
473 | 507 | this.set_text(data.source); |
|
474 | 508 | // make this value the starting point, so that we can only undo |
|
475 | 509 | // to this state, instead of a blank cell |
|
476 | 510 | this.code_mirror.clearHistory(); |
|
477 | 511 | this.auto_highlight(); |
|
478 | 512 | } |
|
479 | 513 | this.set_input_prompt(data.execution_count); |
|
480 | 514 | this.output_area.trusted = data.metadata.trusted || false; |
|
481 | 515 | this.output_area.fromJSON(data.outputs); |
|
482 | 516 | if (data.metadata.collapsed !== undefined) { |
|
483 | 517 | if (data.metadata.collapsed) { |
|
484 | 518 | this.collapse_output(); |
|
485 | 519 | } else { |
|
486 | 520 | this.expand_output(); |
|
487 | 521 | } |
|
488 | 522 | } |
|
489 | 523 | } |
|
490 | 524 | }; |
|
491 | 525 | |
|
492 | 526 | |
|
493 | 527 | CodeCell.prototype.toJSON = function () { |
|
494 | 528 | var data = Cell.prototype.toJSON.apply(this); |
|
495 | 529 | data.source = this.get_text(); |
|
496 | 530 | // is finite protect against undefined and '*' value |
|
497 | 531 | if (isFinite(this.input_prompt_number)) { |
|
498 | 532 | data.execution_count = this.input_prompt_number; |
|
499 | 533 | } else { |
|
500 | 534 | data.execution_count = null; |
|
501 | 535 | } |
|
502 | 536 | var outputs = this.output_area.toJSON(); |
|
503 | 537 | data.outputs = outputs; |
|
504 | 538 | data.metadata.trusted = this.output_area.trusted; |
|
505 | 539 | data.metadata.collapsed = this.output_area.collapsed; |
|
506 | 540 | return data; |
|
507 | 541 | }; |
|
508 | 542 | |
|
509 | 543 | /** |
|
510 | 544 | * handle cell level logic when a cell is unselected |
|
511 | 545 | * @method unselect |
|
512 | 546 | * @return is the action being taken |
|
513 | 547 | */ |
|
514 | 548 | CodeCell.prototype.unselect = function () { |
|
515 | 549 | var cont = Cell.prototype.unselect.apply(this); |
|
516 | 550 | if (cont) { |
|
517 | 551 | // When a code cell is usnelected, make sure that the corresponding |
|
518 | 552 | // tooltip and completer to that cell is closed. |
|
519 | 553 | this.tooltip.remove_and_cancel_tooltip(true); |
|
520 | 554 | if (this.completer !== null) { |
|
521 | 555 | this.completer.close(); |
|
522 | 556 | } |
|
523 | 557 | } |
|
524 | 558 | return cont; |
|
525 | 559 | }; |
|
526 | 560 | |
|
527 | 561 | // Backwards compatability. |
|
528 | 562 | IPython.CodeCell = CodeCell; |
|
529 | 563 | |
|
530 | 564 | return {'CodeCell': CodeCell}; |
|
531 | 565 | }); |
@@ -1,27 +1,28 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | "widgets/js/manager", |
|
6 | 6 | "widgets/js/widget_bool", |
|
7 | 7 | "widgets/js/widget_button", |
|
8 | 8 | "widgets/js/widget_box", |
|
9 | 9 | "widgets/js/widget_float", |
|
10 | 10 | "widgets/js/widget_image", |
|
11 | 11 | "widgets/js/widget_int", |
|
12 | "widgets/js/widget_output", | |
|
12 | 13 | "widgets/js/widget_selection", |
|
13 | 14 | "widgets/js/widget_selectioncontainer", |
|
14 | 15 | "widgets/js/widget_string", |
|
15 | 16 | ], function(widgetmanager) { |
|
16 | 17 | |
|
17 | 18 | // Register all of the loaded views with the widget manager. |
|
18 | 19 | for (var i = 1; i < arguments.length; i++) { |
|
19 | 20 | for (var target_name in arguments[i]) { |
|
20 | 21 | if (arguments[i].hasOwnProperty(target_name)) { |
|
21 | 22 | widgetmanager.WidgetManager.register_widget_view(target_name, arguments[i][target_name]); |
|
22 | 23 | } |
|
23 | 24 | } |
|
24 | 25 | } |
|
25 | 26 | |
|
26 | 27 | return {'WidgetManager': widgetmanager.WidgetManager}; |
|
27 | 28 | }); |
@@ -1,23 +1,24 b'' | |||
|
1 | 1 | from .widget import Widget, DOMWidget, CallbackDispatcher, register |
|
2 | 2 | |
|
3 | 3 | from .widget_bool import Checkbox, ToggleButton |
|
4 | 4 | from .widget_button import Button |
|
5 | 5 | from .widget_box import Box, Popup, FlexBox, HBox, VBox |
|
6 | 6 | from .widget_float import FloatText, BoundedFloatText, FloatSlider, FloatProgress, FloatRangeSlider |
|
7 | 7 | from .widget_image import Image |
|
8 | 8 | from .widget_int import IntText, BoundedIntText, IntSlider, IntProgress, IntRangeSlider |
|
9 | from .widget_output import Output | |
|
9 | 10 | from .widget_selection import RadioButtons, ToggleButtons, Dropdown, Select |
|
10 | 11 | from .widget_selectioncontainer import Tab, Accordion |
|
11 | 12 | from .widget_string import HTML, Latex, Text, Textarea |
|
12 | 13 | from .interaction import interact, interactive, fixed, interact_manual |
|
13 | 14 | |
|
14 | 15 | # Deprecated classes |
|
15 | 16 | from .widget_bool import CheckboxWidget, ToggleButtonWidget |
|
16 | 17 | from .widget_button import ButtonWidget |
|
17 | 18 | from .widget_box import ContainerWidget, PopupWidget |
|
18 | 19 | from .widget_float import FloatTextWidget, BoundedFloatTextWidget, FloatSliderWidget, FloatProgressWidget |
|
19 | 20 | from .widget_image import ImageWidget |
|
20 | 21 | from .widget_int import IntTextWidget, BoundedIntTextWidget, IntSliderWidget, IntProgressWidget |
|
21 | 22 | from .widget_selection import RadioButtonsWidget, ToggleButtonsWidget, DropdownWidget, SelectWidget |
|
22 | 23 | from .widget_selectioncontainer import TabWidget, AccordionWidget |
|
23 | 24 | from .widget_string import HTMLWidget, LatexWidget, TextWidget, TextareaWidget |
General Comments 0
You need to be logged in to leave comments.
Login now