0% found this document useful (0 votes)
42 views

Introduction To Jupyter Notebooks

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Introduction To Jupyter Notebooks

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to Jupyter Notebooks

by
Ted Petrou
© 2021 Ted Petrou All Rights Reserved
2
Contents

I Introduction to Jupyter Notebooks 5

1 Introduction to Jupyter Notebooks 7


1.1 Jupyter Notebook Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2 Getting Started with Jupyter Notebooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3 Executing Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4 Keyboard Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.5 Completing exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.6 Getting help in the notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.7 Jupyter Notebook Command Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

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 Jupyter Notebooks More 21


3.1 Where Jupyter Notebooks Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2 Where Jupyter Notebooks Fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3 On-demand Jupyter Notebooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

4 Jupyter Notebook Extensions 23


4.1 The NBextensions tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.2 The Skip-Traceback Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

3
4 CONTENTS
Part I

Introduction to Jupyter Notebooks

5
Chapter 1

Introduction to Jupyter Notebooks

1.1 Jupyter Notebook Basics


If you have never seen this application, then you might be wondering what strange place you have landed on
the internet. You are now running a Jupyter Notebook server from your localhost at default port 8888.
This specific notebook is connected to an IPython kernel capable of executing Python commands.

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.

1.2 Getting Started with Jupyter Notebooks


It is important that you understand the fundamentals of how to use Jupyter Notebooks. They are one of
the most popular environments to explore data with and are the environment where most of the material
for all Dunder Data courses was written. The remaining content in this chapter details the very basics of
the Jupyter Notebook.

1.2.1 All notebooks are composed of cells


Jupyter Notebooks are composed of cells. Every bit of content within a Jupyter Notebook, including this
text, resides within a cell.

1.2.2 Markdown and Code cells


There are two primary types of cells - Markdown and Code. The cell containing this text is a markdown
cell. Markdown is a simple plain-text language that allows you to add some basic styles and links with little
effort. Markdown is easy to learn with only about a dozen commands. A brief tutorial is provided in a later
chapter.
Code cells are where you write and execute your Python code. They only understand Python code. You
will almost always be working within a code cell.

1.2.3 Identifying Markdown and Code Cells


It is easy to identify whether you are in a markdown or code cell. Click anywhere on this text exactly once.
The outside border of the cell will turn blue and the word Markdown will appear in the dropdown box in
the center of the menu at the top of this page.

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.

1.2.4 Edit Mode vs Command Mode


Jupyter Notebook cells are always in one of two modes: edit or command mode. When in edit mode:
1.3. EXECUTING CELLS 9

• Cells are outlined in green


• Cells have a blinking cursor in them
• Typing results in plain text inside the cell
• A small pencil icon is visible in the top right-hand-side of the menu bar

When in command mode:

• Cells are outlined in blue


• Cells do not have a blinking cursor and there is nowhere to type
• The keys have special meanings
• No small pencil icon visible in the top right-hand-side of the menu bar

1.2.5 Selected Cells


There is always at least one cell in a notebook that is selected. A selected cell will be outlined in either
green or blue depending on what mode it is in. Clicking on a cell is one way to select it.

1.2.6 Changing between edit and command Mode


The very first task we will learn is how to change between edit and command mode. This is a valuable and
simple task. Click anywhere in the code cell below. It has a Python comment in green text. Clicking in the
cell places you in edit mode. You should see a blinking cursor, a green outline around the cell, and will be
able to type text directly into it.

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.

[1]: # I am a code cell. Click here.

1.2.7 Changing modes in a markdown cell


We will now practice changing between edit and command mode in a markdown cell. Click this text in
this markdown cell once to select it. It should be outlined in blue, meaning it is in command mode. Change
to edit mode by pressing enter (or double-clicking it).

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.

1.2.8 More on Command Mode


While in command mode, the keys are mapped to a specific command, such as inserting a new cell, deleting
a cell, copying a cell, etc… You are quite literally commanding the notebook to do something. More details
on command mode will be shown further below.

1.3 Executing Cells


To execute all the lines of Python code within a code cell, select it, and then press shift + enter. If the
last line of code in your cell produces any output, then this value will be displayed in the Out section of the
cell directly below it.
10 CHAPTER 1. INTRODUCTION TO JUPYTER NOTEBOOKS

1.3.1 Execute your first cell


Let’s practice this by executing the cell directly below. It is a code cell that adds two numbers together.
Select the cell in either edit or command mode and press shift + enter to execute it.

[2]: 5 + 7

[2]: 12

1.3.2 Executing with ctrl + enter


