##// END OF EJS Templates
remove unregister trait-change callback in ExtensionManager.__del__...
remove unregister trait-change callback in ExtensionManager.__del__ Due to reference cycles, this can never be called at a time when it would matter, but seems to cause weird crashes sometimes during interpreter teardown on Python 3.5 with traitlets 4.1.

File last commit:

r21636:ec0fabd0
r21833:dc1d1d4e
Show More
wrapperkernels.rst
175 lines | 6.0 KiB | text/x-rst | RstLexer

Making simple Python wrapper kernels

You can now re-use the kernel machinery in IPython to easily make new kernels. This is useful for languages that have Python bindings, such as Octave (via Oct2Py), or languages where the REPL can be controlled in a tty using pexpect, such as bash.

Required steps

Subclass :class:`ipykernel.kernelbase.Kernel`, and implement the following methods and attributes:

To launch your kernel, add this at the end of your module:

if __name__ == '__main__':
    from ipykernel.kernelapp import IPKernelApp
    IPKernelApp.launch_instance(kernel_class=MyKernel)

Example

echokernel.py will simply echo any input it's given to stdout:

from ipykernel.kernelbase import Kernel

class EchoKernel(Kernel):
    implementation = 'Echo'
    implementation_version = '1.0'
    language = 'no-op'
    language_version = '0.1'
    language_info = {'mimetype': 'text/plain'}
    banner = "Echo kernel - as useful as a parrot"

    def do_execute(self, code, silent, store_history=True, user_expressions=None,
                   allow_stdin=False):
        if not silent:
            stream_content = {'name': 'stdout', 'text': code}
            self.send_response(self.iopub_socket, 'stream', stream_content)

        return {'status': 'ok',
                # The base class increments the execution count
                'execution_count': self.execution_count,
                'payload': [],
                'user_expressions': {},
               }

if __name__ == '__main__':
    from ipykernel.kernelapp import IPKernelApp
    IPKernelApp.launch_instance(kernel_class=EchoKernel)

Here's the Kernel spec kernel.json file for this:

{"argv":["python","-m","echokernel", "-f", "{connection_file}"],
 "display_name":"Echo"
}

Optional steps

You can override a number of other methods to improve the functionality of your kernel. All of these methods should return a dictionary as described in the relevant section of the :doc:`messaging spec <messaging>`.