var IPython = (function (IPython) {
"use strict";
var utils = IPython.utils;
var Notebook = function (selector, options) {
this.options = options = options || {};
this.base_url = options.base_url;
this.notebook_path = options.notebook_path;
this.notebook_name = options.notebook_name;
this.element = $(selector);
this.element.scroll();
this.element.data("notebook", this);
this.next_prompt_number = 1;
this.session = null;
this.kernel = null;
this.clipboard = null;
this.undelete_backup = null;
this.undelete_index = null;
this.undelete_below = false;
this.paste_enabled = false;
this.mode = 'command';
this.set_dirty(false);
this.metadata = {};
this._checkpoint_after_save = false;
this.last_checkpoint = null;
this.checkpoints = [];
this.autosave_interval = 0;
this.autosave_timer = null;
this.minimum_autosave_interval = 120000;
this.worksheet_metadata = {};
this.notebook_name_blacklist_re = /[\/\\:]/;
this.nbformat = 3;
this.nbformat_minor = 0;
this.style();
this.create_elements();
this.bind_events();
this.save_notebook = function() {
this.save_notebook_error(null, null, "Load failed, save is disabled");
};
};
Notebook.prototype.style = function () {
$('div#notebook').addClass('border-box-sizing');
};
Notebook.prototype.create_elements = function () {
var that = this;
this.element.attr('tabindex','-1');
this.container = $("<div/>").addClass("container").attr("id", "notebook-container");
var end_space = $('<div/>').addClass('end_space');
end_space.dblclick(function (e) {
var ncells = that.ncells();
that.insert_cell_below('code',ncells-1);
});
this.element.append(this.container);
this.container.append(end_space);
};
Notebook.prototype.bind_events = function () {
var that = this;
$([IPython.events]).on('set_next_input.Notebook', function (event, data) {
var index = that.find_cell_index(data.cell);
var new_cell = that.insert_cell_below('code',index);
new_cell.set_text(data.text);
that.dirty = true;
});
$([IPython.events]).on('set_dirty.Notebook', function (event, data) {
that.dirty = data.value;
});
$([IPython.events]).on('trust_changed.Notebook', function (event, data) {
that.trusted = data.value;
});
$([IPython.events]).on('select.Cell', function (event, data) {
var index = that.find_cell_index(data.cell);
that.select(index);
});
$([IPython.events]).on('edit_mode.Cell', function (event, data) {
that.handle_edit_mode(data.cell);
});
$([IPython.events]).on('command_mode.Cell', function (event, data) {
that.handle_command_mode(data.cell);
});
$([IPython.events]).on('status_autorestarting.Kernel', function () {
IPython.dialog.modal({
title: "Kernel Restarting",
body: "The kernel appears to have died. It will restart automatically.",
buttons: {
OK : {
class : "btn-primary"
}
}
});
});
var collapse_time = function (time) {
var app_height = $('#ipython-main-app').height();
var splitter_height = $('div#pager_splitter').outerHeight(true);
var new_height = app_height - splitter_height;
that.element.animate({height : new_height + 'px'}, time);
};
this.element.bind('collapse_pager', function (event, extrap) {
var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast';
collapse_time(time);
});
var expand_time = function (time) {
var app_height = $('#ipython-main-app').height();
var splitter_height = $('div#pager_splitter').outerHeight(true);
var pager_height = $('div#pager').outerHeight(true);
var new_height = app_height - pager_height - splitter_height;
that.element.animate({height : new_height + 'px'}, time);
};
this.element.bind('expand_pager', function (event, extrap) {
var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast';
expand_time(time);
});
window.onbeforeunload = function (e) {
var kill_kernel = false;
if (kill_kernel) {
that.session.kill_kernel();
}
if (that.dirty) {
if ( that.autosave_interval ) {
setTimeout(function () {
if (that.dirty) {
that.save_notebook();
}
}, 1000);
return "Autosave in progress, latest changes may be lost.";
} else {
return "Unsaved changes will be lost.";
}
}
return null;
};
};
Notebook.prototype.set_dirty = function (value) {
if (value === undefined) {
value = true;
}
if (this.dirty == value) {
return;
}
$([IPython.events]).trigger('set_dirty.Notebook', {value: value});
};
Notebook.prototype.scroll_to_cell = function (cell_number, time) {
var cells = this.get_cells();
time = time || 0;
cell_number = Math.min(cells.length-1,cell_number);
cell_number = Math.max(0 ,cell_number);
var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ;
this.element.animate({scrollTop:scroll_value}, time);
return scroll_value;
};
Notebook.prototype.scroll_to_bottom = function () {
this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
};
Notebook.prototype.scroll_to_top = function () {
this.element.animate({scrollTop:0}, 0);
};
Notebook.prototype.edit_metadata = function () {
var that = this;
IPython.dialog.edit_metadata(this.metadata, function (md) {
that.metadata = md;
}, 'Notebook');
};
Notebook.prototype.get_cell_elements = function () {
return this.container.children("div.cell");
};
Notebook.prototype.get_cell_element = function (index) {
var result = null;
var e = this.get_cell_elements().eq(index);
if (e.length !== 0) {
result = e;
}
return result;
};
Notebook.prototype.get_msg_cell = function (msg_id) {
return IPython.CodeCell.msg_cells[msg_id] || null;
};
Notebook.prototype.ncells = function () {
return this.get_cell_elements().length;
};
Notebook.prototype.get_cells = function () {
return this.get_cell_elements().toArray().map(function (e) {
return $(e).data("cell");
});
};
Notebook.prototype.get_cell = function (index) {
var result = null;
var ce = this.get_cell_element(index);
if (ce !== null) {
result = ce.data('cell');
}
return result;
};
Notebook.prototype.get_next_cell = function (cell) {
var result = null;
var index = this.find_cell_index(cell);
if (this.is_valid_cell_index(index+1)) {
result = this.get_cell(index+1);
}
return result;
};
Notebook.prototype.get_prev_cell = function (cell) {
var result = null;
var index = this.find_cell_index(cell);
if (index !== null && index > 1) {
result = this.get_cell(index-1);
}
return result;
};
Notebook.prototype.find_cell_index = function (cell) {
var result = null;
this.get_cell_elements().filter(function (index) {
if ($(this).data("cell") === cell) {
result = index;
}
});
return result;
};
Notebook.prototype.index_or_selected = function (index) {
var i;
if (index === undefined || index === null) {
i = this.get_selected_index();
if (i === null) {
i = 0;
}
} else {
i = index;
}
return i;
};
Notebook.prototype.get_selected_cell = function () {
var index = this.get_selected_index();
return this.get_cell(index);
};
Notebook.prototype.is_valid_cell_index = function (index) {
if (index !== null && index >= 0 && index < this.ncells()) {
return true;
} else {
return false;
}
};
Notebook.prototype.get_selected_index = function () {
var result = null;
this.get_cell_elements().filter(function (index) {
if ($(this).data("cell").selected === true) {
result = index;
}
});
return result;
};
Notebook.prototype.select = function (index) {
if (this.is_valid_cell_index(index)) {
var sindex = this.get_selected_index();
if (sindex !== null && index !== sindex) {
if (this.mode !== 'command') {
this.command_mode();
}
this.get_cell(sindex).unselect();
}
var cell = this.get_cell(index);
cell.select();
if (cell.cell_type === 'heading') {
$([IPython.events]).trigger('selected_cell_type_changed.Notebook',
{'cell_type':cell.cell_type,level:cell.level}
);
} else {
$([IPython.events]).trigger('selected_cell_type_changed.Notebook',
{'cell_type':cell.cell_type}
);
}
}
return this;
};
Notebook.prototype.select_next = function () {
var index = this.get_selected_index();
this.select(index+1);
return this;
};
Notebook.prototype.select_prev = function () {
var index = this.get_selected_index();
this.select(index-1);
return this;
};
Notebook.prototype.get_edit_index = function () {
var result = null;
this.get_cell_elements().filter(function (index) {
if ($(this).data("cell").mode === 'edit') {
result = index;
}
});
return result;
};
Notebook.prototype.handle_command_mode = function (cell) {
if (this.mode !== 'command') {
cell.command_mode();
this.mode = 'command';
$([IPython.events]).trigger('command_mode.Notebook');
IPython.keyboard_manager.command_mode();
}
};
Notebook.prototype.command_mode = function () {
var cell = this.get_cell(this.get_edit_index());
if (cell && this.mode !== 'command') {
cell.focus_cell();
}
};
Notebook.prototype.handle_edit_mode = function (cell) {
if (cell && this.mode !== 'edit') {
cell.edit_mode();
this.mode = 'edit';
$([IPython.events]).trigger('edit_mode.Notebook');
IPython.keyboard_manager.edit_mode();
}
};
Notebook.prototype.edit_mode = function () {
var cell = this.get_selected_cell();
if (cell && this.mode !== 'edit') {
cell.unrender();
cell.focus_editor();
}
};
Notebook.prototype.focus_cell = function () {
var cell = this.get_selected_cell();
if (cell === null) {return;}
cell.focus_cell();
};
Notebook.prototype.move_cell_up = function (index) {
var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i) && i > 0) {
var pivot = this.get_cell_element(i-1);
var tomove = this.get_cell_element(i);
if (pivot !== null && tomove !== null) {
tomove.detach();
pivot.before(tomove);
this.select(i-1);
var cell = this.get_selected_cell();
cell.focus_cell();
}
this.set_dirty(true);
}
return this;
};
Notebook.prototype.move_cell_down = function (index) {
var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
var pivot = this.get_cell_element(i+1);
var tomove = this.get_cell_element(i);
if (pivot !== null && tomove !== null) {
tomove.detach();
pivot.after(tomove);
this.select(i+1);
var cell = this.get_selected_cell();
cell.focus_cell();
}
}
this.set_dirty();
return this;
};
Notebook.prototype.delete_cell = function (index) {
var i = this.index_or_selected(index);
var cell = this.get_selected_cell();
this.undelete_backup = cell.toJSON();
$('#undelete_cell').removeClass('disabled');
if (this.is_valid_cell_index(i)) {
var old_ncells = this.ncells();
var ce = this.get_cell_element(i);
ce.remove();
if (i === 0) {
if (old_ncells === 1) {
this.insert_cell_below('code');
}
this.select(0);
this.undelete_index = 0;
this.undelete_below = false;
} else if (i === old_ncells-1 && i !== 0) {
this.select(i-1);
this.undelete_index = i - 1;
this.undelete_below = true;
} else {
this.select(i);
this.undelete_index = i;
this.undelete_below = false;
}
$([IPython.events]).trigger('delete.Cell', {'cell': cell, 'index': i});
this.set_dirty(true);
}
return this;
};
Notebook.prototype.undelete_cell = function() {
if (this.undelete_backup !== null && this.undelete_index !== null) {
var current_index = this.get_selected_index();
if (this.undelete_index < current_index) {
current_index = current_index + 1;
}
if (this.undelete_index >= this.ncells()) {
this.select(this.ncells() - 1);
}
else {
this.select(this.undelete_index);
}
var cell_data = this.undelete_backup;
var new_cell = null;
if (this.undelete_below) {
new_cell = this.insert_cell_below(cell_data.cell_type);
} else {
new_cell = this.insert_cell_above(cell_data.cell_type);
}
new_cell.fromJSON(cell_data);
if (this.undelete_below) {
this.select(current_index+1);
} else {
this.select(current_index);
}
this.undelete_backup = null;
this.undelete_index = null;
}
$('#undelete_cell').addClass('disabled');
};
Notebook.prototype.insert_cell_at_index = function(type, index){
var ncells = this.ncells();
index = Math.min(index,ncells);
index = Math.max(index,0);
var cell = null;
if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
if (type === 'code') {
cell = new IPython.CodeCell(this.kernel);
cell.set_input_prompt();
} else if (type === 'markdown') {
cell = new IPython.MarkdownCell();
} else if (type === 'raw') {
cell = new IPython.RawCell();
} else if (type === 'heading') {
cell = new IPython.HeadingCell();
}
if(this._insert_element_at_index(cell.element,index)) {
cell.render();
$([IPython.events]).trigger('create.Cell', {'cell': cell, 'index': index});
cell.refresh();
this.set_dirty(true);
}
}
return cell;
};
Notebook.prototype._insert_element_at_index = function(element, index){
if (element === undefined){
return false;
}
var ncells = this.ncells();
if (ncells === 0) {
this.element.find('div.end_space').before(element);
} else if ( ncells === index ) {
this.get_cell_element(index-1).after(element);
} else if (this.is_valid_cell_index(index)) {
this.get_cell_element(index).before(element);
} else {
return false;
}
if (this.undelete_index !== null && index <= this.undelete_index) {
this.undelete_index = this.undelete_index + 1;
this.set_dirty(true);
}
return true;
};
Notebook.prototype.insert_cell_above = function (type, index) {
index = this.index_or_selected(index);
return this.insert_cell_at_index(type, index);
};
Notebook.prototype.insert_cell_below = function (type, index) {
index = this.index_or_selected(index);
return this.insert_cell_at_index(type, index+1);
};
Notebook.prototype.insert_cell_at_bottom = function (type){
var len = this.ncells();
return this.insert_cell_below(type,len-1);
};
Notebook.prototype.to_code = function (index) {
var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i)) {
var source_element = this.get_cell_element(i);
var source_cell = source_element.data("cell");
if (!(source_cell instanceof IPython.CodeCell)) {
var target_cell = this.insert_cell_below('code',i);
var text = source_cell.get_text();
if (text === source_cell.placeholder) {
text = '';
}
target_cell.set_text(text);
target_cell.code_mirror.clearHistory();
source_element.remove();
this.select(i);
this.set_dirty(true);
}
}
};
Notebook.prototype.to_markdown = function (index) {
var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i)) {
var source_element = this.get_cell_element(i);
var source_cell = source_element.data("cell");
if (!(source_cell instanceof IPython.MarkdownCell)) {
var target_cell = this.insert_cell_below('markdown',i);
var text = source_cell.get_text();
if (text === source_cell.placeholder) {
text = '';
}
target_cell.unrender();
target_cell.set_text(text);
target_cell.code_mirror.clearHistory();
source_element.remove();
this.select(i);
if ((source_cell instanceof IPython.TextCell) && source_cell.rendered) {
target_cell.render();
}
this.set_dirty(true);
}
}
};
Notebook.prototype.to_raw = function (index) {
var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i)) {
var source_element = this.get_cell_element(i);
var source_cell = source_element.data("cell");
var target_cell = null;
if (!(source_cell instanceof IPython.RawCell)) {
target_cell = this.insert_cell_below('raw',i);
var text = source_cell.get_text();
if (text === source_cell.placeholder) {
text = '';
}
target_cell.unrender();
target_cell.set_text(text);
target_cell.code_mirror.clearHistory();
source_element.remove();
this.select(i);
this.set_dirty(true);
}
}
};
Notebook.prototype.to_heading = function (index, level) {
level = level || 1;
var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i)) {
var source_element = this.get_cell_element(i);
var source_cell = source_element.data("cell");
var target_cell = null;
if (source_cell instanceof IPython.HeadingCell) {
source_cell.set_level(level);
} else {
target_cell = this.insert_cell_below('heading',i);
var text = source_cell.get_text();
if (text === source_cell.placeholder) {
text = '';
}
target_cell.set_level(level);
target_cell.unrender();
target_cell.set_text(text);
target_cell.code_mirror.clearHistory();
source_element.remove();
this.select(i);
if ((source_cell instanceof IPython.TextCell) && source_cell.rendered) {
target_cell.render();
}
}
this.set_dirty(true);
$([IPython.events]).trigger('selected_cell_type_changed.Notebook',
{'cell_type':'heading',level:level}
);
}
};
Notebook.prototype.enable_paste = function () {
var that = this;
if (!this.paste_enabled) {
$('#paste_cell_replace').removeClass('disabled')
.on('click', function () {that.paste_cell_replace();});
$('#paste_cell_above').removeClass('disabled')
.on('click', function () {that.paste_cell_above();});
$('#paste_cell_below').removeClass('disabled')
.on('click', function () {that.paste_cell_below();});
this.paste_enabled = true;
}
};
Notebook.prototype.disable_paste = function () {
if (this.paste_enabled) {
$('#paste_cell_replace').addClass('disabled').off('click');
$('#paste_cell_above').addClass('disabled').off('click');
$('#paste_cell_below').addClass('disabled').off('click');
this.paste_enabled = false;
}
};
Notebook.prototype.cut_cell = function () {
this.copy_cell();
this.delete_cell();
};
Notebook.prototype.copy_cell = function () {
var cell = this.get_selected_cell();
this.clipboard = cell.toJSON();
this.enable_paste();
};
Notebook.prototype.paste_cell_replace = function () {
if (this.clipboard !== null && this.paste_enabled) {
var cell_data = this.clipboard;
var new_cell = this.insert_cell_above(cell_data.cell_type);
new_cell.fromJSON(cell_data);
var old_cell = this.get_next_cell(new_cell);
this.delete_cell(this.find_cell_index(old_cell));
this.select(this.find_cell_index(new_cell));
}
};
Notebook.prototype.paste_cell_above = function () {
if (this.clipboard !== null && this.paste_enabled) {
var cell_data = this.clipboard;
var new_cell = this.insert_cell_above(cell_data.cell_type);
new_cell.fromJSON(cell_data);
new_cell.focus_cell();
}
};
Notebook.prototype.paste_cell_below = function () {
if (this.clipboard !== null && this.paste_enabled) {
var cell_data = this.clipboard;
var new_cell = this.insert_cell_below(cell_data.cell_type);
new_cell.fromJSON(cell_data);
new_cell.focus_cell();
}
};
Notebook.prototype.split_cell = function () {
var mdc = IPython.MarkdownCell;
var rc = IPython.RawCell;
var cell = this.get_selected_cell();
if (cell.is_splittable()) {
var texta = cell.get_pre_cursor();
var textb = cell.get_post_cursor();
if (cell instanceof IPython.CodeCell) {
cell.set_text(textb);
var new_cell = this.insert_cell_above('code');
new_cell.set_text(texta);
} else if ((cell instanceof mdc && !cell.rendered) || (cell instanceof rc)) {
cell.set_text(textb);
var new_cell = this.insert_cell_above(cell.cell_type);
new_cell.unrender();
new_cell.set_text(texta);
}
}
};
Notebook.prototype.merge_cell_above = function () {
var mdc = IPython.MarkdownCell;
var rc = IPython.RawCell;
var index = this.get_selected_index();
var cell = this.get_cell(index);
var render = cell.rendered;
if (!cell.is_mergeable()) {
return;
}
if (index > 0) {
var upper_cell = this.get_cell(index-1);
if (!upper_cell.is_mergeable()) {
return;
}
var upper_text = upper_cell.get_text();
var text = cell.get_text();
if (cell instanceof IPython.CodeCell) {
cell.set_text(upper_text+'\n'+text);
} else if ((cell instanceof mdc) || (cell instanceof rc)) {
cell.unrender();
cell.set_text(upper_text+'\n\n'+text);
if (render) {
cell.render();
}
}
this.delete_cell(index-1);
this.select(this.find_cell_index(cell));
}
};
Notebook.prototype.merge_cell_below = function () {
var mdc = IPython.MarkdownCell;
var rc = IPython.RawCell;
var index = this.get_selected_index();
var cell = this.get_cell(index);
var render = cell.rendered;
if (!cell.is_mergeable()) {
return;
}
if (index < this.ncells()-1) {
var lower_cell = this.get_cell(index+1);
if (!lower_cell.is_mergeable()) {
return;
}
var lower_text = lower_cell.get_text();
var text = cell.get_text();
if (cell instanceof IPython.CodeCell) {
cell.set_text(text+'\n'+lower_text);
} else if ((cell instanceof mdc) || (cell instanceof rc)) {
cell.unrender();
cell.set_text(text+'\n\n'+lower_text);
if (render) {
cell.render();
}
}
this.delete_cell(index+1);
this.select(this.find_cell_index(cell));
}
};
Notebook.prototype.collapse_output = function (index) {
var i = this.index_or_selected(index);
var cell = this.get_cell(i);
if (cell !== null && (cell instanceof IPython.CodeCell)) {
cell.collapse_output();
this.set_dirty(true);
}
};
Notebook.prototype.collapse_all_output = function () {
$.map(this.get_cells(), function (cell, i) {
if (cell instanceof IPython.CodeCell) {
cell.collapse_output();
}
});
this.set_dirty(true);
};
Notebook.prototype.expand_output = function (index) {
var i = this.index_or_selected(index);
var cell = this.get_cell(i);
if (cell !== null && (cell instanceof IPython.CodeCell)) {
cell.expand_output();
this.set_dirty(true);
}
};
Notebook.prototype.expand_all_output = function () {
$.map(this.get_cells(), function (cell, i) {
if (cell instanceof IPython.CodeCell) {
cell.expand_output();
}
});
this.set_dirty(true);
};
Notebook.prototype.clear_output = function (index) {
var i = this.index_or_selected(index);
var cell = this.get_cell(i);
if (cell !== null && (cell instanceof IPython.CodeCell)) {
cell.clear_output();
this.set_dirty(true);
}
};
Notebook.prototype.clear_all_output = function () {
$.map(this.get_cells(), function (cell, i) {
if (cell instanceof IPython.CodeCell) {
cell.clear_output();
}
});
this.set_dirty(true);
};
Notebook.prototype.scroll_output = function (index) {
var i = this.index_or_selected(index);
var cell = this.get_cell(i);
if (cell !== null && (cell instanceof IPython.CodeCell)) {
cell.scroll_output();
this.set_dirty(true);
}
};
Notebook.prototype.scroll_all_output = function () {
$.map(this.get_cells(), function (cell, i) {
if (cell instanceof IPython.CodeCell) {
cell.scroll_output();
}
});
this.set_dirty(true);
};
Notebook.prototype.toggle_output = function (index) {
var i = this.index_or_selected(index);
var cell = this.get_cell(i);
if (cell !== null && (cell instanceof IPython.CodeCell)) {
cell.toggle_output();
this.set_dirty(true);
}
};
Notebook.prototype.toggle_all_output = function () {
$.map(this.get_cells(), function (cell, i) {
if (cell instanceof IPython.CodeCell) {
cell.toggle_output();
}
});
this.set_dirty(true);
};
Notebook.prototype.toggle_output_scroll = function (index) {
var i = this.index_or_selected(index);
var cell = this.get_cell(i);
if (cell !== null && (cell instanceof IPython.CodeCell)) {
cell.toggle_output_scroll();
this.set_dirty(true);
}
};
Notebook.prototype.toggle_all_output_scroll = function () {
$.map(this.get_cells(), function (cell, i) {
if (cell instanceof IPython.CodeCell) {
cell.toggle_output_scroll();
}
});
this.set_dirty(true);
};
Notebook.prototype.cell_toggle_line_numbers = function() {
this.get_selected_cell().toggle_line_numbers();
};
Notebook.prototype.start_session = function () {
this.session = new IPython.Session(this, this.options);
this.session.start($.proxy(this._session_started, this));
};
Notebook.prototype._session_started = function(){
this.kernel = this.session.kernel;
var ncells = this.ncells();
for (var i=0; i<ncells; i++) {
var cell = this.get_cell(i);
if (cell instanceof IPython.CodeCell) {
cell.set_kernel(this.session.kernel);
}
}
};
Notebook.prototype.restart_kernel = function () {
var that = this;
IPython.dialog.modal({
title : "Restart kernel or continue running?",
body : $("<p/>").text(
'Do you want to restart the current kernel? You will lose all variables defined in it.'
),
buttons : {
"Continue running" : {},
"Restart" : {
"class" : "btn-danger",
"click" : function() {
that.session.restart_kernel();
}
}
}
});
};
Notebook.prototype.execute_cell = function () {
var cell = this.get_selected_cell();
var cell_index = this.find_cell_index(cell);
cell.execute();
this.command_mode();
this.set_dirty(true);
};
Notebook.prototype.execute_cell_and_insert_below = function () {
var cell = this.get_selected_cell();
var cell_index = this.find_cell_index(cell);
cell.execute();
if (cell_index === (this.ncells()-1)) {
this.command_mode();
this.insert_cell_below('code');
this.select(cell_index+1);
this.edit_mode();
this.scroll_to_bottom();
this.set_dirty(true);
return;
}
this.command_mode();
this.insert_cell_below('code');
this.select(cell_index+1);
this.edit_mode();
this.set_dirty(true);
};
Notebook.prototype.execute_cell_and_select_below = function () {
var cell = this.get_selected_cell();
var cell_index = this.find_cell_index(cell);
cell.execute();
if (cell_index === (this.ncells()-1)) {
this.command_mode();
this.insert_cell_below('code');
this.select(cell_index+1);
this.edit_mode();
this.scroll_to_bottom();
this.set_dirty(true);
return;
}
this.command_mode();
this.select(cell_index+1);
this.focus_cell();
this.set_dirty(true);
};
Notebook.prototype.execute_cells_below = function () {
this.execute_cell_range(this.get_selected_index(), this.ncells());
this.scroll_to_bottom();
};
Notebook.prototype.execute_cells_above = function () {
this.execute_cell_range(0, this.get_selected_index());
};
Notebook.prototype.execute_all_cells = function () {
this.execute_cell_range(0, this.ncells());
this.scroll_to_bottom();
};
Notebook.prototype.execute_cell_range = function (start, end) {
this.command_mode();
for (var i=start; i<end; i++) {
this.select(i);
this.execute_cell();
}
};
Notebook.prototype.get_notebook_name = function () {
var nbname = this.notebook_name.substring(0,this.notebook_name.length-6);
return nbname;
};
Notebook.prototype.set_notebook_name = function (name) {
this.notebook_name = name;
};
Notebook.prototype.test_notebook_name = function (nbname) {
nbname = nbname || '';
if (nbname.length>0 && !this.notebook_name_blacklist_re.test(nbname)) {
return true;
} else {
return false;
}
};
Notebook.prototype.fromJSON = function (data) {
var content = data.content;
var ncells = this.ncells();
var i;
for (i=0; i<ncells; i++) {
this.delete_cell(0);
}
this.metadata = content.metadata;
this.notebook_name = data.name;
var trusted = true;
var worksheet = content.worksheets[0];
if (worksheet !== undefined) {
if (worksheet.metadata) {
this.worksheet_metadata = worksheet.metadata;
}
var new_cells = worksheet.cells;
ncells = new_cells.length;
var cell_data = null;
var new_cell = null;
for (i=0; i<ncells; i++) {
cell_data = new_cells[i];
if (cell_data.cell_type === 'plaintext'){
cell_data.cell_type = 'raw';
}
new_cell = this.insert_cell_at_index(cell_data.cell_type, i);
new_cell.fromJSON(cell_data);
if (new_cell.cell_type == 'code' && !new_cell.output_area.trusted) {
trusted = false;
}
}
}
if (trusted != this.trusted) {
this.trusted = trusted;
$([IPython.events]).trigger("trust_changed.Notebook", trusted);
}
if (content.worksheets.length > 1) {
IPython.dialog.modal({
title : "Multiple worksheets",
body : "This notebook has " + data.worksheets.length + " worksheets, " +
"but this version of IPython can only handle the first. " +
"If you save this notebook, worksheets after the first will be lost.",
buttons : {
OK : {
class : "btn-danger"
}
}
});
}
};
Notebook.prototype.toJSON = function () {
var cells = this.get_cells();
var ncells = cells.length;
var cell_array = new Array(ncells);
var trusted = true;
for (var i=0; i<ncells; i++) {
var cell = cells[i];
if (cell.cell_type == 'code' && !cell.output_area.trusted) {
trusted = false;
}
cell_array[i] = cell.toJSON();
}
var data = {
worksheets : [{
cells: cell_array,
metadata: this.worksheet_metadata
}],
metadata : this.metadata
};
if (trusted != this.trusted) {
this.trusted = trusted;
$([IPython.events]).trigger("trust_changed.Notebook", trusted);
}
return data;
};
Notebook.prototype.set_autosave_interval = function (interval) {
var that = this;
if (this.autosave_timer) {
clearInterval(this.autosave_timer);
}
this.autosave_interval = this.minimum_autosave_interval = interval;
if (interval) {
this.autosave_timer = setInterval(function() {
if (that.dirty) {
that.save_notebook();
}
}, interval);
$([IPython.events]).trigger("autosave_enabled.Notebook", interval);
} else {
this.autosave_timer = null;
$([IPython.events]).trigger("autosave_disabled.Notebook");
}
};
Notebook.prototype.save_notebook = function (extra_settings) {
var model = {};
model.name = this.notebook_name;
model.path = this.notebook_path;
model.content = this.toJSON();
model.content.nbformat = this.nbformat;
model.content.nbformat_minor = this.nbformat_minor;
var start = new Date().getTime();
var settings = {
processData : false,
cache : false,
type : "PUT",
data : JSON.stringify(model),
headers : {'Content-Type': 'application/json'},
success : $.proxy(this.save_notebook_success, this, start),
error : $.proxy(this.save_notebook_error, this)
};
if (extra_settings) {
for (var key in extra_settings) {
settings[key] = extra_settings[key];
}
}
$([IPython.events]).trigger('notebook_saving.Notebook');
var url = utils.url_join_encode(
this.base_url,
'api/notebooks',
this.notebook_path,
this.notebook_name
);
$.ajax(url, settings);
};
Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
this.set_dirty(false);
$([IPython.events]).trigger('notebook_saved.Notebook');
this._update_autosave_interval(start);
if (this._checkpoint_after_save) {
this.create_checkpoint();
this._checkpoint_after_save = false;
}
};
Notebook.prototype._update_autosave_interval = function (start) {
var duration = (new Date().getTime() - start);
if (this.autosave_interval) {
var interval = Math.max(10 * duration, this.minimum_autosave_interval);
interval = 10000 * Math.round(interval / 10000);
if (interval != this.autosave_interval) {
this.set_autosave_interval(interval);
}
}
};
Notebook.prototype.save_notebook_error = function (xhr, status, error) {
$([IPython.events]).trigger('notebook_save_failed.Notebook', [xhr, status, error]);
};
Notebook.prototype.trust_notebook = function (extra_settings) {
var body = $("<div>").append($("<p>")
.text("A trusted IPython notebook may execute hidden malicious code ")
.append($("<strong>")
.append(
$("<em>").text("when you open it")
)
).append(".").append(
" Selecting trust will immediately reload this notebook in a trusted state."
).append(
" For more information, see the "
).append($("<a>").attr("href", "http://ipython.org/ipython-doc/2/notebook/security.html")
.text("IPython security documentation")
).append(".")
);
var nb = this;
IPython.dialog.modal({
title: "Trust this notebook?",
body: body,
buttons: {
Cancel : {},
Trust : {
class : "btn-danger",
click : function () {
var cells = nb.get_cells();
for (var i = 0; i < cells.length; i++) {
var cell = cells[i];
if (cell.cell_type == 'code') {
cell.output_area.trusted = true;
}
}
$([IPython.events]).on('notebook_saved.Notebook', function () {
window.location.reload();
});
nb.save_notebook();
}
}
}
});
};
Notebook.prototype.new_notebook = function(){
var path = this.notebook_path;
var base_url = this.base_url;
var settings = {
processData : false,
cache : false,
type : "POST",
dataType : "json",
async : false,
success : function (data, status, xhr){
var notebook_name = data.name;
window.open(
utils.url_join_encode(
base_url,
'notebooks',
path,
notebook_name
),
'_blank'
);
},
error : utils.log_ajax_error,
};
var url = utils.url_join_encode(
base_url,
'api/notebooks',
path
);
$.ajax(url,settings);
};
Notebook.prototype.copy_notebook = function(){
var path = this.notebook_path;
var base_url = this.base_url;
var settings = {
processData : false,
cache : false,
type : "POST",
dataType : "json",
data : JSON.stringify({copy_from : this.notebook_name}),
async : false,
success : function (data, status, xhr) {
window.open(utils.url_join_encode(
base_url,
'notebooks',
data.path,
data.name
), '_blank');
},
error : utils.log_ajax_error,
};
var url = utils.url_join_encode(
base_url,
'api/notebooks',
path
);
$.ajax(url,settings);
};
Notebook.prototype.rename = function (nbname) {
var that = this;
if (!nbname.match(/\.ipynb$/)) {
nbname = nbname + ".ipynb";
}
var data = {name: nbname};
var settings = {
processData : false,
cache : false,
type : "PATCH",
data : JSON.stringify(data),
dataType: "json",
headers : {'Content-Type': 'application/json'},
success : $.proxy(that.rename_success, this),
error : $.proxy(that.rename_error, this)
};
$([IPython.events]).trigger('rename_notebook.Notebook', data);
var url = utils.url_join_encode(
this.base_url,
'api/notebooks',
this.notebook_path,
this.notebook_name
);
$.ajax(url, settings);
};
Notebook.prototype.delete = function () {
var that = this;
var settings = {
processData : false,
cache : false,
type : "DELETE",
dataType: "json",
error : utils.log_ajax_error,
};
var url = utils.url_join_encode(
this.base_url,
'api/notebooks',
this.notebook_path,
this.notebook_name
);
$.ajax(url, settings);
};
Notebook.prototype.rename_success = function (json, status, xhr) {
var name = this.notebook_name = json.name;
var path = json.path;
this.session.rename_notebook(name, path);
$([IPython.events]).trigger('notebook_renamed.Notebook', json);
};
Notebook.prototype.rename_error = function (xhr, status, error) {
var that = this;
var dialog = $('<div/>').append(
$("<p/>").addClass("rename-message")
.text('This notebook name already exists.')
);
$([IPython.events]).trigger('notebook_rename_failed.Notebook', [xhr, status, error]);
IPython.dialog.modal({
title: "Notebook Rename Error!",
body: dialog,
buttons : {
"Cancel": {},
"OK": {
class: "btn-primary",
click: function () {
IPython.save_widget.rename_notebook();
}}
},
open : function (event, ui) {
var that = $(this);
that.find('input[type="text"]').keydown(function (event, ui) {
if (event.which === IPython.keyboard.keycodes.enter) {
that.find('.btn-primary').first().click();
}
});
that.find('input[type="text"]').focus();
}
});
};
Notebook.prototype.load_notebook = function (notebook_name, notebook_path) {
var that = this;
this.notebook_name = notebook_name;
this.notebook_path = notebook_path;
var settings = {
processData : false,
cache : false,
type : "GET",
dataType : "json",
success : $.proxy(this.load_notebook_success,this),
error : $.proxy(this.load_notebook_error,this),
};
$([IPython.events]).trigger('notebook_loading.Notebook');
var url = utils.url_join_encode(
this.base_url,
'api/notebooks',
this.notebook_path,
this.notebook_name
);
$.ajax(url, settings);
};
Notebook.prototype.load_notebook_success = function (data, status, xhr) {
this.fromJSON(data);
if (this.ncells() === 0) {
this.insert_cell_below('code');
this.edit_mode(0);
} else {
this.select(0);
this.handle_command_mode(this.get_cell(0));
}
this.set_dirty(false);
this.scroll_to_top();
if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
var msg = "This notebook has been converted from an older " +
"notebook format (v"+data.orig_nbformat+") to the current notebook " +
"format (v"+data.nbformat+"). The next time you save this notebook, the " +
"newer notebook format will be used and older versions of IPython " +
"may not be able to read it. To keep the older version, close the " +
"notebook without saving it.";
IPython.dialog.modal({
title : "Notebook converted",
body : msg,
buttons : {
OK : {
class : "btn-primary"
}
}
});
} else if (data.orig_nbformat_minor !== undefined && data.nbformat_minor !== data.orig_nbformat_minor) {
var that = this;
var orig_vs = 'v' + data.nbformat + '.' + data.orig_nbformat_minor;
var this_vs = 'v' + data.nbformat + '.' + this.nbformat_minor;
var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
this_vs + ". You can still work with this notebook, but some features " +
"introduced in later notebook versions may not be available.";
IPython.dialog.modal({
title : "Newer Notebook",
body : msg,
buttons : {
OK : {
class : "btn-danger"
}
}
});
}
if (this.session === null) {
this.start_session();
}
this.list_checkpoints();
if (this.metadata.celltoolbar) {
IPython.CellToolbar.global_show();
IPython.CellToolbar.activate_preset(this.metadata.celltoolbar);
} else {
IPython.CellToolbar.global_hide();
}
delete(this.save_notebook);
$([IPython.events]).trigger('notebook_loaded.Notebook');
};
Notebook.prototype.load_notebook_error = function (xhr, status, error) {
$([IPython.events]).trigger('notebook_load_failed.Notebook', [xhr, status, error]);
utils.log_ajax_error(xhr, status, error);
var msg = $("<div>");
if (xhr.status === 400) {
msg.text(utils.ajax_error_msg(xhr));
} else if (xhr.status === 500) {
msg.text("An unknown error occurred while loading this notebook. " +
"This version can load notebook formats " +
"v" + this.nbformat + " or earlier. See the server log for details.");
}
IPython.dialog.modal({
title: "Error loading notebook",
body : msg,
buttons : {
"OK": {}
}
});
};
Notebook.prototype.save_checkpoint = function () {
this._checkpoint_after_save = true;
this.save_notebook();
};
Notebook.prototype.add_checkpoint = function (checkpoint) {
var found = false;
for (var i = 0; i < this.checkpoints.length; i++) {
var existing = this.checkpoints[i];
if (existing.id == checkpoint.id) {
found = true;
this.checkpoints[i] = checkpoint;
break;
}
}
if (!found) {
this.checkpoints.push(checkpoint);
}
this.last_checkpoint = this.checkpoints[this.checkpoints.length - 1];
};
Notebook.prototype.list_checkpoints = function () {
var url = utils.url_join_encode(
this.base_url,
'api/notebooks',
this.notebook_path,
this.notebook_name,
'checkpoints'
);
$.get(url).done(
$.proxy(this.list_checkpoints_success, this)
).fail(
$.proxy(this.list_checkpoints_error, this)
);
};
Notebook.prototype.list_checkpoints_success = function (data, status, xhr) {
data = $.parseJSON(data);
this.checkpoints = data;
if (data.length) {
this.last_checkpoint = data[data.length - 1];
} else {
this.last_checkpoint = null;
}
$([IPython.events]).trigger('checkpoints_listed.Notebook', [data]);
};
Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) {
$([IPython.events]).trigger('list_checkpoints_failed.Notebook');
};
Notebook.prototype.create_checkpoint = function () {
var url = utils.url_join_encode(
this.base_url,
'api/notebooks',
this.notebook_path,
this.notebook_name,
'checkpoints'
);
$.post(url).done(
$.proxy(this.create_checkpoint_success, this)
).fail(
$.proxy(this.create_checkpoint_error, this)
);
};
Notebook.prototype.create_checkpoint_success = function (data, status, xhr) {
data = $.parseJSON(data);
this.add_checkpoint(data);
$([IPython.events]).trigger('checkpoint_created.Notebook', data);
};
Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) {
$([IPython.events]).trigger('checkpoint_failed.Notebook');
};
Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) {
var that = this;
checkpoint = checkpoint || this.last_checkpoint;
if ( ! checkpoint ) {
console.log("restore dialog, but no checkpoint to restore to!");
return;
}
var body = $('<div/>').append(
$('<p/>').addClass("p-space").text(
"Are you sure you want to revert the notebook to " +
"the latest checkpoint?"
).append(
$("<strong/>").text(
" This cannot be undone."
)
)
).append(
$('<p/>').addClass("p-space").text("The checkpoint was last updated at:")
).append(
$('<p/>').addClass("p-space").text(
Date(checkpoint.last_modified)
).css("text-align", "center")
);
IPython.dialog.modal({
title : "Revert notebook to checkpoint",
body : body,
buttons : {
Revert : {
class : "btn-danger",
click : function () {
that.restore_checkpoint(checkpoint.id);
}
},
Cancel : {}
}
});
};
Notebook.prototype.restore_checkpoint = function (checkpoint) {
$([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
var url = utils.url_join_encode(
this.base_url,
'api/notebooks',
this.notebook_path,
this.notebook_name,
'checkpoints',
checkpoint
);
$.post(url).done(
$.proxy(this.restore_checkpoint_success, this)
).fail(
$.proxy(this.restore_checkpoint_error, this)
);
};
Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) {
$([IPython.events]).trigger('checkpoint_restored.Notebook');
this.load_notebook(this.notebook_name, this.notebook_path);
};
Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) {
$([IPython.events]).trigger('checkpoint_restore_failed.Notebook');
};
Notebook.prototype.delete_checkpoint = function (checkpoint) {
$([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
var url = utils.url_join_encode(
this.base_url,
'api/notebooks',
this.notebook_path,
this.notebook_name,
'checkpoints',
checkpoint
);
$.ajax(url, {
type: 'DELETE',
success: $.proxy(this.delete_checkpoint_success, this),
error: $.proxy(this.delete_checkpoint_error, this)
});
};
Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) {
$([IPython.events]).trigger('checkpoint_deleted.Notebook', data);
this.load_notebook(this.notebook_name, this.notebook_path);
};
Notebook.prototype.delete_checkpoint_error = function (xhr, status, error) {
$([IPython.events]).trigger('checkpoint_delete_failed.Notebook', [xhr, status, error]);
};
IPython.Notebook = Notebook;
return IPython;
}(IPython));