In addition to the output of the operation, the focus is moved to the next cell (this one), which has been
selected in command mode. It is possible to execute a cell without moving the focus to the next cell by
pressing ctrl + enter or cmd + enter on a mac. Place your cursor in the next cell and execute it with
ctrl + enter. You will remain in the current cell but in command mode.
[3]: 3 - 10

[3]: -7

1.3.3 Executing a cell with no output


Not all code cells produce an Out section. Only those where the last line in the cell contains output. Run
the following cell, which assigns the value 7 to the variable name a.

[4]: a = 7

1.3.4 Only last line of cell is output


You can write any number of Python statements in a single cell, but only the last line of the cell will be
outputted to the screen. Execute the following cell to verify this.

[5]: 5 + 7
3 + 6
4 - 2

[5]: 2

1.3.5 Another example with multiple statements but no output


It doesn’t matter if statements before the last line produce output. What determines output is the last line
of the cell. Execute the following cell which produces no output.

[6]: 5 + 7
3 + 6
a = 4

1.3.6 An exception with the print function


It is possible to produce output from lines that are not the last with the print function. Notice how the
results of the first two statements are printed below the cell with the output of the last line still in its own
Out section.
1.4. KEYBOARD SHORTCUTS 11

[7]: print(5 + 7)
print(3 + 6)
4 - 2

12
9

[7]: 2

1.4 Keyboard Shortcuts


There are a few keyboard shortcuts that will make your life working in a Jupyter Notebook easier. Keyboard
shortcuts exist both in command and edit mode and have already issued some of them. Pressing enter in
command mode is the keyboard shortcut to switch to edit mode. Pressing esc while in edit mode is the
shortcut for returning to command mode. Cell execution with shift/ctrl + enter is a shortcut that works
in both command and edit mode.

1.4.1 Inserting cells


Inserting cells above or below the currently selected cell is a common task that you can perform in command
mode. To begin, select a cell and activate command mode by pressing esc once (you don’t need to hold it).
Press A to insert a cell above and B to insert a cell below the selected cell. Practice this now by inserting
cells above and below the following cell.

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.4.2 Deleting Cells


Occasionally, you’ll want to delete a cell. This is done in command mode by pressing D twice. Alternatively,
you can just press X once, which cuts the cell, but has the effect of deleting it if you do not paste back into
your notebook later. Practice deleting some of the cells you just inserted above.

1.4.3 Using the menu to insert and delete cells


Cell insertion and deletion can also be done through the menu at the top of the screen. The Insert menu
handles insertion and the Edit menu has an option for deletion.

1.4.4 Keep your hands on the keyboard - Use the shortcuts


I strongly recommend using the keyboard shortcuts instead of the menu commands. Keeping your hands on
the keyboard allows you to focus on completing tasks instead of reaching for the mouse. These shortcuts will
save you lots of time. There are only about a half-dozen commands that make up nearly all of my keyboard
shortcut usage, so it should not be hard to memorize.

1.4.5 Changing between Code and Markdown cells


By default, all cells are code cells. To change the type of cell, select the cell in command mode and press
M to change it to markdown. When in a markdown cell, press Y to change it back to a code cell. You can
also change cell types from the dropdown menu at the top of the screen or from the Cell menu.
12 CHAPTER 1. INTRODUCTION TO JUPYTER NOTEBOOKS

1.4.6 Raw Cells


You’ll notice in both the dropdown menu above and from the Cell menu that you can change the cell type
to a raw cell. This is an additional type of cell that has no special functionality. It is simply raw text.
These cells can be used to contain code during notebook conversion to PDF or HTML through a library
called NBConvert.

1.4.7 Other keyboard shortcuts


There are many other keyboard shortcuts available, but the ones covered above should represent the vast
majority of your usage. To find all the other shortcuts, activate command mode and press H or find them
in the Help menu.

1.4.8 Stopping Execution


Occasionally, you will write code that creates an infinite loop or just takes too long to complete. You will
see a star within the brackets like this (In [*]) when a cell is being executed.
To stop execution, press the stop button ( � ) above or from the Kernel menu select interrupt. If cell
execution does not stop, select restart from the Kernel menu. All variable and function definitions will
be lost when the notebook is restarted. If you are still unable to stop the execution, you need to go back to
the terminal where you launched the notebook from and kill it pressing ctrl + c twice.

1.4.9 Exiting the browser tab


Exiting the browser tab of where a notebook was open does not shut it down. The notebook will still be
running in the same state as it was with all of the variables still in memory. You just no longer can visually
see it in the browser. You will be able to reopen it from the Jupyter home page and continue using it just
as you were.
If you completely exit out of your browser, you can still get back to your Jupyter home page by navigating
to URL localhost:8888.

