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