mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-30 12:11:32 +08:00
refactor js callbacks
all callbacks get the whole message
This commit is contained in:
parent
9206d1e87f
commit
d99e25f245
@ -244,12 +244,22 @@ var IPython = (function (IPython) {
|
||||
this.set_input_prompt('*');
|
||||
this.element.addClass("running");
|
||||
var callbacks = {
|
||||
'execute_reply': $.proxy(this._handle_execute_reply, this),
|
||||
'output': $.proxy(this.output_area.handle_output, this.output_area),
|
||||
'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
|
||||
'set_next_input': $.proxy(this._handle_set_next_input, this),
|
||||
'input_request': $.proxy(this._handle_input_request, this)
|
||||
};
|
||||
shell : {
|
||||
reply : $.proxy(this._handle_execute_reply, this),
|
||||
payload : {
|
||||
set_next_input : $.proxy(this._handle_set_next_input, this),
|
||||
page : function (payload) {
|
||||
$([IPython.events]).trigger('open_with_text.Pager', payload);
|
||||
}
|
||||
}
|
||||
},
|
||||
iopub : {
|
||||
output : $.proxy(this.output_area.handle_output, this.output_area),
|
||||
clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
|
||||
},
|
||||
input : $.proxy(this._handle_input_request, this)
|
||||
}
|
||||
|
||||
this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
|
||||
};
|
||||
|
||||
@ -257,8 +267,8 @@ var IPython = (function (IPython) {
|
||||
* @method _handle_execute_reply
|
||||
* @private
|
||||
*/
|
||||
CodeCell.prototype._handle_execute_reply = function (content) {
|
||||
this.set_input_prompt(content.execution_count);
|
||||
CodeCell.prototype._handle_execute_reply = function (msg) {
|
||||
this.set_input_prompt(msg.content.execution_count);
|
||||
this.element.removeClass("running");
|
||||
$([IPython.events]).trigger('set_dirty.Notebook', {value: true});
|
||||
}
|
||||
@ -267,8 +277,8 @@ var IPython = (function (IPython) {
|
||||
* @method _handle_set_next_input
|
||||
* @private
|
||||
*/
|
||||
CodeCell.prototype._handle_set_next_input = function (text) {
|
||||
var data = {'cell': this, 'text': text}
|
||||
CodeCell.prototype._handle_set_next_input = function (payload) {
|
||||
var data = {'cell': this, 'text': payload.text}
|
||||
$([IPython.events]).trigger('set_next_input.Notebook', data);
|
||||
}
|
||||
|
||||
@ -276,8 +286,8 @@ var IPython = (function (IPython) {
|
||||
* @method _handle_input_request
|
||||
* @private
|
||||
*/
|
||||
CodeCell.prototype._handle_input_request = function (content) {
|
||||
this.output_area.append_raw_input(content);
|
||||
CodeCell.prototype._handle_input_request = function (msg) {
|
||||
this.output_area.append_raw_input(msg);
|
||||
}
|
||||
|
||||
|
||||
@ -438,4 +448,4 @@ var IPython = (function (IPython) {
|
||||
IPython.CodeCell = CodeCell;
|
||||
|
||||
return IPython;
|
||||
}(IPython));
|
||||
}(IPython));
|
||||
|
@ -150,16 +150,17 @@ var IPython = (function (IPython) {
|
||||
matched_text: ""
|
||||
})
|
||||
} else {
|
||||
var callbacks = {
|
||||
'complete_reply': $.proxy(this.finish_completing, this)
|
||||
};
|
||||
var callbacks = { shell : {
|
||||
reply: $.proxy(this.finish_completing, this)
|
||||
}};
|
||||
this.cell.kernel.complete(line, cur.ch, callbacks);
|
||||
}
|
||||
};
|
||||
|
||||
Completer.prototype.finish_completing = function (content) {
|
||||
Completer.prototype.finish_completing = function (msg) {
|
||||
// let's build a function that wrap all that stuff into what is needed
|
||||
// for the new completer:
|
||||
var content = msg.content;
|
||||
var matched_text = content.matched_text;
|
||||
var matches = content.matches;
|
||||
|
||||
|
@ -231,9 +231,10 @@ var IPython = (function (IPython) {
|
||||
};
|
||||
|
||||
|
||||
OutputArea.prototype.handle_output = function (msg_type, content) {
|
||||
OutputArea.prototype.handle_output = function (msg) {
|
||||
var json = {};
|
||||
json.output_type = msg_type;
|
||||
var msg_type = json.output_type = msg.header.msg_type;
|
||||
var content = msg.content;
|
||||
if (msg_type === "stream") {
|
||||
json.text = content.data;
|
||||
json.stream = content.name;
|
||||
@ -564,9 +565,10 @@ var IPython = (function (IPython) {
|
||||
element.append(toinsert);
|
||||
};
|
||||
|
||||
OutputArea.prototype.append_raw_input = function (content) {
|
||||
OutputArea.prototype.append_raw_input = function (msg) {
|
||||
var that = this;
|
||||
this.expand();
|
||||
var content = msg.content;
|
||||
var area = this.create_output_area();
|
||||
|
||||
// disable any other raw_inputs, if they are left around
|
||||
@ -618,8 +620,8 @@ var IPython = (function (IPython) {
|
||||
}
|
||||
|
||||
|
||||
OutputArea.prototype.handle_clear_output = function (content) {
|
||||
this.clear_output(content.wait);
|
||||
OutputArea.prototype.handle_clear_output = function (msg) {
|
||||
this.clear_output(msg.content.wait);
|
||||
};
|
||||
|
||||
|
||||
|
@ -222,12 +222,12 @@ var IPython = (function (IPython) {
|
||||
|
||||
|
||||
Tooltip.prototype._request_tooltip = function (cell, line) {
|
||||
var callbacks = {
|
||||
'object_info_reply': $.proxy(this._show, this)
|
||||
}
|
||||
var callbacks = { shell : {
|
||||
reply : $.proxy(this._show, this)
|
||||
}};
|
||||
var oir_token = this.extract_oir_token(line);
|
||||
var msg_id = cell.kernel.object_info_request(oir_token, callbacks);
|
||||
}
|
||||
};
|
||||
|
||||
// make an imediate completion request
|
||||
Tooltip.prototype.request = function (cell, hide_if_no_docstring) {
|
||||
@ -301,7 +301,8 @@ var IPython = (function (IPython) {
|
||||
Tooltip.prototype._show = function (reply) {
|
||||
// move the bubble if it is not hidden
|
||||
// otherwise fade it
|
||||
this.name = reply.name;
|
||||
var content = reply.content;
|
||||
this.name = content.name;
|
||||
|
||||
// do some math to have the tooltip arrow on more or less on left or right
|
||||
// width of the editor
|
||||
@ -334,20 +335,20 @@ var IPython = (function (IPython) {
|
||||
});
|
||||
|
||||
// build docstring
|
||||
var defstring = reply.call_def;
|
||||
var defstring = content.call_def;
|
||||
if (defstring == null) {
|
||||
defstring = reply.init_definition;
|
||||
defstring = content.init_definition;
|
||||
}
|
||||
if (defstring == null) {
|
||||
defstring = reply.definition;
|
||||
defstring = content.definition;
|
||||
}
|
||||
|
||||
var docstring = reply.call_docstring;
|
||||
var docstring = content.call_docstring;
|
||||
if (docstring == null) {
|
||||
docstring = reply.init_docstring;
|
||||
docstring = content.init_docstring;
|
||||
}
|
||||
if (docstring == null) {
|
||||
docstring = reply.docstring;
|
||||
docstring = content.docstring;
|
||||
}
|
||||
|
||||
if (docstring == null) {
|
||||
|
@ -239,7 +239,7 @@ var IPython = (function (IPython) {
|
||||
this.shell_channel.send(JSON.stringify(msg));
|
||||
this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
|
||||
return msg.header.msg_id;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get info on object asynchronoulsy
|
||||
@ -340,7 +340,7 @@ var IPython = (function (IPython) {
|
||||
allow_stdin : false
|
||||
};
|
||||
callbacks = callbacks || {};
|
||||
if (callbacks.input_request !== undefined) {
|
||||
if (callbacks.input !== undefined) {
|
||||
content.allow_stdin = true;
|
||||
}
|
||||
$.extend(true, content, options);
|
||||
@ -431,11 +431,22 @@ var IPython = (function (IPython) {
|
||||
delete this._msg_callbacks[msg_id];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* Set callbacks for a particular message.
|
||||
* Callbacks should be a struct of the following form:
|
||||
* shell : {
|
||||
*
|
||||
* }
|
||||
|
||||
*/
|
||||
Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
|
||||
if (callbacks) {
|
||||
this._msg_callbacks[msg_id] = callbacks;
|
||||
// shallow-copy mapping, because we will modify it at the top level
|
||||
var cbcopy = this._msg_callbacks[msg_id] = {};
|
||||
cbcopy.shell = callbacks.shell;
|
||||
cbcopy.iopub = callbacks.iopub;
|
||||
cbcopy.input = callbacks.input;
|
||||
this._msg_callbacks[msg_id] = cbcopy;
|
||||
}
|
||||
};
|
||||
|
||||
@ -443,37 +454,40 @@ var IPython = (function (IPython) {
|
||||
Kernel.prototype._handle_shell_reply = function (e) {
|
||||
var reply = $.parseJSON(e.data);
|
||||
$([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
|
||||
var header = reply.header;
|
||||
var content = reply.content;
|
||||
var metadata = reply.metadata;
|
||||
var msg_type = header.msg_type;
|
||||
var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id);
|
||||
if (callbacks !== undefined) {
|
||||
var cb = callbacks[msg_type];
|
||||
if (cb !== undefined) {
|
||||
cb(content, metadata);
|
||||
}
|
||||
var parent_id = reply.parent_header.msg_id;
|
||||
var callbacks = this.get_callbacks_for_msg(parent_id);
|
||||
if (!callbacks || !callbacks.shell) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (content.payload !== undefined) {
|
||||
var payload = content.payload || [];
|
||||
this._handle_payload(callbacks, payload);
|
||||
var shell_callbacks = callbacks.shell;
|
||||
|
||||
// clear callbacks on shell
|
||||
delete callbacks.shell;
|
||||
delete callbacks.input;
|
||||
if (!callbacks.iopub) {
|
||||
this.clear_callbacks_for_msg(parent_id);
|
||||
}
|
||||
|
||||
if (shell_callbacks.reply !== undefined) {
|
||||
shell_callbacks.reply(reply);
|
||||
}
|
||||
if (content.payload && shell_callbacks.payload) {
|
||||
this._handle_payloads(content.payload, shell_callbacks.payload, reply);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Kernel.prototype._handle_payload = function (callbacks, payload) {
|
||||
var l = payload.length;
|
||||
Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
|
||||
var l = payloads.length;
|
||||
// Payloads are handled by triggering events because we don't want the Kernel
|
||||
// to depend on the Notebook or Pager classes.
|
||||
for (var i=0; i<l; i++) {
|
||||
if (payload[i].source === 'page') {
|
||||
var data = {'text':payload[i].text};
|
||||
$([IPython.events]).trigger('open_with_text.Pager', data);
|
||||
} else if (payload[i].source === 'set_next_input') {
|
||||
if (callbacks.set_next_input !== undefined) {
|
||||
callbacks.set_next_input(payload[i].text);
|
||||
}
|
||||
var payload = payloads[i];
|
||||
var callback = payload_callbacks[payload.source];
|
||||
if (callback) {
|
||||
callback(payload, msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -483,6 +497,17 @@ var IPython = (function (IPython) {
|
||||
if (execution_state === 'busy') {
|
||||
$([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
|
||||
} else if (execution_state === 'idle') {
|
||||
// clear callbacks
|
||||
var parent_id = msg.parent_header.msg_id;
|
||||
var callbacks = this.get_callbacks_for_msg(parent_id);
|
||||
if (callbacks !== undefined) {
|
||||
delete callbacks.iopub;
|
||||
delete callbacks.input;
|
||||
if (!callbacks.shell) {
|
||||
this.clear_callbacks_for_msg(parent_id);
|
||||
}
|
||||
}
|
||||
|
||||
$([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
|
||||
} else if (execution_state === 'restarting') {
|
||||
// autorestarting is distinct from restarting,
|
||||
@ -501,12 +526,12 @@ var IPython = (function (IPython) {
|
||||
// handle clear_output message
|
||||
Kernel.prototype._handle_clear_output = function (msg) {
|
||||
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
|
||||
if (callbacks === undefined) {
|
||||
if (!callbacks || !callbacks.iopub) {
|
||||
return;
|
||||
}
|
||||
var callback = callbacks['clear_output'];
|
||||
if (callback !== undefined) {
|
||||
callback(msg.content, msg.metadata);
|
||||
var callback = callbacks.clear_output;
|
||||
if (callback) {
|
||||
callback(msg);
|
||||
}
|
||||
};
|
||||
|
||||
@ -514,12 +539,12 @@ var IPython = (function (IPython) {
|
||||
// handle an output message (pyout, display_data, etc.)
|
||||
Kernel.prototype._handle_output_message = function (msg) {
|
||||
var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
|
||||
if (callbacks === undefined) {
|
||||
if (!callbacks || !callbacks.iopub) {
|
||||
return;
|
||||
}
|
||||
var callback = callbacks['output'];
|
||||
if (callback !== undefined) {
|
||||
callback(msg.header.msg_type, msg.content, msg.metadata);
|
||||
var callback = callbacks.iopub.output;
|
||||
if (callback) {
|
||||
callback(msg);
|
||||
}
|
||||
};
|
||||
|
||||
@ -546,10 +571,9 @@ var IPython = (function (IPython) {
|
||||
return;
|
||||
}
|
||||
var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
|
||||
if (callbacks !== undefined) {
|
||||
var cb = callbacks[msg_type];
|
||||
if (cb !== undefined) {
|
||||
cb(content, metadata);
|
||||
if (callbacks) {
|
||||
if (callbacks.input) {
|
||||
callbacks.input(request);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user