A quick, practical intro to the Jupyter Notebook#

Original notebook from Fernando Perez
Modified slightly by David Shean

Introduction#

The IPython Notebook is an interactive computing environment that enables users to author notebook documents that include:

  • Live code

  • Interactive widgets

  • Plots

  • Narrative text

  • Equations

  • Images

  • Video

These documents provide a complete and self-contained record of a computation that can be converted to various formats and shared with others using email, Dropbox, version control systems (like git/GitHub) or nbviewer.ipython.org.

Components#

The IPython Notebook combines three components:

  • The notebook web application: An interactive web application for writing and running code interactively and authoring notebook documents.

  • Kernels: Separate processes started by the notebook web application that runs users’ code in a given language and returns output back to the notebook web application. The kernel also handles things like computations for interactive widgets, tab completion and introspection.

  • Notebook documents: Self-contained documents that contain a representation of all content visible in the notebook web application, including inputs and outputs of the computations, narrative text, equations, images, and rich media representations of objects. Each notebook document has its own kernel.

Notebook web application#

The notebook web application enables users to:

  • Edit code in the browser, with automatic syntax highlighting, indentation, and tab completion/introspection.

  • Run code from the browser, with the results of computations attached to the code which generated them.

  • See the results of computations with rich media representations, such as HTML, LaTeX, PNG, SVG, PDF, etc.

  • Create and use interactive JavaScript wigets, which bind interactive user interface controls and visualizations to reactive kernel side computations.

  • Author narrative text using the Markdown markup language.

  • Build hierarchical documents that are organized into sections with different levels of headings.

  • Include mathematical equations using LaTeX syntax in Markdown, which are rendered in-browser by MathJax.

Kernels#

Through IPython’s kernel and messaging architecture, the Notebook allows code to be run in a range of different programming languages. For each notebook document that a user opens, the web application starts a kernel that runs the code for that notebook. Each kernel is capable of running code in a single programming language and there are kernels available in over 100 programming languages.

IPython is the default kernel, it runs Python code.

Each of these kernels communicate with the notebook web application and web browser using a JSON over ZeroMQ/WebSockets message protocol that is described here. Most users don’t need to know about these details, but it helps to understand that “kernels run code.”

Notebook documents#

Notebook documents contain the inputs and outputs of an interactive session as well as narrative text that accompanies the code but is not meant for execution. Rich output generated by running code, including HTML, images, video, and plots, is embeddeed in the notebook, which makes it a complete and self-contained record of a computation.

When you run the notebook web application on your computer, notebook documents are just files on your local filesystem with a .ipynb extension. This allows you to use familiar workflows for organizing your notebooks into folders and sharing them with others using email, Dropbox and version control systems.

#Student Exercise

Notebooks consist of a linear sequence of cells. There are three basic cell types:

  • Code cells: Input and output of live code that is run in the kernel

  • Markdown cells: Narrative text with embedded LaTeX equations

  • Raw cells: Unformatted text that is included, without modification, when notebooks are converted to different formats using nbconvert

