Created
July 8, 2014 19:51
-
-
Save jdfreder/b77e4009d51e4cf94a3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "from __future__ import print_function\nfrom jsobject import JSObject\nwindow = JSObject()", | |
| "prompt_number": 1, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "javascript": "window.jq = $;\n\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\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 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) {\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 // TODO: Wait here for a response.\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 == '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 object>" | |
| } | |
| ], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.IPython.version", | |
| "prompt_number": 5, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 5, | |
| "metadata": {}, | |
| "text": "u'3.0.0-dev'" | |
| } | |
| ], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.IPython.notebook.get_cell(0).element.css('background', 'blue')", | |
| "prompt_number": 6, | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "%%javascript\nwindow.a = 3;", | |
| "prompt_number": 7, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "javascript": "window.a = 3;", | |
| "metadata": {}, | |
| "text": "<IPython.core.display.Javascript object>" | |
| } | |
| ], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "print(window.a)\n\nwindow.a = 4\n\nprint(window.a)", | |
| "prompt_number": 8, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "3\n" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "4\n" | |
| } | |
| ], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "%%javascript\nwindow.obj1 = {myvalue: 6};\n\nwindow.hello_world = function (val) {\n console.log('** hello world ', val); \n return val.myvalue * 2;\n};", | |
| "prompt_number": 9, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "javascript": "window.obj1 = {myvalue: 6};\n\nwindow.hello_world = function (val) {\n console.log('** hello world ', val); \n return val.myvalue * 2;\n};", | |
| "metadata": {}, | |
| "text": "<IPython.core.display.Javascript object>" | |
| } | |
| ], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "print(window.hello_world(window.obj1))", | |
| "prompt_number": 10, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "12\n" | |
| } | |
| ], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.hello_world??", | |
| "prompt_number": 11, | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "str(window.obj1)", | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "%%javascript\nwindow.testf = function () {\n console.log('failure');\n};\n\nwindow.testf2 = function () {\n console.log('success');\n};", | |
| "prompt_number": 12, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "javascript": "window.testf = function () {\n console.log('failure');\n};\n\nwindow.testf2 = function () {\n console.log('success');\n};", | |
| "metadata": {}, | |
| "text": "<IPython.core.display.Javascript object>" | |
| } | |
| ], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.testf()", | |
| "prompt_number": 13, | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.testf2()", | |
| "prompt_number": 14, | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.testf = window.testf2\nwindow.testf()", | |
| "prompt_number": 15, | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.testf()\nwindow.testf2()", | |
| "prompt_number": 16, | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.console.log('hi')", | |
| "prompt_number": 17, | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.jq('.btn').css('background', 'red')", | |
| "prompt_number": 18, | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.console.log(window.IPython)\nwindow.console.log(window.obj1)", | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window._context._calls", | |
| "prompt_number": 19, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 19, | |
| "metadata": {}, | |
| "text": "60" | |
| } | |
| ], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "window.console.log = print", | |
| "prompt_number": 20, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "print!!!\n" | |
| } | |
| ], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "%%javascript\nconsole.log('print!!!');", | |
| "prompt_number": 21, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "javascript": "console.log('print!!!');", | |
| "metadata": {}, | |
| "text": "<IPython.core.display.Javascript object>" | |
| } | |
| ], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "def onhover(*pargs):\n window.jq('#ipython_notebook img').css('width', 100)\ndef onleave(*pargs):\n window.jq('#ipython_notebook img').css('width', '')\n\nwindow.jq('#ipython_notebook').on('mouseover', onhover)\nwindow.jq('#ipython_notebook').on('mouseleave', onleave)", | |
| "prompt_number": 2, | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| }, | |
| { | |
| "metadata": {}, | |
| "cell_type": "code", | |
| "input": "", | |
| "outputs": [], | |
| "language": "python", | |
| "trusted": true, | |
| "collapsed": false | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ], | |
| "metadata": { | |
| "name": "", | |
| "signature": "sha256:2acf6bdea200f89a79cfd4cc2ae1db2601721846ace8b985736cd2eb23d30406" | |
| }, | |
| "nbformat": 3 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment