This article will teach you all you need to know to use Jupyter Notebooks effectively. You only need to go through Section 1 to learn the basics and you can go into Section 2 if you want to further increase your productivity. We strongly suggest to read this tutorial in a (yes, you guessed it) Jupyter Notebook. This way you will be able to actually try the different commands we will introduce here.
Let's build up from the basics, what is a Jupyter Notebook? Well, you are reading one. It is a document made of cells. You can write like I am writing now (markdown cells) or you can perform calculations in Python (code cells) and run them like this:
Cool huh? This combination of prose and code makes Jupyter Notebook ideal for experimentation: we can see the rationale for each experiment, the code and the results in one comprehensive document. In fast.ai, each lesson is documented in a notebook and you can later use that notebook to experiment yourself.
Other renowned institutions in academy and industry use Jupyter Notebook: Google, Microsoft, IBM, Bloomberg, Berkeley and NASA among others. Even Nobel-winning economists use Jupyter Notebooks for their experiments and some suggest that Jupyter Notebooks will be the new format for research papers.
A type of cell in which you can write like this is called Markdown. Markdown is a very popular markup language. To specify that a cell is Markdown you need to click in the drop-down menu in the toolbar and select Markdown.
Click on the the '+' button on the left and select Markdown from the toolbar.
Now you can type your first Markdown cell. Write 'My first markdown cell' and press run.
You should see something like this:
My first markdown cell
Now try making your first Code cell: follow the same steps as before but don't change the cell type (when you add a cell its default type is Code). Type something like 3/2. You should see '1.5' as output.
If you made a mistake in your Markdown cell and you have already ran it, you will notice that you cannot edit it just by clicking on it. This is because you are in Command Mode. Jupyter Notebooks have two distinct modes:
Edit Mode: Allows you to edit a cell's content.
Command Mode: Allows you to edit the notebook as a whole and use keyboard shortcuts but not edit a cell's content.
You can toggle between these two by either pressing ESC and Enter or clicking outside a cell or inside it (you need to double click if its a Markdown cell). You can always know which mode you're on since the current cell has a green border if in Edit Mode and a blue border in Command Mode. Try it!
Shift+Enter: Runs the code or markdown on a cell
Up Arrow+Down Arrow: Toggle across cells
b: Create new cell
0+0: Reset Kernel
You can find more shortcuts in the Shortcuts section below.
That's it. This is all you need to know to use Jupyter Notebooks. That said, we have more tips and tricks below ↓↓↓
The five most important concepts to format your code appropriately when using markdown are:
inline: Surround your text with '`'
blockquote: Place '>' before your text.
Notice that including a hashtag before the text in a markdown cell makes the text a heading. The number of hashtags you include will determine the priority of the header ('#' is level one, '##' is level two, '###' is level three and '####' is level four). We will add three new cells with the '+' button on the left to see how every level of heading looks.
Double click on some headings and find out what level they are!
There are three types of lists in markdown.
Double click on each to see how they are built!
Code cells are different than Markdown cells in that they have an output cell. This means that we can keep the results of our code within the notebook and share them. Let's say we want to show a graph that explains the result of an experiment. We can just run the necessary cells and save the notebook. The output will be there when we open it again! Try it out by running the next four cells.
# Import necessary libraries import matplotlib.pyplot as plt
from PIL import Image
a = 1 b = a + 1 c = b + a + 1 d = c + b + a + 1 a, b, c ,d
(1, 2, 4, 8)
We can also print images while experimenting. I am watching you.
You may be running Jupyter Notebook from an interactive coding environment like Gradient, Sagemaker or Salamander. You can also run a Jupyter Notebook server from your local computer. What's more, if you have installed Anaconda you don't even need to install Jupyter (if not, just
pip install jupyter).
You just need to run
jupyter notebook in your terminal. Remember to run it from a folder that contains all the folders/files you will want to access. You will be able to open, view and edit files located within the directory in which you run this command but not files in parent directories.
If a browser tab does not open automatically once you run the command, you should CTRL+CLICK the link starting with 'https://localhost:' and this will open a new tab in your default browser.
Click on 'New' in the upper right corner and 'Python 3' in the drop-down list (we are going to use a Python kernel for all our experiments).
Note: You will sometimes hear people talking about the Notebook 'kernel'. The 'kernel' is just the Python engine that performs the computations for you.
There are a couple of useful keyboard shortcuts in
Command Mode that you can leverage to make Jupyter Notebook faster to use. Remember that to switch back and forth between
Command Mode and
Edit Mode with Esc and Enter.
m: Convert cell to Markdown
y: Convert cell to Code
D+D: Delete the cell(if it's not the only cell) or delete the content of the cell and reset cell to Code(if only one cell left)
o: Toggle between hide or show output
Shift+Arrow up/Arrow down: Selects multiple cells. Once you have selected them you can operate on them like a batch (run, copy, paste etc).
Shift+M: Merge selected cells.
Shift+Tab: [press these two buttons at the same time, once] Tells you which parameters to pass on a function Shift+Tab: [press these two buttons at the same time, three times] Gives additional information on the method
import numpy as np
There are also some tricks that you can code into a cell.
?function-name: Shows the definition and docstring for that function
??function-name: Shows the source code for that function
Line magics are functions that you can run on cells and take as an argument the rest of the line from where they are called. You call them by placing a '%' sign before the command. The most useful ones are:
%matplotlib inline: This command ensures that all matplotlib plots will be plotted in the output cell within the notebook and will be kept in the notebook when saved.
%autoreload 2: Reload all modules before executing a new line. If a module is edited, it is not necessary to rerun the import commands, the modules will be reloaded automatically.
These three commands are always called together at the beginning of every notebook.
%matplotlib inline %reload_ext autoreload %autoreload 2
%timeit: Runs a line a ten thousand times and displays the average time it took to run it.
%timeit [i+1 for i in range(1000)]
62.6 µs ± 1.82 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
for i in range(1000): a = i+1 b = 'string' c = b+1
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-17-8d78ff778454> in <module> 2 a = i+1 3 b = 'string' ----> 4 c = b+1 TypeError: can only concatenate str (not "int") to str
> <ipython-input-17-8d78ff778454>(4)<module>() 1 for i in range(1000): 2 a = i+1 3 b = 'string' ----> 4 c = b+1 ipdb> ipdb> exit
Jupyter notebooks can be run locally on your laptop/desktop, or on the cloud using third party services like mybinder.org, Google Colab, Kaggle kernels etc. You can keep track of all your Jupyter notebooks using Jovian ( https://www.jvn.io ). It provides an easy way of uploading Jupyter notebooks to the cloud with a single command, so you can access, share and run them whenever you wish.
We begin by installting and importing the Jovian python library:
!pip install jovian --upgrade -q
To upload the current notebook the cloud, use the
jovian.commit command. We can also provide additional files like
cat_example.jpg to ensure that the results of this notebook can be replicated.
[jovian] Saving notebook..
The first time you run
jovian.commit, you'll be asked to provide an API key to upload the notebook to your Jovian account. You can get the API key by pressing the 'Copy API Key' button on your Jovian profile after signing up / logging in on https://www.jvn.io.
Follow this tutorial to learn how you can use Jovian effectively: https://jvn.io/aakashns/903a04b17036436b843d70443ef5d7ad