##// END OF EJS Templates
Merge pull request #6292 from dsblank/master...
Thomas Kluyver -
r17620:2e5b7236 merge
parent child Browse files
Show More
@@ -1,153 +1,153 b''
1 1 Making simple Python wrapper kernels
2 2 ====================================
3 3
4 4 .. versionadded:: 3.0
5 5
6 6 You can now re-use the kernel machinery in IPython to easily make new kernels.
7 7 This is useful for languages that have Python bindings, such as `Octave
8 8 <http://www.gnu.org/software/octave/>`_ (via
9 9 `Oct2Py <http://blink1073.github.io/oct2py/docs/index.html>`_), or languages
10 10 where the REPL can be controlled in a tty using `pexpect <http://pexpect.readthedocs.org/en/latest/>`_,
11 11 such as bash.
12 12
13 13 .. seealso::
14 14
15 15 `bash_kernel <https://github.com/takluyver/bash_kernel>`_
16 16 A simple kernel for bash, written using this machinery
17 17
18 18 Required steps
19 19 --------------
20 20
21 21 Subclass :class:`IPython.kernel.zmq.kernelbase.Kernel`, and implement the
22 22 following methods and attributes:
23 23
24 24 .. class:: MyKernel
25 25
26 26 .. attribute:: implementation
27 27 implementation_version
28 28 language
29 29 language_version
30 30 banner
31 31
32 32 Information for :ref:`msging_kernel_info` replies. 'Implementation' refers
33 33 to the kernel (e.g. IPython), and 'language' refers to the language it
34 34 interprets (e.g. Python). The 'banner' is displayed to the user in console
35 35 UIs before the first prompt. All of these values are strings.
36 36
37 37 .. method:: do_execute(code, silent, store_history=True, user_expressions=None, allow_stdin=False)
38 38
39 39 Execute user code.
40 40
41 41 :param str code: The code to be executed.
42 42 :param bool silent: Whether to display output.
43 43 :param bool store_history: Whether to record this code in history and
44 44 increase the execution count. If silent is True, this is implicitly
45 45 False.
46 46 :param dict user_expressions: Mapping of names to expressions to evaluate
47 47 after the code has run. You can ignore this if you need to.
48 48 :param bool allow_stdin: Whether the frontend can provide input on request
49 49 (e.g. for Python's :func:`raw_input`).
50 50
51 51 Your method should return a dict containing the fields described in
52 52 :ref:`execution_results`. To display output, it can send messages
53 53 using :meth:`~IPython.kernel.zmq.kernelbase.Kernel.send_response`.
54 54 See :doc:`messaging` for details of the different message types.
55 55
56 56 To launch your kernel, add this at the end of your module::
57 57
58 58 if __name__ == '__main__':
59 59 from IPython.kernel.zmq.kernelapp import IPKernelApp
60 60 IPKernelApp.launch_instance(kernel_class=MyKernel)
61 61
62 62 Example
63 63 -------
64 64
65 65 ``echokernel.py`` will simply echo any input it's given to stdout::
66 66
67 67 from IPython.kernel.zmq.kernelbase import Kernel
68 68
69 69 class EchoKernel(Kernel):
70 70 implementation = 'Echo'
71 71 implementation_version = '1.0'
72 72 language = 'no-op'
73 73 language_version = '0.1'
74 74 banner = "Echo kernel - as useful as a parrot"
75 75
76 def do_execute(self, code, silent, store_history=True, user_experssions=None,
76 def do_execute(self, code, silent, store_history=True, user_expressions=None,
77 77 allow_stdin=False):
78 78 if not silent:
79 79 stream_content = {'name': 'stdout', 'data':code}
80 80 self.send_response(self.iopub_socket, 'stream', stream_content)
81 81
82 82 return {'status': 'ok',
83 83 # The base class increments the execution count
84 84 'execution_count': self.execution_count,
85 85 'payload': [],
86 86 'user_expressions': {},
87 87 }
88 88
89 89 if __name__ == '__main__':
90 90 from IPython.kernel.zmq.kernelapp import IPKernelApp
91 91 IPKernelApp.launch_instance(kernel_class=EchoKernel)
92 92
93 93 Here's the Kernel spec ``kernel.json`` file for this::
94 94
95 95 {"argv":["python","-m","echokernel", "-f", "{connection_file}"],
96 96 "display_name":"Echo",
97 97 "language":"no-op"
98 98 }
99 99
100 100
101 101 Optional steps
102 102 --------------
103 103
104 104 You can override a number of other methods to improve the functionality of your
105 105 kernel. All of these methods should return a dictionary as described in the
106 106 relevant section of the :doc:`messaging spec <messaging>`.
107 107
108 108 .. class:: MyKernel
109 109
110 110 .. method:: do_complete(code, cusor_pos)
111 111
112 112 Code completion
113 113
114 114 :param str code: The code already present
115 115 :param int cursor_pos: The position in the code where completion is requested
116 116
117 117 .. seealso::
118 118
119 119 :ref:`msging_completion` messages
120 120
121 121 .. method:: do_inspect(code, cusor_pos, detail_level=0)
122 122
123 123 Object introspection
124 124
125 125 :param str code: The code
126 126 :param int cursor_pos: The position in the code where introspection is requested
127 127 :param int detail_level: 0 or 1 for more or less detail. In IPython, 1 gets
128 128 the source code.
129 129
130 130 .. seealso::
131 131
132 132 :ref:`msging_inspection` messages
133 133
134 134 .. method:: do_history(hist_access_type, output, raw, session=None, start=None, stop=None, n=None, pattern=None, unique=False)
135 135
136 136 History access. Only the relevant parameters for the type of history
137 137 request concerned will be passed, so your method definition must have defaults
138 138 for all the arguments shown with defaults here.
139 139
140 140 .. seealso::
141 141
142 142 :ref:`msging_history` messages
143 143
144 144 .. method:: do_shutdown(restart)
145 145
146 146 Shutdown the kernel. You only need to handle your own clean up - the kernel
147 147 machinery will take care of cleaning up its own things before stopping.
148 148
149 149 :param bool restart: Whether the kernel will be started again afterwards
150 150
151 151 .. seealso::
152 152
153 153 :ref:`msging_shutdown` messages
General Comments 0
You need to be logged in to leave comments. Login now