##// END OF EJS Templates
Add example of a simple kernel
Thomas Kluyver -
Show More
@@ -48,10 +48,50 following methods and attributes:
48 48 using :meth:`~IPython.kernel.zmq.kernelbase.KernelBase.send_response`.
49 49 See :doc:`messaging` for details of the different message types.
50 50
51 To launch your kernel::
51 To launch your kernel, add this at the end of your module::
52
53 if __name__ == '__main__':
54 from IPython.kernel.zmq.kernelapp import IPKernelApp
55 IPKernelApp.launch_instance(kernel_class=MyKernel)
56
57 Example
58 -------
59
60 ``echokernel.py`` will simply echo any input it's given to stdout::
61
62 from IPython.kernel.zmq.kernelbase import KernelBase
63
64 class EchoKernel(KernelBase):
65 implementation = 'Echo'
66 implementation_version = '1.0'
67 language = 'no-op'
68 language_version = '0.1'
69 banner = "Echo kernel - as useful as a parrot"
70
71 def do_execute(self, code, silent, store_history=True, user_experssions=None,
72 allow_stdin=False):
73 if not silent:
74 stream_content = {'name': 'stdout', 'data':code}
75 self.send_response(self.iopub_socket, 'stream', stream_content)
76
77 return {'status': 'ok',
78 # The base class increments the execution count
79 'execution_count': self.execution_count,
80 'payload': [],
81 'user_expressions': {},
82 }
83
84 if __name__ == '__main__':
85 from IPython.kernel.zmq.kernelapp import IPKernelApp
86 IPKernelApp.launch_instance(kernel_class=EchoKernel)
87
88 Here's the Kernel spec ``kernel.json`` file for this::
89
90 {"argv":["python","-m","echokernel", "-f", "{connection_file}"],
91 "display_name":"Echo",
92 "language":"no-op"
93 }
52 94
53 from IPython.kernel.zmq.kernelapp import IPKernelApp
54 IPKernelApp.launch_instance(kernel_class=MyKernel)
55 95
56 96 Optional steps
57 97 --------------
General Comments 0
You need to be logged in to leave comments. Login now