##// END OF EJS Templates
Fix input transformer bug when pasting ipython promts....
Fix input transformer bug when pasting ipython promts. Fixes a SyntaxError that arose when pasting code containing the IPython continuation prompt without preceding spaces, eg.: In [1]: if True: ...: print "No spaces before the '...:' prompt." This issue was brought up in "http://stackoverflow.com/questions/26297356/how-can-i-paste-the-ipython-output-in-ipython#comment41270724_26297356" This commit adds a test for the bug, and fixes the regular expression that strips continuation prompts.

File last commit:

r18471:b4a3f46c
r19036:4f186a3a
Show More
wrapperkernels.rst
174 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:`IPython.kernel.zmq.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 IPython.kernel.zmq.kernelapp import IPKernelApp
    IPKernelApp.launch_instance(kernel_class=MyKernel)

Example

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

from IPython.kernel.zmq.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 IPython.kernel.zmq.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>`.