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