Skip to content

Instantly share code, notes, and snippets.

@ivanov
Created July 10, 2014 15:19
Show Gist options
  • Save ivanov/4cc9c3dd79d1c69a49c1 to your computer and use it in GitHub Desktop.
Save ivanov/4cc9c3dd79d1c69a49c1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"worksheets": [
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "Summary of the talk:"
},
{
"metadata": {},
"cell_type": "heading",
"source": "Build your own light saber!\n\n",
"level": 1
},
{
"metadata": {},
"cell_type": "markdown",
"source": "![](files/yoda.jpg)\n\nPhrase lifted from an excellent talk by Sam Aaron\n\n[Zen and the Art of Live Programming](http://www.infoq.com/presentations/Live-Programming)\n\n(I got there via [this post from Tom Ballinger](http://ballingt.com/2013/12/21/bpython-curtsies.html), thanks Tom!)"
},
{
"metadata": {},
"cell_type": "heading",
"source": "vim-ipython\n",
"level": 1
},
{
"metadata": {},
"cell_type": "markdown",
"source": "http://github.com/ivanov/vim-ipython"
},
{
"metadata": {},
"cell_type": "heading",
"source": "SciPy 2011",
"level": 1
},
{
"metadata": {},
"cell_type": "markdown",
"source": "![](/files/scipy2011.jpg)\n<!-- ![](/files/dwf.png) --> "
},
{
"metadata": {},
"cell_type": "heading",
"source": " ",
"level": 1
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The way to get something done, sit down and talk during the sprints"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "![](/files/2011.jpg)"
},
{
"metadata": {},
"cell_type": "heading",
"source": "with the help of Min Ragan-Kelley",
"level": 1
},
{
"metadata": {},
"cell_type": "markdown",
"source": "![](files/minrk.png)"
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "markdown",
"source": "\nHow? IPython prto\n\n![protocol](files/protocol.png)\n\nMore details in this talk:\n\n**The IPython protocol, frontends and kernels. **\n\nPaul Ivanov and Thomas Kluyver, PyData SV 2014\n\n( [video](https://www.youtube.com/watch?v=QPimD4gL0ak) ) ( \n[slides](http://nbviewer.ipython.org/github/ipython/front-to-back/blob/master/Frontends.ipynb) )"
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"prompt_number": 28,
"outputs": [
{
"output_type": "display_data",
"javascript": "require([\"/static/components/codemirror/keymap/emacs.js\"],\n function (emacs) { \n to('emacs'); \n console.log('emacs.js loaded'); \n });",
"metadata": {},
"text": "<IPython.core.display.Javascript at 0x7f1f11976950>"
}
],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "markdown",
"source": "We use CodeMirror in IPython. CodeMirror supports both `vim` and `emacs` keyboard shortcuts, but you have to enable them. \nHere's how you do that in the notebook."
},
{
"metadata": {},
"cell_type": "code",
"input": "%%javascript\n// change the mode of all current and future CodeMirror instances\nfunction to(mode) {\n var mode = mode || 'vim'\n // first let's apply vim mode to all current cells\n function to_mode(c) { return c.code_mirror.setOption('keyMap', mode);};\n IPython.notebook.get_cells().map(to_mode);\n // apply the mode to future cells created\n IPython.Cell.options_default.cm_config.keyMap = mode;\n}\n\nrequire([\"/static/components/codemirror/keymap/vim.js\"],\n function (vim) { \n to('vim'); \n console.log('emacs.js loaded'); \n });",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "What's coming:\n\nvim-ipython:\n editing notebooks",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "heading",
"source": "So go forth and build your own lightsaber!",
"level": 1
},
{
"metadata": {},
"cell_type": "markdown",
"source": "![](files/saber.jpg)"
},
{
"metadata": {},
"cell_type": "heading",
"source": "Emacs users?!",
"level": 2
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Sam Aaron's [Zen and the Art of Live Programming](http://www.infoq.com/presentations/Live-Programming)\n\nJohn Kitchin's [Emacs + org-mode + python in reproducible research](https://www.youtube.com/watch?v=1-dUkyn_fZA) (SciPy 2013)\n\nJohn has a dovetailing follow-up talk this year: \"A success story in using Python in a graduate chemical engineering course.\" This room 2:15 p.m.–2:45 p.m."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "We use CodeMirror in IPython. CodeMirror supports both `vim` and `emacs` keyboard shortcuts, but you have to enable them. \nHere's how you do that in the notebook."
},
{
"metadata": {},
"cell_type": "code",
"input": "%%javascript\n// change the mode of all current and future CodeMirror instances\nfunction to(mode) {\n var mode = mode || 'vim'\n // first let's apply vim mode to all current cells\n function to_mode(c) { return c.code_mirror.setOption('keyMap', mode);};\n IPython.notebook.get_cells().map(to_mode);\n // apply the mode to future cells created\n IPython.Cell.options_default.cm_config.keyMap = mode;\n}\n\nrequire([\"/static/components/codemirror/keymap/emacs.js\"],\n function (emacs) { \n to('emacs'); \n console.log('emacs.js loaded'); \n });",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Then, for you, just put a call of: to('emacs') in that curly\nbraces of that function (emacs) callback from earlier (that\nfunction's what will be called after the emacs.js file is\nactually loaded, because trying to call it before that won't\nwork. So your solution should look something like this:"
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "pwd ",
"prompt_number": 9,
"outputs": [
{
"output_type": "pyout",
"prompt_number": 9,
"metadata": {},
"text": "u'/home/pi/cur/scipy2014'"
}
],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "markdown",
"source": "%%javascript \nrequire(['files/jsobject']"
},
{
"metadata": {},
"cell_type": "code",
"input": "from jsobject import JSObject\nwindow = JSObject()",
"prompt_number": 21,
"outputs": [
{
"output_type": "display_data",
"javascript": "window.jq = $;\n\nvar kernel = IPython.notebook.kernel;\nvar ws_host = kernel.ws_host;\nvar make_guid = IPython.utils.uuid;\nvar is_immutable = function (x) { return !(x instanceof Object); };\nvar last_cell = null;\n\nvar instance_register = {}; // GUID -> Instance\n\n// console.log(ajax_url);\n// console.log($.ajax(ajax_url, {async: false}));\n\nvar get_guid = function(instance) {\n if (instance._guid === undefined) {\n var guid = make_guid();\n instance._guid = guid;\n instance_register[guid] = instance;\n }\n return instance._guid;\n};\n\nvar comm_opened = function (comm, msg) {\n var call_count = 0;\n\n var results_cache = {};\n\n var send = function(original_msg, content) {\n var cell = IPython.notebook.get_msg_cell(original_msg.parent_header.msg_id) || last_cell;\n last_cell = cell;\n var handle_output = null;\n var handle_clear_output = null;\n if (cell && cell.output_area) {\n handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);\n handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);\n }\n\n // Create callback dict using what is known\n var callbacks = {\n iopub : {\n output : handle_output,\n clear_output : handle_clear_output,\n },\n };\n comm.send(content, callbacks);\n };\n\n var encode = function (index, results) {\n var response = null;\n if (is_immutable(results)) {\n response = {\n index: index,\n immutable: true,\n value: results\n };\n } else {\n response = {\n index: index,\n immutable: false,\n value: get_guid(results)\n };\n }\n return response;\n };\n\n var results_response = function (msg, index, results) {\n if (results === undefined) {\n results = null;\n }\n send(msg, encode(index, results));\n };\n\n var get_object = function(msg, x, timeout) {\n timeout = timeout || 3000;\n var obj;\n if (x.immutable) {\n obj = x.value;\n } else {\n obj = instance_register[x.value];\n }\n\n if (x.callback) {\n\n var callback = function () {\n var encoded = [];\n for (var i = 0; i < arguments.length; i++) {\n var argument = arguments[i];\n encoded.push(encode(call_count, argument));\n }\n send(msg, {\n 'callback': x.callback,\n 'index': call_count,\n 'arguments': encoded});\n\n\n call_count++;\n return null; // TODO: Return value here.\n };\n\n if (obj === null || obj === undefined) {\n return callback;\n } else {\n\n // Make the object callable!\n Object.setPrototypeOf(obj, Object.getPrototypeOf(callback));\n Object.setPrototypeOf(callback, obj);\n return callback;\n }\n\n }\n return obj;\n };\n\n var get_objects = function(msg, x) {\n var values = [];\n for (var i = 0; i < x.length; i++) {\n values.push(get_object(msg, x[i]));\n }\n return values;\n };\n\n var on_comm_msg = function(msg) {\n var data = msg.content.data;\n \n // method\n // parent\n // child\n // value\n // args\n // index\n if (data.method == 'getattr') {\n if (data.parent === '') {\n results_response(msg, data.index, window[data.child]);\n } else {\n results_response(msg, data.index, instance_register[data.parent][data.child]);\n }\n } else if (data.method == 'setattr') {\n if (data.parent === '') {\n window[data.child] = get_object(msg, data.value);\n } else {\n instance_register[data.parent][data.child] = get_object(msg, data.value);\n }\n results_response(msg, data.index, true);\n } else if (data.method == 'return') {\n var index = data.index;\n var results = data.results;\n results_cache[index] = results;\n } else if (data.method == 'apply') {\n var parent = window;\n if (data.parent !== '') {\n parent = instance_register[data.parent];\n }\n var instance = instance_register[data.function];\n results_response(msg, data.index, instance.apply(parent, get_objects(msg, data.args)));\n }\n };\n comm.on_msg(on_comm_msg);\n};\n\nIPython.notebook.kernel.comm_manager.register_target('BrowserContext', comm_opened);\n",
"metadata": {},
"text": "<IPython.core.display.Javascript at 0x7f1f11976610>"
}
],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "from __future__ import print_function",
"prompt_number": 22,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "window.console._log = window.console.log",
"prompt_number": 23,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "\ndef tee(x):\n print(x)\n window.console._log(x)\n \nwindow.console.log = tee",
"prompt_number": 24,
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "toggling full screen mode\n"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "toggling full screen mode\n"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "yo!\n"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "emacs.js loaded\n"
}
],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "import IPython\nIPython.__version__\n",
"prompt_number": 2,
"outputs": [
{
"output_type": "pyout",
"prompt_number": 2,
"metadata": {},
"text": "'2.1.0'"
}
],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "%load jsobject.js",
"prompt_number": 3,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
}
],
"metadata": {}
}
],
"metadata": {
"name": "",
"signature": "sha256:cc46cf7628ae43a2dd70526cb32736b1c749be410e5bdb6d56dcb15653231605"
},
"nbformat": 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment