The Jupyter Notebook provides an interactive computing environment that allows users to create notebook documents. The name Jupyter comes from the core programming languages it supports: Julia, Python, and R
The Jupyter Notebook comprises of three components
- The notebook web application: To write and run code in an interactive environment.
- Kernels: Kernel is a computation engine that runs users code present in the notebook document in a given language and returns output back to the notebook web application. The kernel also deals with computations for interactive widgets, introspection, and tab completion.
- Notebook documents: A document is a representation of all content visible in the notebook web application. This includes input and output of the computations, plots, narrative text, interactive widgets, equations, images, and video. There is a separate kernel for every document. The notebook documents can be converted to various formats and shared among others using email, Dropbox, and version control tools like git.
Jupyter Notebook Installation
The installation of python doesn’t include the Jupyter notebook. Hence we need to install it separately.
Even though there are many distributions of python, in this article we are going to cover only 2 of those for installing Jupyter notebook.
One of the most popular distributions of python is Cpython, which is the reference implementation of Python written in C and Python. You can get Cpython from their website. You can install the Jupyter notebook using pip, which is a handy tool that comes with Python. The command to install using pip is
pip install jupyter
Anaconda is also one of the popular python distributions. Anaconda comes with many preinstalled scientific libraries including the Jupyter notebook. So if you have installed Anaconda in your system, then you don’t need to install the Jupyter notebook separately.
Starting the server
Now you have installed the Jupyter notebook, let’s see how to use it. Open the terminal and go the folder of your choice. Create a subfolder of your choice to start with.
To start the server type the following command in the terminal window
You can infer from the terminal that the notebook is running at ‘https://localhost:8888/’. Open your default browser, go the following URL “https://localhost:8888/tree”. Your browser window should look like
Now our server is running, let’s create a notebook now.
Creating a Jupyter Notebook
To create a new notebook click the “New” button in the upper right corner. Among the list of choices choose Python 3. For this tutorial, we are using Python 3. Now your browser should look like:
Naming the Jupyter Notebook
Since we have created the notebook, let us see how to give a meaningful name to it.
You can notice that at the top of the page, there is a word Untitled which is nothing but the title of the page as well as the name of the notebook. When you click the word Untitled, you will get a pop-up like the below image, where you can rename the notebook as per your wish. I am renaming it as ‘Hello_ Jupyter’.
Running the cell
To run the cell, we need to add something to the cell.
The cell can contain the statements written in a programming language of the current kernel. We choose the kernel whenever we create a new notebook. Remember we created a notebook we chose Python 3. That means our cell can contain a python code.
Let’s add some python code to the cell and start running it to make sure everything is everything fine.
Running a cell means executing the cell contents. You see a list of options under the menu bar. The Run button is present among the options. The alternative way to run a cell is, in your keyboard, you can just press Shift+Enter.
The Output should look like:
In case we have multiple cells in your notebook, that enables the users to run the cells in order. Variables will be shared and imported across the cells. Since there is no need to recreate the variables or reimporting libraries at each cell, it is easy for users to split the code into logical chunks.
To run multiple cells press
Shift+J or Shift+Down to select the cells in downward in direction.
Shift+K or Shift+Up to select the cells in an upward direction.
Below is an example of running multiple cells where the variables are shared among the cells.
Notice to the left of the cell, there is a square brace next to the word In. Those square braces will be auto-filled with a number based on the order that we ran the cells. When a new notebook is opened, the number will be 1.
Menus are used to interact with the Jupyter notebook. The menu bar is present at the top of the notebook.
Let’s go over each of the items in detail.
From the options listed under the file menu, we can understand that we can do the below things with the file menu.
- Create a new notebook.
- Open an existing one.
- Rename a notebook.
- Save a notebook.
- With the help of the checkpoint option, you can create a checkpoint you can rollback to when needed.
The edit menu is to cut, copy, paste and reorder the cells. It also provides the functionality to delete, merge, or split cells.
You can notice that some of the options in the edit menu are greyed out. It means that the options are not applicable to the current cell. For example, we can insert an image into a Markdown cell, but in a code cell, we cannot. If you are seeing a greyed out option, change the cell’s type and see if it’s available to use now. The Cell menu explains how to change the cell’s type.
The view menu is all about changing the visibility of the cell. It provides the functionality to toggle header and toolbar. You can also toggle line numbers within the cell and mess with the cell’s toolbar.
If you want to add a new cell above or cell the selected the cell, insert option is the one.
As seen in the Edit menu, if you want to change the cell’s type you have to go the cell menu. In addition, the cell menu allows the user to execute a selected cell, a group of cells above or below the selected cells, and even all the cells.
Suppose you want to share your cell with others, you might need to clear the cell’s output so that others can execute themselves. The cell menu provides the option to clear the current output as well as the outputs.
To work with the Kernel that runs in the background, the kernel menu is the one. You can restart, reconnect, interrupt or shut down the kernel. Even though we don’t work with the kernel options much, there might a need to restart the kernel while debugging the notebook.
If you are not seeing the widget menu in the menu bar, run the below command in your terminal and start your Jupyter server again.
jupyter nbextension enable --py widgetsnbextension --sys-prefix
The help menu provides the user with a lot of reference materials, user interface tour, keyboard shortcuts.
Starting the Terminal from Notebook
Remember there is a list of options under the “New” button at the top right corner in the URL “https://localhost:8888/tree”. The Jupyter notebook allows the user not to create the notebook, but also a text file, folder and even start a Terminal in the browser. You can run any shell command you want in the terminal.
If you are using Windows, Terminal Option is not available.
How to see what’s running
Go to this URL “https://localhost:8888/tree”. Apart from Files, you can see two other tabs: Running and Clusters.
In the Running tab, you check what are the terminals and notebooks are currently running. This comes handy when you want to shut down the server, you want to save all the notebooks so that none of your data is lost. Even though you can hardly lose the data with the auto-save option, the Running tab gives you a better view of the currently running Notebooks and Terminals.
Also, you can shut down any particular notebook in the Running tab itself.
Adding Rich Content to the cell
Let’s understand what are the cell types before knowing how to add rich content to the cell.
Type of cells
In the cell menu, you can see the list of cell types. There are 4 types of cells: Code, Markdown, Raw NBConvert, and Heading.
The heading is no longer supported. Instead of Heading, we use Markdown.
The Raw NBConvert is used only when using the nbconvert command-line tool. It allows the user to control the formatting when a Notebook is converted to another format.
The most used cell types are Code and Markdown. As we have seen, in code cell we can code and execute the cell. With the markdown cell, we can style the text.
Styling the text
Markdown is a markup language which is nothing but a superset of HTML.
To Convert a text format to Italics, use a single underscore or single asterisk.
To Convert a text format to Bold, use a double underscore or double asterisk.
Add a new cell of type Markdown and enter the below text :
When you run the cell, the output should look like:
Adding the header
Using the pound symbol before the text makes it a header. The more the number of pound symbol, the less will be the size of the text. You can check the text preview before executing the cell.
After running the cell, the output will look like:
Creating a list
You can create a list by using dashes, plus signs or asterisk.
The output will be like:
Markdown is used when you want to insert a code example, but don’t want your end-user to run it. For a single line code, use single backticks and triple backticks for a block of code.
The output looks like:
Converting notebooks to other formats
With nbconvert the notebooks of extension .ipynb can be converted to below formats:
- ReStructured Text
- Executable script
The nbconvert uses the Jinja template, a template engine that was made for Python to convert your Notebook files into these other formats. You might also need to install Pandoc and TeX to convert your notebook to all the formats above. You may not be able to export your Notebooks to some of the above-mentioned formats if you don’t have these.
Open the terminal and navigate the folder where your notebook exists. The command to convert the Jupyter notebook to other format looks like
jupyter nbconvert <notebook to be converteted> --to <output format>
The below command converts the notebook to pdf.
jupyter nbconvert Hello_Jupyter.ipynb --to pdf
The log will display the error or warnings if there is any. If the conversion is successful you will now have Hello_Jupyter.pdf inside your folder. Similar to pdf, you can convert your notebook to any of the formats listed above.
File menu to download to other formats
In the File menu, you can find the download option. Using download option also you can convert your Jupyter notebook to other formats. Even though this option looks easier, using nbconvert you can convert multiple Jupyter notebooks at once, which is not possible using download.
Even though Jupyter has a lot of in-built functionalities, you can use extensions to add more functionalities. Jupyter supports the following extensions:
- IPython kernel
- Notebook server
You can get Jupyter Notebook extensions from the google. There are a lot of extensions. jupyter_contrib_nbextensions is one of the popular ones that are provided by the Jupyter community, which you can get from GitHub, can be installed with pip.
Most of the Jupyter Extensions can be installed with pip. The command looks like:
jupyter nbextension install EXTENSION_NAME
After installing the extension, enable it using the below command:
jupyter nbextension enable EXTENSION_NAME
To see the extension you may need to restart your Jupyter Notebook kernel
With Jupyter NbExtensions Configurator you can manage other extensions. It displays all the currently installed extensions and allows you to enable and disable your extensions within the Jupyter Notebook’s user interface.
The Jupyter Notebook is useful not only for learning but also for sharing your data. You can turn your Notebook into a presentation or share among others with GitHub. While sharing the notebook, if you don’t want your users to install anything, use a binder.
Google Colaboratory and Microsoft Azure Notebooks are Google’s and Microsoft’s own version of Notebooks that you can use to create, browse for other notebooks, and share your Notebooks.
If you want, you can try JupyterLab, an advanced version of the Jupyter Notebook, a product launched by project Jupyter. Using JupterLab you can work with Notebooks, terminals, text editors, and code consoles in a flexible, integrated, and extensible manner. You can also arrange multiple documents in the work area side by side using tabs and splitters.