{ "metadata": { "kernelspec": { "codemirror_mode": { "name": "ipython", "version": 3 }, "display_name": "IPython (Python 3)", "language": "python", "name": "python3" }, "name": "", "signature": "sha256:2180817d2ec76fd7ba949218adc8f4a2f1718c44eb28cc7c531d499cfb7e90b1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Working with the kernel (*Back End*)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can know 'where we are' using the following:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%connect_info" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now I can open a qtconsole connected to the same kernel:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ipython qtconsole --existing kernel-6ea34ea1-9b11-441d-9f7a-693c1cf71804.json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And I can modify what I have in the notebook from the console and viceversa" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "assert a == 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----------------------------" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Creating our own kernels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
This recipe was shamelessly stolen from the [excellent cookbook](http://ipython-books.github.io/cookbook/) written by [@cyrillerossant](https://twitter.com/cyrillerossant).
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create a very simple and minimal kernel that will add a modification of the python print function in the notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile printkernel.py\n", "\n", "from io import StringIO\n", "\n", "from IPython.kernel.zmq.kernelbase import Kernel\n", "from IPython.display import HTML\n", "\n", "a = '

>>> '\n", "b = '

'\n", "\n", "class PrintKernel(Kernel):\n", " implementation = 'Print'\n", " implementation_version = '-31.0'\n", " language = 'python' # will be used for\n", " # syntax highlighting\n", " language_version = ''\n", " banner = \"Advanced printing in the notebook\"\n", " \n", " def do_execute(self, code, silent,\n", " store_history=True,\n", " user_expressions=None,\n", " allow_stdin=False):\n", "\n", " expr = None\n", " try:\n", " expr = code.split('print(')[1].split(')')[0]\n", " out = HTML(a + eval(expr) + b)\n", " except:\n", " pass\n", "\n", " if not silent:\n", " if expr:\n", " # We send the standard output to the client.\n", " self.send_response(self.iopub_socket,\n", " 'stream', {\n", " 'name': 'stdout', \n", " 'data': 'Richer print'})\n", "\n", " # We prepare the response with our rich data\n", " # (the plot).\n", " content = {\"html\": [out.data],\n", " \"metadata\": {},\n", " \"text\": [repr(out)]}\n", "\n", " # We send the display_data message with the\n", " # contents.\n", " self.send_response(self.iopub_socket,\n", " 'display_data', content)\n", "\n", " # We return the exection results.\n", " return {'status': 'ok',\n", " 'execution_count': self.execution_count,\n", " 'payload': [],\n", " 'user_expressions': {},\n", " }\n", "\n", "if __name__ == '__main__':\n", " from IPython.kernel.zmq.kernelapp import IPKernelApp\n", " IPKernelApp.launch_instance(kernel_class=PrintKernel)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "My dumb kernel is here. The next step is to indicate to IPython that this new kernel is available. To do this, we need to create a kernel spec kernel.json file and put it in (IPYTHONDIR)/kernels/print/. This file contains the following lines:\n", "````\n", "{\n", " \"argv\": [\"python\", \"-m\",\n", " \"printkernel\", \"-f\",\n", " \"{connection_file}\"],\n", " \"display_name\": \"Print\",\n", " \"language\": \"python\"\n", "}\n", "````" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see how the kernel works. Run the following in a console from the same folder where `printkernel.py` and `kernel.json` files are:\n", "\n", " ipython notebook --KernelManager.kernel_cmd=\"['python', '-m', 'printkernel', '-f', '{connection_file}']\"" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Go deeper" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://github.com/takluyver/bash_kernel\n", "\n", "http://ipython.org/ipython-doc/dev/development/wrapperkernels.html\n", "\n", "http://nbviewer.ipython.org/github/ipython-books/cookbook-code/blob/master/notebooks/chapter01_basic/06_kernel.ipynb\n", "\n", "http://ipython.org/ipython-doc/dev/development/messaging.html" ] } ], "metadata": {} } ] }