|
|
|
|
@ -26,12 +26,13 @@ var IPython = (function (IPython) {
|
|
|
|
|
var Notebook = function (selector, options) {
|
|
|
|
|
var options = options || {};
|
|
|
|
|
this._baseProjectUrl = options.baseProjectUrl;
|
|
|
|
|
|
|
|
|
|
this.notebook_path = options.notebookPath;
|
|
|
|
|
this.notebook_name = options.notebookName;
|
|
|
|
|
this.element = $(selector);
|
|
|
|
|
this.element.scroll();
|
|
|
|
|
this.element.data("notebook", this);
|
|
|
|
|
this.next_prompt_number = 1;
|
|
|
|
|
this.kernel = null;
|
|
|
|
|
this.session = null;
|
|
|
|
|
this.clipboard = null;
|
|
|
|
|
this.undelete_backup = null;
|
|
|
|
|
this.undelete_index = null;
|
|
|
|
|
@ -49,7 +50,6 @@ var IPython = (function (IPython) {
|
|
|
|
|
// single worksheet for now
|
|
|
|
|
this.worksheet_metadata = {};
|
|
|
|
|
this.control_key_active = false;
|
|
|
|
|
this.notebook_id = null;
|
|
|
|
|
this.notebook_name = null;
|
|
|
|
|
this.notebook_name_blacklist_re = /[\/\\:]/;
|
|
|
|
|
this.nbformat = 3 // Increment this when changing the nbformat
|
|
|
|
|
@ -78,6 +78,18 @@ var IPython = (function (IPython) {
|
|
|
|
|
return this._baseProjectUrl || $('body').data('baseProjectUrl');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notebook.prototype.notebookPath = function() {
|
|
|
|
|
var path = $('body').data('notebookPath');
|
|
|
|
|
if (path != 'None') {
|
|
|
|
|
if (path[path.length-1] != '/') {
|
|
|
|
|
path = path.substring(0,path.length);
|
|
|
|
|
};
|
|
|
|
|
return path;
|
|
|
|
|
} else {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an HTML and CSS representation of the notebook.
|
|
|
|
|
*
|
|
|
|
|
@ -745,7 +757,7 @@ var IPython = (function (IPython) {
|
|
|
|
|
var i = this.index_or_selected(index);
|
|
|
|
|
var cell = this.get_selected_cell();
|
|
|
|
|
this.undelete_backup = cell.toJSON();
|
|
|
|
|
$('#undelete_cell').removeClass('disabled');
|
|
|
|
|
$('#undelete_cell').removeClass('ui-state-disabled');
|
|
|
|
|
if (this.is_valid_cell_index(i)) {
|
|
|
|
|
var ce = this.get_cell_element(i);
|
|
|
|
|
ce.remove();
|
|
|
|
|
@ -1028,11 +1040,11 @@ var IPython = (function (IPython) {
|
|
|
|
|
Notebook.prototype.enable_paste = function () {
|
|
|
|
|
var that = this;
|
|
|
|
|
if (!this.paste_enabled) {
|
|
|
|
|
$('#paste_cell_replace').removeClass('disabled')
|
|
|
|
|
$('#paste_cell_replace').removeClass('ui-state-disabled')
|
|
|
|
|
.on('click', function () {that.paste_cell_replace();});
|
|
|
|
|
$('#paste_cell_above').removeClass('disabled')
|
|
|
|
|
$('#paste_cell_above').removeClass('ui-state-disabled')
|
|
|
|
|
.on('click', function () {that.paste_cell_above();});
|
|
|
|
|
$('#paste_cell_below').removeClass('disabled')
|
|
|
|
|
$('#paste_cell_below').removeClass('ui-state-disabled')
|
|
|
|
|
.on('click', function () {that.paste_cell_below();});
|
|
|
|
|
this.paste_enabled = true;
|
|
|
|
|
};
|
|
|
|
|
@ -1045,9 +1057,9 @@ var IPython = (function (IPython) {
|
|
|
|
|
*/
|
|
|
|
|
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');
|
|
|
|
|
$('#paste_cell_replace').addClass('ui-state-disabled').off('click');
|
|
|
|
|
$('#paste_cell_above').addClass('ui-state-disabled').off('click');
|
|
|
|
|
$('#paste_cell_below').addClass('ui-state-disabled').off('click');
|
|
|
|
|
this.paste_enabled = false;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
@ -1146,7 +1158,7 @@ var IPython = (function (IPython) {
|
|
|
|
|
this.undelete_backup = null;
|
|
|
|
|
this.undelete_index = null;
|
|
|
|
|
}
|
|
|
|
|
$('#undelete_cell').addClass('disabled');
|
|
|
|
|
$('#undelete_cell').addClass('ui-state-disabled');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Split/merge
|
|
|
|
|
@ -1370,50 +1382,20 @@ var IPython = (function (IPython) {
|
|
|
|
|
this.get_selected_cell().toggle_line_numbers();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Kernel related things
|
|
|
|
|
// Session related things
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start a new kernel and set it on each code cell.
|
|
|
|
|
* Start a new session and set it on each code cell.
|
|
|
|
|
*
|
|
|
|
|
* @method start_kernel
|
|
|
|
|
* @method start_session
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.start_kernel = function () {
|
|
|
|
|
var base_url = $('body').data('baseKernelUrl') + "kernels";
|
|
|
|
|
this.kernel = new IPython.Kernel(base_url);
|
|
|
|
|
this.kernel.start({notebook: this.notebook_id});
|
|
|
|
|
// Now that the kernel has been created, tell the CodeCells about it.
|
|
|
|
|
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.kernel)
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
Notebook.prototype.start_session = function () {
|
|
|
|
|
var notebook_info = this.notebookPath() + this.notebook_name;
|
|
|
|
|
console.log(notebook_info)
|
|
|
|
|
this.session = new IPython.Session(notebook_info, this);
|
|
|
|
|
this.session.start();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prompt the user to restart the IPython kernel.
|
|
|
|
|
*
|
|
|
|
|
* @method restart_kernel
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.restart_kernel = function () {
|
|
|
|
|
var that = this;
|
|
|
|
|
IPython.dialog.modal({
|
|
|
|
|
title : "Restart kernel or continue running?",
|
|
|
|
|
body : $("<p/>").html(
|
|
|
|
|
'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.kernel.restart();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run the selected cell.
|
|
|
|
|
@ -1548,6 +1530,7 @@ var IPython = (function (IPython) {
|
|
|
|
|
* @param {Object} data JSON representation of a notebook
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.fromJSON = function (data) {
|
|
|
|
|
data = data.content;
|
|
|
|
|
var ncells = this.ncells();
|
|
|
|
|
var i;
|
|
|
|
|
for (i=0; i<ncells; i++) {
|
|
|
|
|
@ -1556,7 +1539,7 @@ var IPython = (function (IPython) {
|
|
|
|
|
};
|
|
|
|
|
// Save the metadata and name.
|
|
|
|
|
this.metadata = data.metadata;
|
|
|
|
|
this.notebook_name = data.metadata.name;
|
|
|
|
|
this.notebook_name = data.metadata.name +'.ipynb';
|
|
|
|
|
// Only handle 1 worksheet for now.
|
|
|
|
|
var worksheet = data.worksheets[0];
|
|
|
|
|
if (worksheet !== undefined) {
|
|
|
|
|
@ -1671,7 +1654,12 @@ var IPython = (function (IPython) {
|
|
|
|
|
error : $.proxy(this.save_notebook_error, this)
|
|
|
|
|
};
|
|
|
|
|
$([IPython.events]).trigger('notebook_saving.Notebook');
|
|
|
|
|
var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
|
|
|
|
|
if (this.notebook_path != "") {
|
|
|
|
|
var url = this.baseProjectUrl() + 'api/notebooks/' + this.notebook_path+ this.notebook_name;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
var url = this.baseProjectUrl() + 'api/notebooks/' +this.notebook_name;
|
|
|
|
|
}
|
|
|
|
|
$.ajax(url, settings);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@ -1730,24 +1718,25 @@ var IPython = (function (IPython) {
|
|
|
|
|
* Request a notebook's data from the server.
|
|
|
|
|
*
|
|
|
|
|
* @method load_notebook
|
|
|
|
|
* @param {String} notebook_id A notebook to load
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.load_notebook = function (notebook_id) {
|
|
|
|
|
var that = this;
|
|
|
|
|
this.notebook_id = notebook_id;
|
|
|
|
|
// We do the call with settings so we can set cache to false.
|
|
|
|
|
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 = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
|
|
|
|
|
$.ajax(url, settings);
|
|
|
|
|
};
|
|
|
|
|
* @param {String} notebook_naem and path A notebook to load
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.load_notebook = function (notebook_name, notebook_path) {
|
|
|
|
|
var that = this;
|
|
|
|
|
this.notebook_name = notebook_name;
|
|
|
|
|
this.notebook_path = notebook_path;
|
|
|
|
|
// We do the call with settings so we can set cache to false.
|
|
|
|
|
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 = this.baseProjectUrl() + 'api/notebooks/' + this.notebook_path + this.notebook_name;
|
|
|
|
|
$.ajax(url, settings);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Success callback for loading a notebook from the server.
|
|
|
|
|
@ -1803,12 +1792,13 @@ var IPython = (function (IPython) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the kernel after the notebook is completely loaded to prevent
|
|
|
|
|
// Create the session after the notebook is completely loaded to prevent
|
|
|
|
|
// code execution upon loading, which is a security risk.
|
|
|
|
|
this.start_kernel();
|
|
|
|
|
if (this.session == null) {
|
|
|
|
|
this.start_session(this.notebook_path);
|
|
|
|
|
}
|
|
|
|
|
// load our checkpoint list
|
|
|
|
|
IPython.notebook.list_checkpoints();
|
|
|
|
|
|
|
|
|
|
$([IPython.events]).trigger('notebook_loaded.Notebook');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@ -1876,14 +1866,19 @@ var IPython = (function (IPython) {
|
|
|
|
|
*
|
|
|
|
|
* @method list_checkpoints
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.list_checkpoints = function () {
|
|
|
|
|
var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints';
|
|
|
|
|
$.get(url).done(
|
|
|
|
|
$.proxy(this.list_checkpoints_success, this)
|
|
|
|
|
).fail(
|
|
|
|
|
$.proxy(this.list_checkpoints_error, this)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
Notebook.prototype.list_checkpoints = function () {
|
|
|
|
|
if (this.notebook_path != "") {
|
|
|
|
|
var url = this.baseProjectUrl() + 'api/notebooks/' + this.notebook_path+ this.notebook_name + '/checkpoints';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
var url = this.baseProjectUrl() + 'api/notebooks/' +this.notebook_name + '/checkpoints';
|
|
|
|
|
}
|
|
|
|
|
$.get(url).done(
|
|
|
|
|
$.proxy(this.list_checkpoints_success, this)
|
|
|
|
|
).fail(
|
|
|
|
|
$.proxy(this.list_checkpoints_error, this)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Success callback for listing checkpoints.
|
|
|
|
|
@ -1921,14 +1916,19 @@ var IPython = (function (IPython) {
|
|
|
|
|
*
|
|
|
|
|
* @method create_checkpoint
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.create_checkpoint = function () {
|
|
|
|
|
var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints';
|
|
|
|
|
$.post(url).done(
|
|
|
|
|
$.proxy(this.create_checkpoint_success, this)
|
|
|
|
|
).fail(
|
|
|
|
|
$.proxy(this.create_checkpoint_error, this)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
Notebook.prototype.create_checkpoint = function () {
|
|
|
|
|
if (this.notebook_path != "") {
|
|
|
|
|
var url = this.baseProjectUrl() + 'api/notebooks/' + this.notebook_path + this.notebook_name + '/checkpoints';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
var url = this.baseProjectUrl() + 'api/notebooks/' +this.notebook_name + '/checkpoints';
|
|
|
|
|
}
|
|
|
|
|
$.post(url).done(
|
|
|
|
|
$.proxy(this.create_checkpoint_success, this)
|
|
|
|
|
).fail(
|
|
|
|
|
$.proxy(this.create_checkpoint_error, this)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Success callback for creating a checkpoint.
|
|
|
|
|
@ -2003,7 +2003,12 @@ var IPython = (function (IPython) {
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.restore_checkpoint = function (checkpoint) {
|
|
|
|
|
$([IPython.events]).trigger('checkpoint_restoring.Notebook', checkpoint);
|
|
|
|
|
var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint;
|
|
|
|
|
if (this.notebook_path != "") {
|
|
|
|
|
var url = this.baseProjectUrl() + 'api/notebooks/' + this.notebook_path + this.notebook_name + '/checkpoints/' + checkpoint;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
var url = this.baseProjectUrl() + 'api/notebooks/' +this.notebook_name + '/checkpoints/' + checkpoint;
|
|
|
|
|
}
|
|
|
|
|
$.post(url).done(
|
|
|
|
|
$.proxy(this.restore_checkpoint_success, this)
|
|
|
|
|
).fail(
|
|
|
|
|
@ -2021,7 +2026,7 @@ var IPython = (function (IPython) {
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) {
|
|
|
|
|
$([IPython.events]).trigger('checkpoint_restored.Notebook');
|
|
|
|
|
this.load_notebook(this.notebook_id);
|
|
|
|
|
this.load_notebook(this.notebook_name, this.notebook_path);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -2043,8 +2048,13 @@ var IPython = (function (IPython) {
|
|
|
|
|
* @param {String} checkpoint ID
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.delete_checkpoint = function (checkpoint) {
|
|
|
|
|
$([IPython.events]).trigger('checkpoint_deleting.Notebook', checkpoint);
|
|
|
|
|
var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint;
|
|
|
|
|
$([IPython.events]).trigger('checkpoint_restoring.Notebook', checkpoint);
|
|
|
|
|
if (this.notebook_path != "") {
|
|
|
|
|
var url = this.baseProjectUrl() + 'api/notebooks/' + this.notebook_path + this.notebook_name + '/checkpoints/' + checkpoint;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
var url = this.baseProjectUrl() + 'api/notebooks/' +this.notebook_name + '/checkpoints/' + checkpoint;
|
|
|
|
|
}
|
|
|
|
|
$.ajax(url, {
|
|
|
|
|
type: 'DELETE',
|
|
|
|
|
success: $.proxy(this.delete_checkpoint_success, this),
|
|
|
|
|
@ -2062,7 +2072,7 @@ var IPython = (function (IPython) {
|
|
|
|
|
*/
|
|
|
|
|
Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) {
|
|
|
|
|
$([IPython.events]).trigger('checkpoint_deleted.Notebook', data);
|
|
|
|
|
this.load_notebook(this.notebook_id);
|
|
|
|
|
this.load_notebook(this.notebook_name, this.notebook_path);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|