Getting started with Jupyter Notebooks
What are Jupyter Notebooks?
Jupyter notebooks (previously known as IPython notebooks) are open-source web-based applications that allow users to combine code and rich text elements in a single file. The main feature of Jupyter Notebooks is that you can execute chunks of programming code, one chunk at a time. Some people have the ability to write code in one go. But if you’re anything like me, you keep stumbling and fumbling along the way towards something that really works. Notebooks suit this workflow because you can re-execute each cell as often as you like, try as many variations as you want until you are ready to move to the next bit. Yes! You don't have to execute all your code every time you make a minor code change.
Jupyter notebooks are also designed to be shared with others. You can tell stories with explanatory text, output, or embedded images and videos. Others can then tinker with parts of the code that interest them the most. These features make Jupyter Notebooks the most popular IDE among data scientists as seen in the recent Kaggle survey.
Kaggle is an online community of data scientists, owned by Google, Inc. Recently, Kaggle conducted an industry-wide survey that represents a comprehensive view of the state of data science and machine learning. After cleaning the data, the survey finished with almost 24k responses the results of which are shown above.
Hopefully, by this point, you are convinced about the usefulness of Jupyter Notebooks.
Setting up your own notebook
Although Jupyter notebooks support a lot of languages, Python is a prerequisite for their installation. The simplest way to install them is using the Anaconda (https://www.anaconda.com/download/) package manager that allows you to install the latest version of Python, Jupyter as well as many other packages simultaneously. Once installed, open your command prompt and type the following command:
This should launch the Jupyter Dashboard in your default browser.
NOTE 1: On Windows, you might have to add the path to the directory where Anaconda is installed in Environment Variables. Alternately, you can use the Anaconda Navigator Desktop App to launch a new notebook.
NOTE 2: As of writing this article, We using Jupyter 5.5.0 and Python 3.6.5 that comes with Anaconda.
The dashboard looks as follows:
As you can see from the URL, we are running Jupyter locally. The “Files” tab keeps track of all your notebooks, files, and subdirectories while the “Running” tab keeps track of all your processes. Jupyter also provides an in-browser terminal which is really cool.
Let’s start by creating a new notebook. To create a notebook, click on New -> Python3. This will open a new notebook which should look like this.
The notebook is created but it is untitled. By clicking into the text “Untitled” at the top, you can give it a name. The notebook consists of cells. Notebooks can be in 2 different modes: Command mode and Edit mode. Command mode allows you to add or delete cells. A blue highlight around a cell indicates command mode. Edit mode allows you to write code inside the cell. A green highlight around a cell indicates Edit mode. We can press Enter to switch to Edit mode and Esc to switch back to Command mode. There is also a kernel that runs behind every notebook to execute our code and return the output back to the cell. Let’s start by naming our notebook as “my_first_notebook”. Then click on the menu which says “Code” in the toolbar and change it to “Markdown.” Now press Enter or click inside the cell. Either way, the cell should switch from Command mode to Edit mode.
Notice the green marker besides the cell that indicates Edit mode. To run a cell press Ctrl + Enter. You should see the output of your markdown code. Now click on the ‘+’ icon on the toolbar or go to the Insert Menu in the Menu bar to add another cell.
Once you’ve inserted a new cell, write and execute the python code shown below. This time, instead of using Ctrl + Enter to execute your cells, use Shift+ Enter and Jupyter will automatically insert a new cell below the one you execute for you.
Out indicates the output of cell 3. This example illustrates how easy it is to separate blocks of code in Jupyter notebooks.
Now imagine that you add a lot more code to our notebook and at some point, you decide to make changes to the implementation of the function simple_interest. You would like to check the results for 2 or 3 variations of the formula for calculating simple_interest. In these kinds of situations, Jupyter allows us to make changes to and execute only the cells relevant to us while leaving the rest of them as they are. This saves us CPU time as we don’t have to run other parts of our code again and again especially those that can be time-consuming.
For the sake of this article, we will make a simple change. We will give the parameter ‘r’ a default value of 0.05 and rerun the cell. In the screenshot below, I’ve added some code to separate the function definition and function call for demonstration purposes. You can add your own code to follow along.
Jupyter also shows you the sequence in which you run cells. Notice the In  in the above image. Now go to the cell where the function is called and execute that.
If our changes affect a lot of cells, it might be a good idea to execute all the cells again. One such situation might be when we’ve read a file at the start of our application and we decide to change it. The ‘Cell’ Menu provides a lot of options for running a bunch of cells together.
However, running all the cells again will not change the state of the kernel. If you want to follow the traditional way of rerunning the whole program by clearing all the variables defined in your application, you can do so by restarting the kernel. You can also interrupt the kernel if a calculation takes too long to execute or we enter an infinite loop. These functionalities are available in the Kernel Menu.
The Edit Menu allows us to work with cells. You can merge two cells together, delete multiple cells, or move a cell up or down by a place using the Edit menu. To select multiple cells, switch to Command mode and use Shift + Arrow keys. Once selected go to Edit -> Delete Cells. You can also undo this by clicking on Edit -> Undo Delete Cells. Let’s delete the code we inserted in between our function call and function definition.
To save the current state of the notebook we can use Checkpoints. Then we can go back to that Checkpoint and revert our changes if necessary. To create a new checkpoint just press Ctrl + S.
Jupyter also gives you various extensions to share your notebook in. Check the File Menu in the Menu bar for the same.
This right here is enough content to get you started with Jupyter Notebooks. If you’ve followed along, you will know how tiring it can be to use a mouse for everything. It really slows you down. We can make use of keyboard shortcuts instead. Listed below are some of the most useful keyboard shortcuts. You can click on Help -> Keyboard shortcuts to find all of them. You can also edit the existing ones or create your own shortcuts by clicking on Help -> Edit keyboard shortcuts. Play around a little till you get used to things.
Most useful keyboard shortcuts:
Esc : Switch to command mode
Arrow keys : move between cells
Shift + Arrow keys: Select cells
A : insert cell above
B : insert cell below
D + D: delete current cell
Z : undo cell deletion
X : cut selected cells
C : copy selected cells
Shift + V: paste above
V : paste below
M : change cell type to markdown
Y : change cell type to code
Enter : Switch to edit mode
Tab : code completion
Shift + Tab : documentation
Ctrl + / : Comment selected lines
Ctrl + Enter : Run current cell
Shift + Enter : Run current cell and move to the next cell. If there is no next cell, insert a new one.
Alt + Enter : Run current cell and add a new cell below it.
Ctrl + S : Save and checkpoint.