mirror of
https://github.com/jupyter/notebook.git
synced 2024-12-27 04:20:22 +08:00
Cells shouldn't know about Sessions
This commit is contained in:
parent
7ea311ad64
commit
0558ec4ce0
@ -60,8 +60,8 @@ var IPython = (function (IPython) {
|
||||
* @param {object|undefined} [options]
|
||||
* @param [options.cm_config] {object} config to pass to CodeMirror
|
||||
*/
|
||||
var CodeCell = function (session, options) {
|
||||
this.session = session || null;
|
||||
var CodeCell = function (kernel, options) {
|
||||
this.kernel = kernel || null;
|
||||
this.code_mirror = null;
|
||||
this.input_prompt_number = null;
|
||||
this.collapsed = false;
|
||||
@ -231,8 +231,8 @@ var IPython = (function (IPython) {
|
||||
|
||||
// Kernel related calls.
|
||||
|
||||
CodeCell.prototype.set_session = function (session) {
|
||||
this.session = session;
|
||||
CodeCell.prototype.set_kernel = function (kernel) {
|
||||
this.kernel = kernel;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -250,7 +250,7 @@ var IPython = (function (IPython) {
|
||||
'set_next_input': $.proxy(this._handle_set_next_input, this),
|
||||
'input_request': $.proxy(this._handle_input_request, this)
|
||||
};
|
||||
this.last_msg_id = this.session.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
|
||||
this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -153,7 +153,7 @@ var IPython = (function (IPython) {
|
||||
var callbacks = {
|
||||
'complete_reply': $.proxy(this.finish_completing, this)
|
||||
};
|
||||
this.cell.session.kernel.complete(line, cur.ch, callbacks);
|
||||
this.cell.kernel.complete(line, cur.ch, callbacks);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -33,6 +33,7 @@ var IPython = (function (IPython) {
|
||||
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;
|
||||
@ -797,7 +798,7 @@ var IPython = (function (IPython) {
|
||||
|
||||
if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
|
||||
if (type === 'code') {
|
||||
cell = new IPython.CodeCell(this.session);
|
||||
cell = new IPython.CodeCell(this.kernel);
|
||||
cell.set_input_prompt();
|
||||
} else if (type === 'markdown') {
|
||||
cell = new IPython.MarkdownCell();
|
||||
@ -1390,21 +1391,21 @@ var IPython = (function (IPython) {
|
||||
*/
|
||||
Notebook.prototype.start_session = function () {
|
||||
this.session = new IPython.Session(this.notebook_name, this.notebook_path, this);
|
||||
this.session.start();
|
||||
this.link_cells_to_session();
|
||||
this.session.start($.proxy(this._session_started, this));
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Once a session is started, link the code cells to the session
|
||||
* Once a session is started, link the code cells to the kernel
|
||||
*
|
||||
*/
|
||||
Notebook.prototype.link_cells_to_session= function(){
|
||||
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_session(this.session);
|
||||
cell.set_kernel(this.session.kernel);
|
||||
};
|
||||
};
|
||||
};
|
||||
@ -1806,21 +1807,20 @@ var IPython = (function (IPython) {
|
||||
$.ajax(url,settings);
|
||||
};
|
||||
|
||||
Notebook.prototype.notebook_rename = function (nbname) {
|
||||
Notebook.prototype.rename = function (nbname) {
|
||||
var that = this;
|
||||
var new_name = nbname + '.ipynb'
|
||||
var name = {'name': new_name};
|
||||
var data = {name: nbname + '.ipynb'};
|
||||
var settings = {
|
||||
processData : false,
|
||||
cache : false,
|
||||
type : "PATCH",
|
||||
data : JSON.stringify(name),
|
||||
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('notebook_rename.Notebook');
|
||||
$([IPython.events]).trigger('rename_notebook.Notebook', data);
|
||||
var url = utils.url_path_join(
|
||||
this.baseProjectUrl(),
|
||||
'api/notebooks',
|
||||
@ -1835,8 +1835,8 @@ var IPython = (function (IPython) {
|
||||
this.notebook_name = json.name
|
||||
var name = this.notebook_name
|
||||
var path = json.path
|
||||
this.session.notebook_rename(name, path);
|
||||
$([IPython.events]).trigger('notebook_renamed.Notebook');
|
||||
this.session.rename_notebook(name, path);
|
||||
$([IPython.events]).trigger('notebook_renamed.Notebook', json);
|
||||
}
|
||||
|
||||
Notebook.prototype.rename_error = function (json, status, xhr) {
|
||||
|
@ -95,7 +95,7 @@ var IPython = (function (IPython) {
|
||||
);
|
||||
return false;
|
||||
} else {
|
||||
IPython.notebook.notebook_rename(new_name);
|
||||
IPython.notebook.rename(new_name);
|
||||
}
|
||||
}}
|
||||
},
|
||||
|
@ -128,7 +128,7 @@ var IPython = (function (IPython) {
|
||||
// reexecute last call in pager by appending ? to show back in pager
|
||||
var that = this;
|
||||
var empty = function () {};
|
||||
cell.session.kernel.execute(
|
||||
cell.kernel.execute(
|
||||
that.name + '?', {
|
||||
'execute_reply': empty,
|
||||
'output': empty,
|
||||
@ -226,7 +226,7 @@ var IPython = (function (IPython) {
|
||||
'object_info_reply': $.proxy(this._show, this)
|
||||
}
|
||||
var oir_token = this.extract_oir_token(line);
|
||||
var msg_id = cell.session.kernel.object_info_request(oir_token, callbacks);
|
||||
var msg_id = cell.kernel.object_info_request(oir_token, callbacks);
|
||||
}
|
||||
|
||||
// make an imediate completion request
|
||||
|
@ -23,7 +23,7 @@ var IPython = (function (IPython) {
|
||||
this._baseProjectUrl = notebook.baseProjectUrl();
|
||||
};
|
||||
|
||||
Session.prototype.start = function() {
|
||||
Session.prototype.start = function(callback) {
|
||||
var that = this;
|
||||
var model = {
|
||||
notebook : {
|
||||
@ -37,13 +37,18 @@ var IPython = (function (IPython) {
|
||||
type : "POST",
|
||||
data: JSON.stringify(model),
|
||||
dataType : "json",
|
||||
success : $.proxy(this.start_kernel, that),
|
||||
success : function (data, status, xhr) {
|
||||
that._handle_start_success(data);
|
||||
if (callback) {
|
||||
callback(data, status, xhr);
|
||||
}
|
||||
},
|
||||
};
|
||||
var url = utils.url_path_join(this._baseProjectUrl, 'api/sessions');
|
||||
$.ajax(url, settings);
|
||||
};
|
||||
|
||||
Session.prototype.notebook_rename = function (name, path) {
|
||||
Session.prototype.rename_notebook = function (name, path) {
|
||||
this.name = name;
|
||||
this.path = path;
|
||||
var model = {
|
||||
@ -63,7 +68,7 @@ var IPython = (function (IPython) {
|
||||
$.ajax(url, settings);
|
||||
};
|
||||
|
||||
Session.prototype.delete_session = function() {
|
||||
Session.prototype.delete = function() {
|
||||
var settings = {
|
||||
processData : false,
|
||||
cache : false,
|
||||
@ -76,16 +81,15 @@ var IPython = (function (IPython) {
|
||||
|
||||
// Kernel related things
|
||||
/**
|
||||
* Start a new kernel and set it on each code cell.
|
||||
* Create the Kernel object associated with this Session.
|
||||
*
|
||||
* @method start_kernel
|
||||
* @method _handle_start_success
|
||||
*/
|
||||
Session.prototype.start_kernel = function (json) {
|
||||
this.id = json.id;
|
||||
this.kernel_content = json.kernel;
|
||||
Session.prototype._handle_start_success = function (data, status, xhr) {
|
||||
this.id = data.id;
|
||||
var base_url = utils.url_path_join($('body').data('baseKernelUrl'), "api/kernels");
|
||||
this.kernel = new IPython.Kernel(base_url, this.session_id);
|
||||
this.kernel._kernel_started(this.kernel_content);
|
||||
this.kernel = new IPython.Kernel(base_url);
|
||||
this.kernel._kernel_started(data.kernel);
|
||||
};
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user