Introduction To Jupyter Notebooks
Introduction To Jupyter Notebooks
by
Ted Petrou
© 2021 Ted Petrou All Rights Reserved
2
Contents
2 Markdown Tutorial 15
2.1 Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Italics and bold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3 Code formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.5 Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.6 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.7 New lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.8 Block quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.9 Horizontal lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.10 Markdown summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3
4 CONTENTS
Part I
5
Chapter 1
1.1.1 What are Jupyter Notebooks and why are we using them?
Jupyter Notebooks allow you to write code interactively, visualize results, and enhance the output via
markdown, HTML, LaTeX, and other rich content streams like this Monty Python image below.
We are using them because they provide an excellent means to interactively and iteratively explore and
visualize data in one place. They are also great for providing space for exercises.
1.1.2 What’s the difference between IPython notebooks and Jupyter Notebooks?
IPython Notebook was the original name for these notebooks. IPython itself is an interactive Python
shell that was created by Fernando Perez in 2001. You can run the IPython shell by simply opening a
terminal (activating your conda environment) and running the command ipython. IPython Notebooks,
created around 2011, gave IPython the web interface we use today. The idea of a web-based notebook was
7
8 CHAPTER 1. INTRODUCTION TO JUPYTER NOTEBOOKS
not new. They were already a popular way to do interactive computing in other languages like Maple or
Mathematica.
At first, IPython Notebooks only supported the execution of Python. As IPython Notebooks started to
rise in popularity, developers started creating kernels capable of executing other languages. You can see a
list of all the backend execution kernels currently available. In 2015, the project was renamed Jupyter, a
name derived from the languages Julia, Python, and R. This ‘big split’ resulted in the language agnostic
bits separated from the actual execution of the code. As it turns out, lots of people enjoy working in this
type of an environment and hopefully you will too.
Code cells have the word Code in that same box. Also, all code cells will have the word In displayed directly
to the left of the cell and a number inside of brackets denoting its order of execution. If there is no number
inside the brackets, then the code cell has yet to be executed. If the cell has been executed, the result will
be displayed directly below with the word Out directly to the left. Some code cells do not output anything
upon execution and will not have an Out section.
Press esc once to enter command mode. The cell outline will turn blue and the blinking cursor will have
disappeared. From command mode, press enter to go back into edit mode. Practice changing from edit to
command mode several times by alternating between pressing esc and enter.
By changing to edit mode, you have revealed the underlying markdown text used to create the nicely
formatted page. Press shift + enter to execute the markdown and return the cell contents back to the
formatted display. The cell below should be selected in command mode.
[2]: 5 + 7
[2]: 12
[3]: -7
[4]: a = 7
[5]: 5 + 7
3 + 6
4 - 2
[5]: 2
[6]: 5 + 7
3 + 6
a = 4
[7]: print(5 + 7)
print(3 + 6)
4 - 2
12
9
[7]: 2
Select this cell. Activate command mode (press esc) and then insert cells above by pressing A and below
by pressing B. Code cells will be inserted by default.
1.5.1 Exercise 1
What is 489 plus 143?
1.6. GETTING HELP IN THE NOTEBOOK 13
[ ]:
1.5.2 Exercise 2
Some exercise here that is in the way!
[ ]:
I find that being able to discover attributes and methods paired with the ability to reveal the documentation
a powerful and useful combination when using Jupyter Notebooks.
Markdown Tutorial
Learning markdown is very valuable as it provides a means to improve the quality of the notebooks you
create and even turn them into reports that can be published. In fact, the entirety of this book is written
in markdown within Jupyter Notebooks. This tutorial covers just about all of the markdown specification.
For a different tutorial, checkout this Markdown Cheatsheet by Adam Pritchard.
2.1 Headers
Headers are larger-sized text that precede a paragraph of text. Headers are created by beginning a line with
hash symbols followed by a space and then the text. Let’s see an example:
# Some header
Writing the above in a markdown cell creates the largest possible header. If you are familiar with HTML, it
corresponds with an H1 header. Adding more hash symbols decreases the size of the header. For instance,
#### Some header creates an H4 header. The smallest header, H6, uses six hash symbols.
15
16 CHAPTER 2. MARKDOWN TUTORIAL
a = 3
b = 4
c = a + b
2.4 Lists
It’s possible to create both unordered and ordered lists. Unordered lists are created by beginning a line
with a single asterisk followed by a space and then the text of that list item. You can create a sublist by
beginning a line with a tab indentation and then an asterisk. The following markdown creates an unordered
list with the second item having its own sublist.
* some item
* another item
* sublist first item
* sublist second item
* yet another item
The rendered markdown will look like the following:
• some item
• another item
– sublist first item
– sublist second item
• yet another item
Ordered lists are created by beginning a line with a number followed by a period and a space. The first
number determines the starting number of the list. The other numbers do not affect the actual number that
is rendered. For instance, the following markdown creates an ordered list from 1 to 3.
1. first item
1. second item
1. third item
And here is the rendered markdown:
1. first item
2. second item
3. third item
The following also creates an ordered list beginning at 4. Only the first item in the list controls which
number is actually rendered.
4. first item
10. second item
3. third item
The rendered markdown:
4. first item
5. second item
6. third item
2.5 Hyperlinks
It is possible to create a hyperlink to any valid URL. There are two types of links, reference and inline.
For both types, begin by placing the words you would like to link within square brackets. For reference links,
append an additional set of square brackets immediately following these brackets and place the reference
2.6. IMAGES 17
text inside. The reference text can be anything, but I typically use numbers. Finally, place the reference
text within square brackets on its own line followed by a colon and the URL. I can link to my business’s
home page like this:
[Visit Dunder Data for expert data science training][1]
[1]: https://dunderdata.com
The rendered markdown:
Visit Dunder Data for expert data science training
With inline links, place the link in parentheses immediately following the square brackets like this.
[Visit Dunder Data for expert data science training](https://dunderdata.com)
Both reference and inline links render the same. Although inline links use slightly less typing, I prefer
reference links as the markdown is easier to read. When I use reference links, I organize them by placing
them all at the bottom of the cell.
2.6 Images
Adding images is similar to adding hyperlinks. The only difference is that a single exclamation mark, !,
precedes the square brackets. The first set of square brackets contains text for the visually impaired, usually
referred to as alt text. The link to the image can be a URL or relative link to a location in your file system
that contains an image. The following is markdown using a reference link to a relative file location.
![A tiger lying down][2]
[2]: images/tiger.png
Here is the rendered markdown:
– Ordered - start line with number, period, and space. First number matters. The rest do not.
• Hyperlinks
– Inline - [text to display](www.example.com)
– Reference - [text to display][1]
∗ Put reference in a different line [1]: www.example.com
– Images - Same as above, but precede with !
• New lines
– No new line when pressing enter
– End line with two blank spaces to create new line
2.10. MARKDOWN SUMMARY 19
The Jupyter Notebook has limitations just like any tool and is not the best for every situation. There are
certain situations that Jupyter Notebooks excel at and there are others where it fails. Depending on what
you are doing, Jupyter may or may not be the appropriate tool.
This type of workflow is iterative, because code is constantly being executed, output examined, which leads
to code being modified and executed again. It is interactive because results are given immediately upon
running the code.
21
22 CHAPTER 3. JUPYTER NOTEBOOKS MORE
3.2.3 Know where Jupyter Notebook development ends and traditional software de-
velopment begins
I typically use Jupyter Notebooks when I am exploring data, building a formal report, creating content, or
doing a temporary calculation. When exploring data, I typically use two notebooks, one for scratch work
and another for my formal report. I ensure the formal notebook has the same behavior as a normal program
by executing all of the cells in order from the top. You can do this by using the option Restart & Run
All from the Kernel menu or with the Run All option in the Cell menu. I also document my thoughts
carefully in markdown cells.
Jupyter Notebooks were designed such that other developers could extend their functionality. A set of
Jupyter Notebook extensions is found in the jupyter_contrib_nbextensions package. Instructions for in-
stalling the package are found in the Python Installation course. If you have not yet installed this package,
you can do so with a single command:
These extensions are contributed by a community not officially related to Jupyter so they may not all work
as well as the other components in the notebook. There also might be issues when using multiple different
extensions simultaneously, so I advise to use just a few at a time.
If you installed the jupyter_contrib_nbextensions package correctly, you will see a new tab titled NBEx-
tensions when you first start Jupyter.
23
24 CHAPTER 4. JUPYTER NOTEBOOK EXTENSIONS
Uncheck the box and then activate the Skip-Traceback extension. Most extensions provide a few options
to control their functionality. Scroll down the page and you’ll see a checkbox next to an option for ‘add a
button to the toolbar…’. Go ahead and check the box.
Because I do a lot of writing in the notebooks, I also activate the spellchecker extension which highlights
misspelled words when editing a markdown cell.
As the name implies, the extension allows you to skip the traceback. So, what is a traceback? A traceback is
the set of programming statements that were executed whenever an error occurs in the program. Tracebacks
allow you to literally trace back the steps of execution to help you find bugs. While this feature is helpful
for developers, it isn’t helpful for users of the library who are only interested in the error message.
When Skip-Traceback is activated, only the error message is visible and not the complete traceback. The
reason this can be extremely useful is that tracebacks for some libraries like pandas can be very long and
take up the entire screen. The following image shows the complete traceback for the case when a column
name that does not exist is selected from a DataFrame. This is a common error and it’s unnecessary to see
this wall of text.
Skip-Traceback only shows the most important part of the traceback for the users, which is the type of error
and the error message, which typically is just a single line of output. Skip-Traceback provides a toggle arrow
to reveal the traceback if desired.
26 CHAPTER 4. JUPYTER NOTEBOOK EXTENSIONS