Internally, notebook documents are JSON data with binary values [base64](http://en.wikipedia.org/wiki/Base64) encoded. This allows them to be read and manipulated programmatically by any programming language. Because JSON is a text format, notebook documents are version control friendly.

Notebooks can be exported to different static formats including HTML, reStructeredText, LaTeX, PDF, and slide shows using Jupyter’s nbconvert utility.

Furthermore, any notebook document available from a public URL on or GitHub can be shared via http://nbviewer.jupyter.org. This service loads the notebook document from the URL and renders it as a static web page. The resulting web page may thus be shared with others without their needing to install Jupyter.

Body#

The body of a notebook is composed of cells. Each cell contains either markdown, code input, code output, or raw text. Cells can be included in any order and edited at-will, allowing for a large ammount of flexibility for constructing a narrative.

  • Markdown cells - These are used to build a nicely formatted narrative around the code in the document. The majority of this lesson is composed of markdown cells.

  • Code cells - These are used to define the computational code in the document. They come in two forms: the input cell where the user types the code to be executed, and the output cell which is the representation of the executed code. Depending on the code, this representation may be a simple scalar value, or something more complex like a plot or an interactive widget.

  • Raw cells - These are used when text needs to be included in raw form, without execution or transformation.

a = 1
print(a)
1

Modality#

The notebook user interface is modal. This means that the keyboard behaves differently depending upon the current mode of the notebook. A notebook has two modes: edit and command.

Edit mode is indicated by a blue cell border and a prompt showing in the editor area. When a cell is in edit mode, you can type into the cell, like a normal text editor.

Command mode is indicated by a grey cell background. When in command mode, the structure of the notebook can be modified as a whole, but the text in individual cells cannot be changed. Most importantly, the keyboard is mapped to a set of shortcuts for efficiently performing notebook and cell actions. For example, pressing c when in command mode, will copy the current cell; no modifier is needed.

Enter edit mode by pressing Enter or using the mouse to click on a cell’s editor area.

Enter command mode by pressing Esc or using the mouse to click outside a cell’s editor area.

Do not attempt to type into a cell when in command mode; unexpected things will happen!

%pylab inline
plot(rand(100))
Populating the interactive namespace from numpy and matplotlib
[<matplotlib.lines.Line2D at 0x7fb9332c3bb0>]
../../_images/02_Intro_Jupyter_Notebook_17_2.png

Mouse navigation#

The first concept to understand in mouse-based navigation is that cells can be selected by clicking on them. The currently selected cell is indicated with a blue outline or gray background depending on whether the notebook is in edit or command mode. Clicking inside a cell’s editor area will enter edit mode. Clicking on the prompt or the output area of a cell will enter command mode.

The second concept to understand in mouse-based navigation is that cell actions usually apply to the currently selected cell. For example, to run the code in a cell, select it and then click the button in the toolbar or the Run -> Run Selected Cells menu item. Similarly, to copy a cell, select it and then click the button in the toolbar or the Edit -> Copy menu item. With this simple pattern, it should be possible to perform nearly every action with the mouse.

Markdown cells have one other state which can be modified with the mouse. These cells can either be rendered or unrendered. When they are rendered, a nice formatted representation of the cell’s contents will be presented. When they are unrendered, the raw text source of the cell will be presented. To render the selected cell with the mouse, click the button in the toolbar or the Run -> Run Selected Cells menu item. To unrender the selected cell, double click on the cell.

Keyboard Navigation#

The modal user interface of the IPython Notebook has been optimized for efficient keyboard usage. This is made possible by having two different sets of keyboard shortcuts: one set that is active in edit mode and another in command mode.

The most important keyboard shortcuts are Enter, which enters edit mode, and Esc, which enters command mode.

In edit mode, most of the keyboard is dedicated to typing into the cell’s editor. Thus, in edit mode there are relatively few shortcuts. In command mode, the entire keyboard is available for shortcuts, so there are many more possibilities.

The following shortcuts have been found to be the most useful in day-to-day tasks:

  • Basic navigation: enter, shift-enter, up/k, down/j

  • Saving the notebook: s

  • Cell types: y, m, r

  • Cell creation: a, b

  • Cell editing: x, c, v, d, z, ctrl+shift+-

  • Kernel operations: i, .

You can fully customize JupyterLab’s keybindings by accessing the Settings -> Advanced Settings Editor menu item.

Running Code#

First and foremost, the Jupyter Notebook is an interactive environment for writing and running code. Jupyter is capable of running code in a wide range of languages. However, this notebook, and the default kernel in Jupyter, runs Python code.

Code cells allow you to enter and run Python code#

Run a code cell using Shift-Enter or pressing the button in the toolbar above:

a = 10
a + 1
11
a = a + 1
a
11
print(a + 1)
12

Note the difference between the above printing statement and the operation below:

a + 1
12

When a value is returned by a computation, it is displayed with a number, that tells you this is the output value of a given cell. You can later refere to any of these values (should you need one that you forgot to assign to a named variable). The last three are available respectively as auto-generated variables called _, __ and ___ (one, two and three underscores). In addition to these three convenience ones for recent results, you can use _N, where N is the number in [N], to access any numbered output.

There are two other keyboard shortcuts for running code:

  • Alt-Enter runs the current cell and inserts a new one below.

  • Ctrl-Enter run the current cell and enters command mode.

Managing the IPython Kernel#

Code is run in a separate process called the IPython Kernel. The Kernel can be interrupted or restarted. Try running the following cell and then hit the button in the toolbar above.

import time
time.sleep(10)
a
11

If the Kernel dies you will be prompted to restart it. Here we call the low-level system libc.time routine with the wrong argument via ctypes to segfault the Python interpreter:

import sys
from ctypes import CDLL
# This will crash a Linux or Mac system
# equivalent calls can be made on Windows
dll = 'dylib' if sys.platform == 'darwin' else 'so.6'
libc = CDLL("libc.%s" % dll) 
libc.time(-1)  # BOOM!!

Cell menu#

The “Run” menu has a number of items for running code in different ways, including

  • Run Selected Cells

  • Run All Cells

  • Run Selected Cell or Current Line in Console

  • Run All Above Selected Cell

  • Run Selected Cell and All Below

  • Restart Kernel and Run All Cells

Restarting the kernels#

The kernel maintains the state of a notebook’s computations. You can reset this state by restarting the kernel. This is done by clicking on the in the toolbar above.

sys.stdout and sys.stderr#

The stdout and stderr streams are displayed as text in the output area.

print("hi, stdout")
hi, stdout
print('hi, stderr', file=sys.stderr)
hi, stderr

Output is asynchronous#

All output is displayed as it is generated in the Kernel: instead of blocking on the execution of the entire cell, output is made available to the Notebook immediately as it is generated by the kernel (even though the whole cell is submitted for execution as a single unit).

If you execute the next cell, you will see the output one piece at a time, not all at the end:

import time, sys
for i in range(8):
    print(i)
    time.sleep(0.5)
0
1
2
3
4
5
6
7

Large outputs#

To better handle large outputs, the output area can be collapsed. Run the following cell and then click on the vertical blue bar to the left of the output:

for i in range(500):
    print(i)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

Markdown Cells#

Text can be added to IPython Notebooks using Markdown cells. Markdown is a popular markup language that is a superset of HTML. Its specification can be found here:

http://daringfireball.net/projects/markdown/

You can view the source of a cell by double clicking on it, or while the cell is selected in command mode, press Enter to edit it. One A cell has been editted, use Shift-Enter to re-render it.

Markdown basics#

You can make text italic or bold.

You can build nested itemized or enumerated lists:

  • One

    • Sublist

      • This

    • Sublist - That - The other thing

  • Two

    • Sublist

  • Three

    • Sublist

Now another list:

  1. Here we go

    1. Sublist

    2. Sublist

  2. There we go

  3. Now this

You can add horizontal rules:


Here is a blockquote:

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren’t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one– and preferably only one –obvious way to do it. Although that way may not be obvious at first unless you’re Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea – let’s do more of those!

And shorthand for links:

IPython’s website

You can add headings using Markdown’s syntax:

Heading 1#

Heading 2#

Heading 2.1#

Heading 2.2#

Embedded code#

You can embed code meant for illustration instead of execution in Python:

def f(x):
    """a docstring"""
    return x**2

or other languages:

if (i=0; i<n; i++) {
  printf("hello %d\n", i);
  x += 4;
}

LaTeX equations#

Courtesy of MathJax, you can include mathematical expressions both inline: \(e^{i\pi} + 1 = 0\) and displayed:

\[e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i\]

Use single dolars delimiter for inline math, so $thisisinline\int math$ will give \(this is inline\int math\), for example to refer to variable within text.

Double dollars $$\int_0^{2\pi} f(r, \phi) \partial \phi $$ is used for standalone formulas:

\[\int_0^{2\pi} f(r, \phi) \partial \phi \]

Github flavored markdown (GFM)#

The Notebook webapp support Github flavored markdown meaning that you can use triple backticks for code blocks

```python
print "Hello World"
```

```javascript
console.log("Hello World")
```

Gives

print "Hello World"
console.log("Hello World")

And a table like this :

| This | is   |
|------|------|
|   a  | table| 

A nice HTML Table

This

is

a

table

General HTML#

Because Markdown is a superset of HTML you can even add things like HTML tables:

Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2