Jupyter notebooks#
Learning goals
After finishing this chapter, you are expected to
be able to run Jupyter notebooks
create, edit, and run cells in a notebook
know how to use Jupyter lab
By now, you have programmed in the command line using the Python interpreter and you have written and run scripts. Now we’re going to teach you a third and very popular way to program in Python: using notebooks. A Jupyter notebook is a file that can contain code and text. In fact, you’re looking at a notebook right now! A notebook consists of cells and each cell can be run by a Python kernel. This Python kernel is like a Python interpreter that is continuously running in the background and keeping track of your variables.
Notebooks in VS Code#
In order to use notebooks in VS Code, you first need to install the Jupyter extension, just like you did earlier for the Python extension. Go to the extensions window, search for ‘Jupyter’ and select the top option, as below.
Once the extension has been installed, you can create a notebook file much the same way that you create .py
files. The only difference is that a notebook file should always end in an .ipynb
extension. If all is well, when you create a file (for example my_notebook.ipynb
) and open it in VS Code, you should get the notebook editor view. This view is a bit different from the normal Python script editing view. The file already contains one cell that you can start editing. However, before you start editing, it’s good to select a Python kernel. For this, click on Select Kernel
in the top right of the screen. This takes you to your list of Python Environments, where you should again select your Anaconda Python interpreter.
Cells#
There are two main types of cells in notebooks
Code cells. These are cells that can be run and interpreted by Python
Markdown cells. These are cells that contain text in the Markdown style.
You can use as many cells as you want in a notebook, but each cell can only have one of these two types. Markdown cells can be used to write comments, explanations, other documentation in between your blocks of code, or you can use them to add headers to your blocks of code. Markdown is a markup language like HTML that provides options to write text bold, italic, use \(\LaTeX\), add headers, etc. We are not going to teach you all markdown commands here, but you can find an overview of the most commonly used Markdown statements here. Markdown can be very useful when building documents. In fact, this entire manual is written in Markdown. You can download each chapter as an *.ipynb
file and run it as a notebook.
Running cells#
Once you have a notebook opened in VS Code, you can run its individual cells by hitting Shift + Enter. The Jupyter kernel will then execute the contents of the cell, and if there is any output, that output will be shown directly below the cell. This is similar to how you’ve used scripts before, with two main differences:
The output is directly shown in the notebook, and not in a terminal window.
Your Python session is persistent, which means that any variables that you create in a code cell, will be kept in memory.
This means that you can create a variable in one cell, run that cell, and then reuse that variable in some other cell. Similarly, you can define a function in one cell and then reuse that function in another cell. Take a look at the example below.
This is also dangerous: you should keep of track of the order in which you execute cells, because a variable might have a different value than you expect, depending on the order in which you run cells. That means that if you run one cell first and then the other, you might get different behavior than the other way around.
Order
The order in which you run cells matters!
Variable inspector in VS Code#
To help you keep track of variables and values in your ‘workspace’, VS Code contains a variable inspector. This lists all the variables currently present in your kernel. You enter the variable inspector by clicking on Variables
on the top of your notebook. For the example above, you then get something like this, where you see variables a
and b
and their current values.
Magic commands#
Jupyter notebook cells let you use ‘magic commmands’, these are commands in your notebook that start with %
or %%
. Not of all of them are equally useful, but you can get a full list by typing %quickref
in a cell and running that cell. Some example of useful commands are %who
, %%time
and %timeit
. Just like the variable inspector, %who
shows you the variables in the current session. By providing a data type as argument, you can also see specifically which integers or strings (for example) are present.
# Let's create some variables
a, b, c = 45, 23, 128
d, e, f = 34.2, 4534.1, -129.3
g, h, i = 'Little', 'green', 'bag'
print('All variables')
%who
print('Integers')
%who int
print('Floats')
%who float
print('Strings')
%who str
All variables
a b c d e f g h i
Integers
a b c
Floats
d e f
Strings
g h i
The %%timeit
command tells you how long an individual cell takes to run. For example, we can determine how long our recursive function takes to compute \(10!\). We can add arguments to the %%timeit
function and here tell it to time this cell 100 times and compute an average. This is useful because a computer doesn’t always take the same time to execute some code, depending on which other processes are running.
%%timeit -n 100
def factorial(n):
if n > 1:
return n * factorial(n-1)
else:
return 1
factorial(10)
1.3 µs ± 7.43 ns per loop (mean ± std. dev. of 7 runs, 100 loops each)
The %timeit
function (with one %
) does something similar, but only for one line of code. In this case, we could use it like
def factorial(n):
if n > 1:
return n * factorial(n-1)
else:
return 1
%timeit -n 100 factorial(10)
1.23 µs ± 6.35 ns per loop (mean ± std. dev. of 7 runs, 100 loops each)
to get a similar result.
Alternative ways to run notebooks#
The notebook format in .ipynb
is not restricted to VS Code. In addition to running a notebook directly in VS Code, you can run notebooks in (at least) threeother ways: on a Jupyter lab server, on the UT JupyterHub, on Google Colab.
Running a Jupyter lab server In a terminal window (for example Anaconda Prompt), navigate to the directory in which you want to work and type
jupyter lab
. This should start a Jupyter lab environment in your browser. If this does not happen, look at the output in your terminal and find the address of the lab environment. This is usually something likehttps://localhost:8888
. It might be that you need a token (kind of password) to login to your Jupyter lab environment.The UT Jupyter server The University of Twente has an own JupyterHub server. This is a Jupyter environment on which many students/employees can log in at the same time (without accessing each others scripts and data). To use this server, go to the UT Jupyter website and log in with your student credentials. In fact, you can directly open the chapters of this manual as
.ipynb
notebooks on the UT Jupyter server by clicking the little rocket on the top of the page and selecting ‘JupyterHub’. It’s important to realize that if you use this option, you cannot easily access the files on your own computer.Google Colab Google offers Google Colab, which is a lot like Jupyter Notebooks and is (almost) fully compatible. You can upload
.ipynb
files to Google Colab and download your Google Colab notebooks as.ipynb
files.