1.4.10 Shutting down Jupyter


To exit completely out of Jupyter, you’ll need to exit the program where you launched it from - either a
terminal shell or the Anaconda Prompt or by pressing ctrl + c twice within that program.

1.5 Completing exercises


The courses contains hundreds of exercises all written in markdown cells. All exercises have a single code
cell following them where you will write the solution. Unfortunately, there is one issue that will appear
during every exercise. Whenever you execute the code for your solution, the next cell (the next exercise)
will be selected in command mode. While this is fine if you have completed the exercise and want to move
on to the next one, it is not ideal if you want to continue writing more code in a new cell.
When you find yourself in this situation (which frequently happens), simply press A to insert a new cell
above and then enter to go into edit mode in that new cell to continue writing more code. Practice resolving
this situation by executing the code cell below exercise 1 by pressing shift + enter and then adding a new
cell once you land on top of exercise 2.

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!

[ ]:

1.6 Getting help in the notebook


In this section, we will cover a couple tools provided within the notebook that help with writing code.

1.6.1 Attribute and method discovery


A common difficultly when writing code (in any language) is to remember the syntax available to use. In
Python, every object has attributes and methods that provide the functionality. Some objects have dozens
or even hundreds of available attributes and methods. Even those who use these objects on a daily basis
will have trouble remembering their names.
The notebooks provide a way to discover all these possibilities with a simple process. With your variable
name already assigned to an object, place a dot after it and then press tab. A dropdown, scrollable list of
all the possible attributes and methods will appear. The following image depicts this process with a string
object.

1.6.2 Reveal the documentation


After you have selected a method to execute from your object, you’ll need to know how to use it. With your
cursor at the end of the method hold shift and press tab twice to reveal the documentation. This is a
trick I use frequently when working in a Jupyter Notebook. The data science libraries are vast and difficult
to retain all the information in working memory. Revealing the documentation in the notebook as I am
coding is very helpful. The following image depicts the documentation from a pandas DataFrame method.
14 CHAPTER 1. INTRODUCTION TO JUPYTER NOTEBOOKS

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.

1.7 Jupyter Notebook Command Summary


• Two kinds of cells
– Markdown - a plain-text language with a small set of commands for styling text
– Code - only understands Python code. Has In[ ] to the left
• Two cell modes
– Edit - Blinking cursor inside, allows you to edit cell contents, outlined in green
– Command - Keys command the notebook, outlined in blue
• Selected cell - always one cell selected. Outlined in green or blue
• Keyboard Shortcuts
– shift + enter - execute cell and move to the cell below
– ctrl/cmd + enter - execute cell and go into command mode in current cell
– esc - activate command mode
– enter - activate edit mode
– A - insert cell above
– B - insert cell below
– DD - delete cell
– X - cut cell
– M - convert to markdown cell
– Y - convert to code cell
• Getting help coding
– tab - press tab to list available commands
– shift + tab + tab - use inside of a function/method to reveal documentation
Chapter 2

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.

2.2 Italics and bold


To italicize words, surround them with a single asterisk. For instance, *some phrase* would create some
phrase in markdown.
To make words bold, surround them with two consecutive asterisks. For instance, **some phrase** becomes
some phrase.

2.3 Code formatting


When writing code within text, it’s important that is formatted differently in order to read it more easily.
To format code, wrap the contents in backticks, ‘. For instance, ‘a = 99‘ renders as a = 99. You can write
blocks of code on separate lines with three backticks to start and end the block. The following shows an
example of how you would write a few lines of code in a block.
“‘
a=3
b=4
c=a+b
“‘
The above markdown is rendered as:

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:

2.7 New lines


One surprising feature of markdown is how it enforces the creation of new lines. Simply pressing enter at
the end of a line will not render a new line. You must end a line with two blank spaces in order for a new
line to be rendered. To create a new paragraph, use an empty line. For instance, the following markdown
is rendered on one line with its output below.
This
is
one
line
of
markdown
18 CHAPTER 2. MARKDOWN TUTORIAL

And this will be a new paragraph


This is one line of markdown
And this will be a new paragraph

2.8 Block quotes


You can emphasize the importance of some text by making it a block quote. To do, begin the line with the
greater than sign >, as done below.
> Learning markdown will really make your notebook experience better!
The rendered markdown:
Learning markdown will really make your notebook experience better!

2.9 Horizontal lines


Create a horizontal line that extends the width of the page using three consecutive asterisks.
Text above line
***
Text below line
The rendered markdown:
Text above line *** Text below line

2.10 Markdown summary


• Headers - One to six # at the beginning of a line
• Italics - wrap text in a single asterisk - *this is important*
• Bold - wrap text in two asterisks - **this is really important**
• Code formating
– Inline - one backtick - a = 99
– Block - three backticks at beginning of line
“‘
a=3
b=4
c=a+b
“‘
• Lists
– Unordered - start line with asterisk and space. Indent for sublists

– 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

– Start a new paragraph by leaving one whole line blank


• Block quotes
– Begin line with >
• Horizontal lines
– Three asterisks - ***
20 CHAPTER 2. MARKDOWN TUTORIAL
Chapter 3

Jupyter Notebooks More

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.

3.1 Where Jupyter Notebooks Excel


Jupyter Notebooks are excellent for constructing data explorations that incorporate code, visualizations,
text, images, videos, and math text. Essentially, they are great at building a report that someone can read
and get information from. They are great for blog posts, teaching, formal reports, and interview assignments.

3.1.1 Iterative and interactive workflow


One of the best features of the notebook is the ability to run one line of code, get output, and then use this
output to run the very next line of code. For many scientists, it will be absolutely necessary to process their
thinking one line at a time. Writing multiple lines of code in a row just doesn’t make sense because there
needs to be some analysis or decision based on the output of the previous line before continuing.

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.

3.1.2 Using Jupyter as a light-weight interactive dashboard


There are a number of interactive widgets that you can embed into the Notebook to give a user control.
There are also interactive visualization libraries such as bokeh and plotly that both provide powerful tools
for dashboards.

3.2 Where Jupyter Notebooks Fail


You might not like notebooks. Don’t fear, you will not be alone. There is no mandatory requirement to use
Jupyter Notebooks to write Python or do data science. Traditional software development, such as building
an application, would not be a time where Jupyter Notebooks would be a good choice. Other programming
environments, such as Sublime, PyCharm, or Visual Studio Code provide much better tools to organize,
test, write, refactor, debug and version control code.

21
22 CHAPTER 3. JUPYTER NOTEBOOKS MORE

3.2.1 Notebooks suffer from messiness


It is incredibly easy to make a complete mess of your notebooks. They can become very difficult to follow
especially if you do not document what is happening. It is easy to get carried away doing lots of scratch
work and creating dozens of code cells that may or may not work.

3.2.2 Out of order code execution


One built-in feature/problem with notebooks is that you can run any cell at any place at any time. In a
normal computer program, control flows from one line to the next. With notebooks, you control which cells
get executed, so it is easy to execute cells in a different order than their natural ordering. Furthermore, it
is easy to lose track of variable values.

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.

3.3 On-demand Jupyter Notebooks


There are a number of websites that host Jupyter Notebooks that you can use at any time with just a
connection to the internet. Each of the following sites provides quick access to a notebook from anywhere
in the world.
• Dunder Data Python Challenges
• Google Colaboratory
• Microsoft Azure
• Project Jupyter Home
Chapter 4

Jupyter Notebook Extensions

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:

conda install -c conda-forge jupyter_contrib_nbextensions

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.

4.1 The NBextensions tab

If you installed the jupyter_contrib_nbextensions package correctly, you will see a new tab titled NBEx-
tensions when you first start Jupyter.

Click on this tab to reveal the several dozen extensions available.

23
24 CHAPTER 4. JUPYTER NOTEBOOK EXTENSIONS

4.2 The Skip-Traceback Extension


I only recommend activating the Skip-Traceback extension for the Dunder Data courses. You’ll notice
that it is unable to be selected at the current state. Look at the top-left corner of the page under the
Configurable nbextensions header. There is a checkbox that warns about possible compatibility issues
with the notebook breaking for certain extensions. This is a warning and means that not all of the extensions
have been fully tested. I have yet to encounter any person having issue with the Skip-Traceback extension.

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.

4.2.1 Skip-Traceback in the Notebook


Open up a new Jupyter Notebook or reload the page for this current notebook and you will see a little icon
with an exclamation mark within a triangle in the menu bar above. By default, the Skip-Traceback feature
will be turned on.
4.2. THE SKIP-TRACEBACK EXTENSION 25

4.2.2 How it works

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

4.2.3 Skip-Traceback is optional


I find the Skip-Traceback extension to be extremely useful, especially for those new to programming. It
allows you to focus on the error message which is the most important part of the traceback. Even so, it is
not a necessary component of the courses, so you may not enable it if desired.

You might also like