Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A basic IPython comm example.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"application/javascript": [
"// Put this in a file accessible to the server and delete 'mymodule'\n",
"// so the define call is define([], function() {...\n",
"define('mymodule', [], function() {\n",
" var mytarget = function (comm, msg) {\n",
" console.log('starting comm', comm, msg); \n",
" comm.on_msg(function(m) {\n",
" console.log('message', m);\n",
" comm.send(['some reply data', m.content.data])\n",
" });\n",
" comm.on_close(function(m) {\n",
" console.log('close', m)\n",
" })\n",
" }\n",
" return {'mytarget': mytarget}\n",
"})"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%javascript\n",
"// Put this in a file accessible to the server and delete 'mymodule'\n",
"// so the define call is define([], function() {...\n",
"define('mymodule', [], function() {\n",
" var mytarget = function (comm, msg) {\n",
" console.log('starting comm', comm, msg); \n",
" comm.on_msg(function(m) {\n",
" console.log('message', m);\n",
" comm.send(['some reply data', m.content.data])\n",
" });\n",
" comm.on_close(function(m) {\n",
" console.log('close', m)\n",
" })\n",
" }\n",
" return {'mytarget': mytarget}\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from ipykernel.comm import Comm\n",
"msgs = []\n",
"def on_msg(msg):\n",
" msgs.append(msg)\n",
"\n",
"c=Comm(target_name='mytarget', target_module='mymodule', data={'some': 'data'})\n",
"c.on_msg(on_msg)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"c.send(['some', 'more', 'data'])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"c.close(['some', 'closing', 'data'])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[{'buffers': [],\n",
" 'content': {u'comm_id': u'bc90f39cd6ab4716aea5e01245623b50',\n",
" u'data': [u'some reply data', [u'some', u'more', u'data']]},\n",
" 'header': {u'msg_id': u'8CA47C9899D94E378F9EB5C2595F7CA5',\n",
" u'msg_type': u'comm_msg',\n",
" u'session': u'46A5D8C39F8B4A17BB6EBBA7B0A8D46D',\n",
" u'username': u'username',\n",
" u'version': u'5.0'},\n",
" 'metadata': {},\n",
" 'msg_id': u'8CA47C9899D94E378F9EB5C2595F7CA5',\n",
" 'msg_type': u'comm_msg',\n",
" 'parent_header': {}}]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"msgs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.