{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Notes taken from the online [tutorial](https://jakevdp.github.io/blog/2013/06/01/ipython-notebook-javascript-python-communication/) about embedding javascript in ipython notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Learning Notes\n", "### Executing python from within javascript\n", "\n", "The key functionality needed for interaction between javascript and the `IPython kernel` is the kernel object in the IPython Javascript package. Use `IPython.display.HTML` for embedded html and javascript display \n", "\n", "```javascript\n", "var kernel = IPython.notebook.kernel;\n", "kernel.execute(command);\n", "```\n", "\n", "or with a callback function in javascript\n", "\n", "```javascript\n", "var kernel = IPython.notebook.kernel;\n", "function callback(out_type, out_data){\n", " // do_something\n", "}\n", "kernel.execute(command, {\"output\": callback});\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "Variable Name:
\n", "Variable Value:
\n", "\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import HTML\n", "\n", "input_form = \"\"\"\n", "
\n", "Variable Name:
\n", "Variable Value:
\n", "\n", "
\n", "\"\"\"\n", "\n", "javascript = \"\"\"\n", "\n", "\"\"\"\n", "\n", "HTML(input_form + javascript)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'foo' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0mfoo\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'foo' is not defined" ] } ], "source": [ "print foo" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "Code:
\n", "Result:
\n", "\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add an input form similar to what we saw above\n", "input_form = \"\"\"\n", "
\n", "Code:
\n", "Result:
\n", "\n", "
\n", "\"\"\"\n", "\n", "# here the javascript has a function to execute the code\n", "# within the input box, and a callback to handle the output.\n", "javascript = \"\"\"\n", "\n", "\"\"\"\n", "\n", "HTML(input_form + javascript)" ] }, { "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }