Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A node.js kernel for IPython notebook. You can see the explanation of the ipynb rendered in http://nbviewer.ipython.org
zmq = require("zmq")
fs = require("fs")
var config = JSON.parse(fs.readFileSync(process.argv[2]))
var connexion = "tcp://"+config.ip+":"
var shell_conn = connexion+config.shell_port
var pub_conn = connexion+config.iopub_port
var hb_conn = connexion+config.hb_port
var util = require('util'),
vm = require('vm'),
initSandbox = {},
context = vm.createContext(initSandbox);
var hb_socket = zmq.createSocket('rep');
hb_socket.bind(hb_conn)
hb_socket.on('message',
function(data){
console.log("wtf ?");
hb_socket.send(data);
});
var pub_socket = zmq.createSocket('pub');
pub_socket.bind(pub_conn);
var reply_socket = zmq.createSocket('xrep')
reply_socket.bind(shell_conn)
reply_socket.on('message',
function(data){
for(i in arguments){
console.log("["+i+"]: "+arguments[i].toString())
}
var parent_header = JSON.parse(arguments[3].toString());
var unparsed_content = arguments[6];
if(unparsed_content != undefined ) {
var content = JSON.parse(unparsed_content.toString());
}
var code = content?content.code:undefined;
var result
if(code != undefined){
result = vm.runInContext(code , context, '<kernel>');
} else {
result = 'undefined'
}
var header_reply ={
msg_id:1,
session:parent_header.session,
msg_type:"execute_reply",
}
var ident = "";
var delim = "<IDS|MSG>"
var signature = ""
var metadata = {}
var content = JSON.stringify({
execution_count:1,
data:{
"text/plain":result?result.toString():"undefined"
}
})
var header_pub ={
msg_id:1,
session:parent_header.session,
msg_type:"pyout",
}
pub_socket.send([
ident,
delim,
signature,
JSON.stringify(header_pub),
JSON.stringify(parent_header),
JSON.stringify(metadata),
content])
reply_socket.send([
ident,
delim,
signature,
JSON.stringify(header_reply),
JSON.stringify(parent_header),
JSON.stringify(metadata),
content
]);
})
reply_socket.on('error',
function(data){
console.log('error',data)
})
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

I have played a bit with IPython (1.1.0 installed with anaconda) and ZMQ and almost immediately ran into:

[2014-01-14 21:26:12.657] [DEBUG] app - startKernel: /Users/mksenzov/.ipython/profile_node.js/security/kernel-28cd2951-6587-4a7b-b892-8294faf1cf43.json
[2014-01-14 21:26:12.662] [DEBUG] app - config: {"stdin_port":59633,"ip":"127.0.0.1","control_port":59634,"hb_port":59635,"signature_scheme":"hmac-sha256","key":"","shell_port":59631,"transport":"tcp","iopub_port":59632}
[2014-01-14 21:26:12.664] [DEBUG] zmq_sockets - createSocket({ type: 'xrep', protocol: 'tcp', ip: '127.0.0.1', port: 59635 })
[2014-01-14 21:26:12.664] [INFO] zmq_sockets - create socket on tcp://127.0.0.1:59635
[2014-01-14 21:26:12.665] [INFO] zmq_sockets - created
[2014-01-14 21:26:12.665] [DEBUG] zmq_sockets - createSocket({ type: 'router', protocol: 'tcp', ip: '127.0.0.1', port: 59631 })
[2014-01-14 21:26:12.665] [INFO] zmq_sockets - create socket on tcp://127.0.0.1:59631
[2014-01-14 21:26:12.665] [INFO] zmq_sockets - created
2014-01-14 21:26:12.778 [NotebookApp] CRITICAL | Malformed message: ['']
Traceback (most recent call last):
File "/Users/mksenzov/anaconda/lib/python2.7/site-packages/IPython/html/base/zmqhandlers.py", line 68, in _on_zmq_reply
msg = self._reserialize_reply(msg_list)
File "/Users/mksenzov/anaconda/lib/python2.7/site-packages/IPython/html/base/zmqhandlers.py", line 50, in _reserialize_reply
idents, msg_list = self.session.feed_identities(msg_list)
File "/Users/mksenzov/anaconda/lib/python2.7/site-packages/IPython/kernel/zmq/session.py", line 722, in feed_identities
idx = msg_list.index(DELIM)
ValueError: '<IDS|MSG>' is not in list
2014-01-14 21:26:15.574 [NotebookApp] Polling kernel...
2014-01-14 21:26:18.574 [NotebookApp] Polling kernel...

So I figured I must be doing smth wrong and found this gist, well it has the same issues: malformed messages and execution results not being reflected:

2014-01-14 21:23:34.070 [NotebookApp] Kernel args: {'extra_arguments': [u'--debug', u"--IPKernelApp.parent_appname='ipython-notebook'", u'--profile-dir', u'/Users/mksenzov/.ipython/profile_node.js'], 'cwd': u'/Users/mksenzov/INode'}
debugger listening on port 5858
2014-01-14 21:23:34.298 [NotebookApp] Connecting to: tcp://127.0.0.1:59388
2014-01-14 21:23:34.299 [NotebookApp] CRITICAL | Malformed message: ['']
Traceback (most recent call last):
File "/Users/mksenzov/anaconda/lib/python2.7/site-packages/IPython/html/base/zmqhandlers.py", line 68, in _on_zmq_reply
msg = self._reserialize_reply(msg_list)
File "/Users/mksenzov/anaconda/lib/python2.7/site-packages/IPython/html/base/zmqhandlers.py", line 50, in _reserialize_reply
idents, msg_list = self.session.feed_identities(msg_list)
File "/Users/mksenzov/anaconda/lib/python2.7/site-packages/IPython/kernel/zmq/session.py", line 722, in feed_identities
idx = msg_list.index(DELIM)
ValueError: '<IDS|MSG>' is not in list

Here is my gist: https://gist.github.com/mksenzov/8429770

I tried changing the version of ZMQ bindings for node.js to no effect... I guess the next step is to add debug on the IPython side and see what it is doing right before connecting to the kernel, but from the looks of the log on the kernel side - there is not much going on.

Am I missing something obvious?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment