##// END OF EJS Templates
Function to refactor print statements in doctests to print() function calls for Python 3.
Function to refactor print statements in doctests to print() function calls for Python 3.

File last commit:

r4704:7b26cae7
r4890:055cc6cb
Show More
codecell.js
452 lines | 14.9 KiB | application/javascript | JavascriptLexer
Brian E. Granger
More review changes....
r4609 //----------------------------------------------------------------------------
// 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.
//----------------------------------------------------------------------------
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
//============================================================================
// CodeCell
//============================================================================
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 var IPython = (function (IPython) {
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 var utils = IPython.utils;
var CodeCell = function (notebook) {
this.code_mirror = null;
this.input_prompt_number = ' ';
Brian E. Granger
Autocompletion working with CTRL-SPACE.
r4389 this.is_completing = false;
this.completion_cursor = null;
Brian E. Granger
Added saving and loading of output of all types.
r4497 this.outputs = [];
Brian E. Granger
Added collapsed field to the code cell.
r4533 this.collapsed = false;
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 IPython.Cell.apply(this, arguments);
};
CodeCell.prototype = new IPython.Cell();
CodeCell.prototype.create_element = function () {
Brian E. Granger
Pager is working again.
r4361 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
Brian E. Granger
Better tabindex support.
r4629 cell.attr('tabindex','2');
Brian E. Granger
More work updating the notebook to use dynamics resizing.
r4360 var input = $('<div></div>').addClass('input hbox');
Brian E. Granger
Updating font-sizing to use the YUI protocol.
r4379 input.append($('<div/>').addClass('prompt input_prompt'));
Brian E. Granger
More work updating the notebook to use dynamics resizing.
r4360 var input_area = $('<div/>').addClass('input_area box-flex1');
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 this.code_mirror = CodeMirror(input_area.get(0), {
indentUnit : 4,
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 mode: 'python',
theme: 'ipython',
Brian E. Granger
Fixing execution related things....
r4378 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 });
input.append(input_area);
Brian E. Granger
More work updating the notebook to use dynamics resizing.
r4360 var output = $('<div></div>').addClass('output vbox');
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 cell.append(input).append(output);
this.element = cell;
this.collapse()
};
Brian E. Granger
Fixing execution related things....
r4378 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
// This method gets called in CodeMirror's onKeyDown/onKeyPress handlers and
// is used to provide custom key handling. Its return value is used to determine
// if CodeMirror should ignore the event: true = ignore, false = don't ignore.
Brian E. Granger
Fixing Ctrl-Enter on Firefox.
r4704 if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) {
Brian E. Granger
Fixing execution related things....
r4378 // Always ignore shift-enter in CodeMirror as we handle it.
return true;
Brian E. Granger
Fixing tab completion edge cases.
r4555 } else if (event.keyCode === 9 && event.type == 'keydown') {
// Tab completion.
Brian E. Granger
Autocompletion working with CTRL-SPACE.
r4389 var cur = editor.getCursor();
Brian E. Granger
Notebook now uses tab for autocompletion.
r4393 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur).trim();
if (pre_cursor === "") {
// Don't autocomplete if the part of the line before the cursor is empty.
// In this case, let CodeMirror handle indentation.
return false;
} else {
// Autocomplete the current line.
event.stop();
var line = editor.getLine(cur.line);
this.is_completing = true;
this.completion_cursor = cur;
IPython.notebook.complete_cell(this, line, cur.ch);
return true;
}
Brian E. Granger
Autoindentation fixed and enabled by default.
r4529 } else if (event.keyCode === 8 && event.type == 'keydown') {
Brian E. Granger
Implemented smart autoindenting.
r4512 // If backspace and the line ends with 4 spaces, remove them.
var cur = editor.getCursor();
var line = editor.getLine(cur.line);
var ending = line.slice(-4);
if (ending === ' ') {
editor.replaceRange('',
{line: cur.line, ch: cur.ch-4},
{line: cur.line, ch: cur.ch}
);
event.stop();
return true;
} else {
return false;
};
Brian E. Granger
Fixing execution related things....
r4378 } else {
Brian E. Granger
Fixing tab completion edge cases.
r4555 // keypress/keyup also trigger on TAB press, and we don't want to use those
// to disable tab completion.
if (this.is_completing && event.keyCode !== 9) {
var ed_cur = editor.getCursor();
var cc_cur = this.completion_cursor;
if (ed_cur.line !== cc_cur.line || ed_cur.ch !== cc_cur.ch) {
this.is_completing = false;
this.completion_cursor = null;
};
};
Brian E. Granger
Fixing execution related things....
r4378 return false;
};
};
Brian E. Granger
Autocompletion working with CTRL-SPACE.
r4389
CodeCell.prototype.finish_completing = function (matched_text, matches) {
// console.log("Got matches", matched_text, matches);
Brian E. Granger
Fixing tab completion edge cases.
r4555 if (!this.is_completing || matches.length === 0) {return;}
Brian E. Granger
Autocompletion working with CTRL-SPACE.
r4389
var that = this;
var cur = this.completion_cursor;
Brian E. Granger
A single tab-completion match is now automatically selected.
r4554
var insert = function (selected_text) {
that.code_mirror.replaceRange(
selected_text,
{line: cur.line, ch: (cur.ch-matched_text.length)},
{line: cur.line, ch: cur.ch}
);
};
if (matches.length === 1) {
insert(matches[0]);
setTimeout(function(){that.code_mirror.focus();}, 50);
return;
};
Brian E. Granger
Autocompletion working with CTRL-SPACE.
r4389 var complete = $('<div/>').addClass('completions');
var select = $('<select/>').attr('multiple','true');
for (var i=0; i<matches.length; ++i) {
select.append($('<option/>').text(matches[i]));
}
select.children().first().attr('selected','true');
select.attr('size',Math.min(10,matches.length));
var pos = this.code_mirror.cursorCoords();
complete.css('left',pos.x+'px');
complete.css('top',pos.yBot+'px');
complete.append(select);
$('body').append(complete);
var done = false;
var close = function () {
if (done) return;
done = true;
complete.remove();
that.is_completing = false;
that.completion_cursor = null;
};
var pick = function () {
insert(select.val()[0]);
close();
setTimeout(function(){that.code_mirror.focus();}, 50);
};
select.blur(close);
select.keydown(function (event) {
var code = event.which;
if (code === 13 || code === 32) {
// Pressing SPACE or ENTER will cause a pick
event.stopPropagation();
event.preventDefault();
pick();
} else if (code === 38 || code === 40) {
// We don't want the document keydown handler to handle UP/DOWN,
// but we want the default action.
event.stopPropagation();
} else {
Brian E. Granger
Fixing tab completion edge cases.
r4555 // All other key presses exit completion.
Brian E. Granger
Autocompletion working with CTRL-SPACE.
r4389 event.stopPropagation();
event.preventDefault();
close();
that.code_mirror.focus();
}
});
// Double click also causes a pick.
select.dblclick(pick);
select.focus();
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.select = function () {
IPython.Cell.prototype.select.apply(this);
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 // Todo: this dance is needed because as of CodeMirror 2.12, focus is
// not causing the cursor to blink if the editor is empty initially.
// While this seems to fix the issue, this should be fixed
// in CodeMirror proper.
var s = this.code_mirror.getValue();
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 this.code_mirror.focus();
Brian E. Granger
Updating CodeMirror to v 2.12....
r4504 if (s === '') this.code_mirror.setValue('');
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian E. Granger
Ctrl-Enter now does not delete input, but selects it.
r4675 CodeCell.prototype.select_all = function () {
var start = {line: 0, ch: 0};
var nlines = this.code_mirror.lineCount();
var last_line = this.code_mirror.getLine(nlines-1);
var end = {line: nlines-1, ch: last_line.length};
this.code_mirror.setSelection(start, end);
};
Brian E. Granger
Added saving and loading of output of all types.
r4497 CodeCell.prototype.append_output = function (json) {
this.expand();
if (json.output_type === 'pyout') {
this.append_pyout(json);
} else if (json.output_type === 'pyerr') {
this.append_pyerr(json);
} else if (json.output_type === 'display_data') {
this.append_display_data(json);
} else if (json.output_type === 'stream') {
this.append_stream(json);
};
this.outputs.push(json);
};
Brian E. Granger
All output types are not indented.
r4642 CodeCell.prototype.create_output_area = function () {
var oa = $("<div/>").addClass("hbox output_area");
oa.append($('<div/>').addClass('prompt'));
return oa;
};
Brian E. Granger
Added saving and loading of output of all types.
r4497 CodeCell.prototype.append_pyout = function (json) {
n = json.prompt_number || ' ';
Brian E. Granger
All output types are not indented.
r4642 var toinsert = this.create_output_area();
toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
Brian E. Granger
Adding page break logic to the print css....
r4625 this.append_mime_type(json, toinsert);
Brian E. Granger
All output types are not indented.
r4642 this.element.find('div.output').append(toinsert);
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 // If we just output latex, typeset it.
Brian E. Granger
Added saving and loading of output of all types.
r4497 if (json.latex !== undefined) {
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
};
};
Brian E. Granger
Added saving and loading of output of all types.
r4497 CodeCell.prototype.append_pyerr = function (json) {
var tb = json.traceback;
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 if (tb !== undefined && tb.length > 0) {
Brian E. Granger
Finishing display system work....
r4528 var s = '';
var len = tb.length;
for (var i=0; i<len; i++) {
s = s + tb[i] + '\n';
}
s = s + '\n';
Brian E. Granger
All output types are not indented.
r4642 var toinsert = this.create_output_area();
this.append_text(s, toinsert);
this.element.find('div.output').append(toinsert);
Brian E. Granger
Finishing display system work....
r4528 };
Brian E. Granger
Added saving and loading of output of all types.
r4497 };
CodeCell.prototype.append_stream = function (json) {
Brian E. Granger
All output types are not indented.
r4642 var toinsert = this.create_output_area();
this.append_text(json.text, toinsert);
this.element.find('div.output').append(toinsert);
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Added saving and loading of output of all types.
r4497 CodeCell.prototype.append_display_data = function (json) {
Brian E. Granger
All output types are not indented.
r4642 var toinsert = this.create_output_area();
this.append_mime_type(json, toinsert)
this.element.find('div.output').append(toinsert);
Brian E. Granger
Fixing latex rendering bug.
r4553 // If we just output latex, typeset it.
if (json.latex !== undefined) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
};
Brian E. Granger
Added saving and loading of output of all types.
r4497 };
CodeCell.prototype.append_mime_type = function (json, element) {
if (json.html !== undefined) {
Brian E. Granger
All output types are not indented.
r4642 this.append_html(json.html, element);
Brian E. Granger
Added saving and loading of output of all types.
r4497 } else if (json.latex !== undefined) {
Brian E. Granger
All output types are not indented.
r4642 this.append_latex(json.latex, element);
Brian E. Granger
Added saving and loading of output of all types.
r4497 } else if (json.svg !== undefined) {
Brian E. Granger
All output types are not indented.
r4642 this.append_svg(json.svg, element);
Brian E. Granger
Added saving and loading of output of all types.
r4497 } else if (json.png !== undefined) {
Brian E. Granger
All output types are not indented.
r4642 this.append_png(json.png, element);
Brian E. Granger
Finishing display system work....
r4528 } else if (json.jpeg !== undefined) {
Brian E. Granger
All output types are not indented.
r4642 this.append_jpeg(json.jpeg, element);
Brian E. Granger
Added saving and loading of output of all types.
r4497 } else if (json.text !== undefined) {
Brian E. Granger
All output types are not indented.
r4642 this.append_text(json.text, element);
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
};
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
MinRK
support html representations in the notebook frontend...
r4403 CodeCell.prototype.append_html = function (html, element) {
Brian E. Granger
All output types are not indented.
r4642 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_html rendered_html");
MinRK
support html representations in the notebook frontend...
r4403 toinsert.append(html);
element.append(toinsert);
}
Brian E. Granger
Added saving and loading of output of all types.
r4497 CodeCell.prototype.append_text = function (data, element) {
Brian E. Granger
All output types are not indented.
r4642 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_stream");
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 toinsert.append($("<pre/>").html(data));
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 element.append(toinsert);
};
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.append_svg = function (svg, element) {
Brian E. Granger
All output types are not indented.
r4642 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_svg");
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 toinsert.append(svg);
element.append(toinsert);
};
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.append_png = function (png, element) {
Brian E. Granger
All output types are not indented.
r4642 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_png");
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png));
element.append(toinsert);
};
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Finishing display system work....
r4528 CodeCell.prototype.append_jpeg = function (jpeg, element) {
Brian E. Granger
All output types are not indented.
r4642 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_jpeg");
Brian E. Granger
Finishing display system work....
r4528 toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg));
element.append(toinsert);
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.append_latex = function (latex, element) {
// This method cannot do the typesetting because the latex first has to
// be on the page.
Brian E. Granger
All output types are not indented.
r4642 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_latex");
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 toinsert.append(latex);
element.append(toinsert);
}
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.clear_output = function () {
this.element.find("div.output").html("");
Brian E. Granger
Added saving and loading of output of all types.
r4497 this.outputs = [];
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
CTRL-ENTER now runs a cell in "terminal mode"...
r4390 CodeCell.prototype.clear_input = function () {
this.code_mirror.setValue('');
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.collapse = function () {
Brian E. Granger
Added collapsed field to the code cell.
r4533 if (!this.collapsed) {
this.element.find('div.output').hide();
this.collapsed = true;
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.expand = function () {
Brian E. Granger
Added collapsed field to the code cell.
r4533 if (this.collapsed) {
this.element.find('div.output').show();
this.collapsed = false;
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Cell collapse/expand is not called "Toggle".
r4639 CodeCell.prototype.toggle_output = function () {
if (this.collapsed) {
this.expand();
} else {
this.collapse();
};
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.set_input_prompt = function (number) {
var n = number || ' ';
this.input_prompt_number = n
this.element.find('div.input_prompt').html('In&nbsp;[' + n + ']:');
};
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.get_code = function () {
return this.code_mirror.getValue();
};
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.set_code = function (code) {
return this.code_mirror.setValue(code);
};
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.at_top = function () {
var cursor = this.code_mirror.getCursor();
if (cursor.line === 0) {
return true;
} else {
return false;
}
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349 };
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.at_bottom = function () {
var cursor = this.code_mirror.getCursor();
if (cursor.line === (this.code_mirror.lineCount()-1)) {
return true;
} else {
return false;
}
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349 };
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 CodeCell.prototype.fromJSON = function (data) {
Brian E. Granger
Added saving and loading of output of all types.
r4497 // console.log('Import from JSON:', data);
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 if (data.cell_type === 'code') {
Brian E. Granger
Massive work on the notebook document format....
r4484 if (data.input !== undefined) {
this.set_code(data.input);
}
if (data.prompt_number !== undefined) {
this.set_input_prompt(data.prompt_number);
} else {
this.set_input_prompt();
};
Brian E. Granger
Added saving and loading of output of all types.
r4497 var len = data.outputs.length;
for (var i=0; i<len; i++) {
this.append_output(data.outputs[i]);
};
Brian E. Granger
Added collapsed field to the code cell.
r4533 if (data.collapsed !== undefined) {
if (data.collapsed) {
this.collapse();
};
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
};
CodeCell.prototype.toJSON = function () {
Brian E. Granger
Added saving and loading of output of all types.
r4497 var data = {};
Brian E. Granger
Massive work on the notebook document format....
r4484 data.input = this.get_code();
data.cell_type = 'code';
if (this.input_prompt_number !== ' ') {
data.prompt_number = this.input_prompt_number
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian E. Granger
Added saving and loading of output of all types.
r4497 var outputs = [];
var len = this.outputs.length;
for (var i=0; i<len; i++) {
outputs[i] = this.outputs[i];
};
data.outputs = outputs;
Brian E. Granger
Massive work on the notebook document format....
r4484 data.language = 'python';
Brian E. Granger
Added collapsed field to the code cell.
r4533 data.collapsed = this.collapsed;
Brian E. Granger
Added saving and loading of output of all types.
r4497 // console.log('Export to JSON:',data);
Brian E. Granger
Massive work on the notebook document format....
r4484 return data;
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Brian E. Granger
Added saving and loading of output of all types.
r4497
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 IPython.CodeCell = CodeCell;
return IPython;
}(IPython));