Skip to content

Commit b3bd10f

Browse files
committed
Merge pull request #30 from BCSharp/ipython4_ch01_rcp06
Upgraded Chapter 01 Recipe 06 to IPython 4.0.0
2 parents 97d0b89 + 78f046a commit b3bd10f

File tree

1 file changed

+110
-31
lines changed

1 file changed

+110
-31
lines changed

notebooks/chapter01_basic/06_kernel.ipynb

Lines changed: 110 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"cell_type": "markdown",
1919
"metadata": {},
2020
"source": [
21-
"This recipe has been tested on the development version of IPython 3. It should work on the final version of IPython 3 with no or minimal changes. We give all references about wrapper kernels and messaging protocols at the end of this recipe."
21+
"This recipe has been tested on IPython 4. It should work on IPython 3 with minimal changes. We give all references about wrapper kernels and messaging protocols at the end of this recipe."
2222
]
2323
},
2424
{
@@ -39,7 +39,8 @@
3939
"%%writefile plotkernel.py\n",
4040
"# NOTE: We create the `plotkernel.py` file here so that \n",
4141
"# you don't have to do it...\n",
42-
"from IPython.kernel.zmq.kernelbase import Kernel\n",
42+
"from ipykernel.kernelbase import Kernel\n",
43+
"#from IPython.kernel.zmq.kernelbase import Kernel # IPython < 4.x\n",
4344
"import numpy as np\n",
4445
"import matplotlib.pyplot as plt\n",
4546
"from io import BytesIO\n",
@@ -64,10 +65,17 @@
6465
"class PlotKernel(Kernel):\n",
6566
" implementation = 'Plot'\n",
6667
" implementation_version = '1.0'\n",
67-
" language = 'python' # will be used for\n",
68-
" # syntax highlighting\n",
69-
" language_version = ''\n",
7068
" banner = \"Simple plotting\"\n",
69+
" language_info = {\n",
70+
" 'name': 'python', # will be used for syntax highlighting\n",
71+
" 'version': '',\n",
72+
" 'file_extension': '.plot',\n",
73+
" 'mimetype': 'text/x-python'\n",
74+
" }\n",
75+
" # language and language_version needed only for protocol version < 5.0\n",
76+
" language = language_info['name']\n",
77+
" language_version = language_info['version']\n",
78+
"\n",
7179
" \n",
7280
" def do_execute(self, code, silent,\n",
7381
" store_history=True,\n",
@@ -98,7 +106,7 @@
98106
" # We prepare the response with our rich data\n",
99107
" # (the plot).\n",
100108
" content = {\n",
101-
" 'source': 'kernel',\n",
109+
" #'source': 'kernel', # IPython < 4.x\n",
102110
"\n",
103111
" # This dictionary may contain different\n",
104112
" # MIME representations of the output.\n",
@@ -129,23 +137,25 @@
129137
" }\n",
130138
"\n",
131139
"if __name__ == '__main__':\n",
132-
" from IPython.kernel.zmq.kernelapp import IPKernelApp\n",
140+
" from ipykernel.kernelapp import IPKernelApp\n",
141+
" #from IPython.kernel.zmq.kernelapp import IPKernelApp # IPython < 4.x\n",
133142
" IPKernelApp.launch_instance(kernel_class=PlotKernel)"
134143
]
135144
},
136145
{
137146
"cell_type": "markdown",
138147
"metadata": {},
139148
"source": [
140-
"1. First, we create a file `plotkernel.py`. This file will contain the implementation of our custom kernel. Let's import a few modules."
149+
"1\\. First, we create a file `plotkernel.py`. This file will contain the implementation of our custom kernel. Let's import a few modules."
141150
]
142151
},
143152
{
144153
"cell_type": "markdown",
145154
"metadata": {},
146155
"source": [
147-
"```\n",
148-
"from IPython.kernel.zmq.kernelbase import Kernel\n",
156+
"```python\n",
157+
"from ipykernel.kernelbase import Kernel\n",
158+
"#from IPython.kernel.zmq.kernelbase import Kernel # IPython < 4.x\n",
149159
"import numpy as np\n",
150160
"import matplotlib.pyplot as plt\n",
151161
"from io import BytesIO\n",
@@ -156,14 +166,14 @@
156166
"cell_type": "markdown",
157167
"metadata": {},
158168
"source": [
159-
"2. We write a function that returns a PNG base64-encoded representation of a matplotlib figure."
169+
"2\\. We write a function that returns a PNG base64-encoded representation of a matplotlib figure."
160170
]
161171
},
162172
{
163173
"cell_type": "markdown",
164174
"metadata": {},
165175
"source": [
166-
"```\n",
176+
"```python\n",
167177
"def _to_png(fig):\n",
168178
" \"\"\"Return a base64-encoded PNG from a \n",
169179
" matplotlib figure.\"\"\"\n",
@@ -178,14 +188,14 @@
178188
"cell_type": "markdown",
179189
"metadata": {},
180190
"source": [
181-
"3. Now, we write a function that parses a code string which has the form `y = f(x)`, and returns a NumPy function. Here, `f` is an arbitrary Python expression that can use NumPy functions."
191+
"3\\. Now, we write a function that parses a code string which has the form `y = f(x)`, and returns a NumPy function. Here, `f` is an arbitrary Python expression that can use NumPy functions."
182192
]
183193
},
184194
{
185195
"cell_type": "markdown",
186196
"metadata": {},
187197
"source": [
188-
"```\n",
198+
"```python\n",
189199
"_numpy_namespace = {n: getattr(np, n) \n",
190200
" for n in dir(np)}\n",
191201
"def _parse_function(code):\n",
@@ -198,36 +208,42 @@
198208
"cell_type": "markdown",
199209
"metadata": {},
200210
"source": [
201-
"4. For our new wrapper kernel, we create a class deriving from `Kernel`. There are a few metadata fields we need to provide."
211+
"4\\. For our new wrapper kernel, we create a class deriving from `Kernel`. There are a few metadata fields we need to provide."
202212
]
203213
},
204214
{
205215
"cell_type": "markdown",
206216
"metadata": {},
207217
"source": [
208-
"```\n",
218+
"```python\n",
209219
"class PlotKernel(Kernel):\n",
210220
" implementation = 'Plot'\n",
211221
" implementation_version = '1.0'\n",
212-
" language = 'python' # will be used for\n",
213-
" # syntax highlighting\n",
214-
" language_version = ''\n",
215222
" banner = \"Simple plotting\"\n",
216-
" ```"
223+
" language_info = {\n",
224+
" 'name': 'python', # will be used for syntax highlighting\n",
225+
" 'version': '',\n",
226+
" 'file_extension': '.plot',\n",
227+
" 'mimetype': 'text/x-python'\n",
228+
" }\n",
229+
" # language and language_version needed only for protocol version < 5.0\n",
230+
" language = language_info['name']\n",
231+
" language_version = language_info['version']\n",
232+
"```"
217233
]
218234
},
219235
{
220236
"cell_type": "markdown",
221237
"metadata": {},
222238
"source": [
223-
"5. In this class, we implement a `do_execute()` method that takes code as input, and sends responses to the client."
239+
"5\\. In this class, we implement a `do_execute()` method that takes code as input, and sends responses to the client."
224240
]
225241
},
226242
{
227243
"cell_type": "markdown",
228244
"metadata": {},
229245
"source": [
230-
"```\n",
246+
"```python\n",
231247
"def do_execute(self, code, silent,\n",
232248
" store_history=True,\n",
233249
" user_expressions=None,\n",
@@ -292,38 +308,99 @@
292308
"cell_type": "markdown",
293309
"metadata": {},
294310
"source": [
295-
"6. Finally, we add the following lines at the end of the file."
311+
"6\\. Finally, we add the following lines at the end of the file."
296312
]
297313
},
298314
{
299315
"cell_type": "markdown",
300316
"metadata": {},
301317
"source": [
302-
"```\n",
318+
"```python\n",
303319
"if __name__ == '__main__':\n",
304-
" from IPython.kernel.zmq.kernelapp import IPKernelApp\n",
320+
" from ipykernel.kernelapp import IPKernelApp\n",
321+
" #from IPython.kernel.zmq.kernelapp import IPKernelApp # IPython < 4.x\n",
305322
" IPKernelApp.launch_instance(kernel_class=PlotKernel)```"
306323
]
307324
},
308325
{
309326
"cell_type": "markdown",
310327
"metadata": {},
311328
"source": [
312-
"7. Our kernel is ready! The next step is to indicate to IPython that this new kernel is available. To do this, we need to create a **kernel spec** `kernel.json` file and put it in `~/.ipython/kernels/plot/`. This file contains the following lines:"
329+
"7\\. Our kernel is ready! The next step is to indicate to IPython that this new kernel is available. To do this, we need to create a **kernel spec** `kernel.json` file in a directory named after our kernel."
330+
]
331+
},
332+
{
333+
"cell_type": "code",
334+
"execution_count": null,
335+
"metadata": {
336+
"collapsed": false
337+
},
338+
"outputs": [],
339+
"source": [
340+
"!mkdir plot"
313341
]
314342
},
315343
{
316344
"cell_type": "markdown",
317345
"metadata": {},
318346
"source": [
319-
"```\n",
347+
"The `kernel.json` file contains the following lines:"
348+
]
349+
},
350+
{
351+
"cell_type": "code",
352+
"execution_count": null,
353+
"metadata": {
354+
"collapsed": false
355+
},
356+
"outputs": [],
357+
"source": [
358+
"%%writefile plot/kernel.json\n",
320359
"{\n",
321360
" \"argv\": [\"python\", \"-m\",\n",
322361
" \"plotkernel\", \"-f\",\n",
323362
" \"{connection_file}\"],\n",
324363
" \"display_name\": \"Plot\",\n",
325364
" \"language\": \"python\"\n",
326-
"}```"
365+
"}"
366+
]
367+
},
368+
{
369+
"cell_type": "markdown",
370+
"metadata": {},
371+
"source": [
372+
"If you are using IPython 3, simply move the directory `plot` (with `kernel.json` in it) to `~/.ipython/kernels/`. This may still work (deprecated) for IPython 4, but since IPython 4 is Jupyter based, the proper way is to copy the whole directory to the appropriate location using the `jupyter` command:"
373+
]
374+
},
375+
{
376+
"cell_type": "code",
377+
"execution_count": null,
378+
"metadata": {
379+
"collapsed": false
380+
},
381+
"outputs": [],
382+
"source": [
383+
"!jupyter kernelspec install plot --user --replace"
384+
]
385+
},
386+
{
387+
"cell_type": "markdown",
388+
"metadata": {},
389+
"source": [
390+
"The main argument to this command is the path to the directory containing the kernel spec. The option `--user` instructs Jupyter to install the kernel for the current user only. The option `--replace` overwrites any already installed kernel with the same name (if exists).\n",
391+
"\n",
392+
"The installation can be verified by listing all kernels known to Jupyter and their locations:"
393+
]
394+
},
395+
{
396+
"cell_type": "code",
397+
"execution_count": null,
398+
"metadata": {
399+
"collapsed": false
400+
},
401+
"outputs": [],
402+
"source": [
403+
"!jupyter kernelspec list"
327404
]
328405
},
329406
{
@@ -337,7 +414,9 @@
337414
"cell_type": "markdown",
338415
"metadata": {},
339416
"source": [
340-
"8. In IPython 3, you can launch a notebook with this kernel from the IPython notebook dashboard. However, this feature is not available at the time of writing. An alternative (that is probably going to be deprecated by the time IPython 3 is released) is to run the following command in a terminal:"
417+
"8\\. In IPython 4, you can launch a notebook with this kernel from the IPython notebook dashboard, in a dropdown menu from the ‘New’ button.\n",
418+
"\n",
419+
"In IPython 3, this feature may not be available in all versions. An alternative (deprecated) is to run the following command in a terminal:"
341420
]
342421
},
343422
{
@@ -353,7 +432,7 @@
353432
"cell_type": "markdown",
354433
"metadata": {},
355434
"source": [
356-
"9. Finally, in a new notebook backed by our custom plot kernel, we can simply write mathematical equations `y=f(x)`. The corresponding graph appears in the output area."
435+
"9\\. Finally, in a new notebook backed by our custom plot kernel, we can simply write mathematical equations `y=f(x)`. The corresponding graph appears in the output area."
357436
]
358437
},
359438
{
@@ -382,7 +461,7 @@
382461
"name": "python",
383462
"nbconvert_exporter": "python",
384463
"pygments_lexer": "ipython3",
385-
"version": "3.4.2"
464+
"version": "3.5.0"
386465
}
387466
},
388467
"nbformat": 4,

0 commit comments

Comments
 (0)