##// END OF EJS Templates
Convert matplotlib gui name in enable_gui (#14451)...
Convert matplotlib gui name in enable_gui (#14451) This is a bug that I discovered whilst dealing with matplotlib/matplotlib#28332. The change here is in `TerminalInteractiveShell.enable_gui` which needs to convert a gui of `macosx` from Matplotlib into `osx` in IPython. It reuses the converter function created in #14420. I've added a test that uses `enable_gui` and raises an exception before this PR but passes with it. The remainder of the changes are formatting changes from `pre-commit`.

File last commit:

r26565:29cc7180
r28797:41362418 merge
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>`.