Introduction to Jupyter Notebook

To compile and execute our python programs, we need an IDE (Integrated Development Environment) similar to what Visual Studio provides for C/C++ and Eclipse for Java. Keeping the requirement of initial learners in mind, engineers developed an easy-to-use IDE, Jupyter notebook, to run python codes line-by-line or in a cell manner. We will primarily use this IDE to develop real-life applications in Machine Learning and Data Science.

So this blog familiarizes learners with its installation and primary usage.

Key takeaways from this blog

  • What is a Jupyter notebook?
  • Installation of Jupyter notebook
  • Notebook Basics
  • Menu options available in Jupyter-notebook
  • Playing with cells in Jupyter notebook
  • Jupyter notebook extensions

Let’s start understanding what exactly it is.

What is a Jupyter notebook?

The Jupyter notebook is an open-source IDE that provides interactive Data Science and Machine Learning environments. It can be used to create Jupyter documents with embedded live codes, making it very handy for educational and presentation purposes. Embedded live code means we can interact with this code and show the effect of changes in real-time. We can also embed various visualizations in the document. 

It provides a web-based interactive computational environment where we can write text, codes, or documents. The name “Jupyter” comes primarily from the languages it supports, Julia, Python, and R.

How to Install Jupyter notebook in our System?

We can install the Jupyter notebook using the pip command for different OS (Windows, Mac, and Linux) like this:

pip install jupyter

We only need to install Anaconda on our systems, and Jupyter notebook comes preinstalled.

Now that we have installed it, let’s learn about the essential functions it supports. We will start with starting the notebook server, renaming it, and continuing to understand more about the available ‘menu’ options.

Jupyter Notebook Basics

To work with commands and codes in Jupyter notebook, we need to know it’s working. So let’s first create a notebook, and then we will write our simple programs in it. For creating a notebook, the foremost step is creating a Jupyter server. Jupyter provides a web-based interactive computational environment. Hence we need a server-client mechanism, where the server will provide the backend support from the respective OS, and the client will be the frontend for the web application. 

Starting the Jupyter server

We want our web application (frontend) to be pointing at some root folder, hence we first go to that particular folder location, open the terminal and then use the following command:

jupyter notebook

It will start Jupyter by opening our default browser with the URL: http://127.0.0.1:8888/tree . Our screen will look as shown below:

How to start the Jupyter-server in Jupyter notebook?

Please note that we have just started running a notebook server, not an exact notebook, so let’s see how to create one.

How to create a notebook?

On the right side of the image above, we can find the “New” button in between the “Upload” and “refresh” buttons. Once we click on the ‘New’ button, it will ask to start the notebook as a Python2 kernel or Python3 kernel. If our system has both Python2 and Python3 installed, we can choose as per our project’s needs. Let’s select Python3 for now. A new tab will open in the browser with a clean new notebook, as shown below.

How to create a notebook in Jupyter-notebook?

The default name for this notebook file would be “Untitled” which can be confusing. So let’s change the name of this notebook.

How to rename the notebook?

The image above shows that the filename (written beside the Jupyter logo) is ‘Untitled1’. We can click on this name which will open a dialogue box that can be used to rename the file, as shown below:

How to rename the notebook in Jupyter-notebook?

We gave the name “Jupyter intro” to the file. This will create a blank notebook file which we will use to run Python code. Let’s see how we can do that.

How to execute the cells in Jupyter notebook?

We can see an empty cell with In [ ]: in a new notebook where we can write the python code. The base language is the same one we chose while selecting the notebook, i.e., Python3 or Python2. For example:

In [ ]: print ("Hello World")

After writing the code, we can run the cells by choosing the Run button from the row of buttons on the top. We can also press the shift + enter key to run the cells. The notebook file will look as follows:

How to execute a cell in a Jupyter-notebook?

Do note that the left side of each cell has square brackets. These brackets are empty for a new cell. As we continue to run the cells, they will be filled with a number that indicates the order in which they were executed.
Let’s learn about the main options present in the “menu” in Jupyter, which are essential for us to use notebooks efficiently.

Menu options available in Jupyter Notebook

Various options are present in the menu of Jupyter. Let’s start with the File option:

File

  • New Notebook: It is used to create new notebooks for the project per our needs.
  • Open: This command redirects our local host and helps us to open already present files in our system.
  • Make a copy: This helps to make a copy of our current notebook and is helpful while performing some experiments with an existing notebook. Note that the copy is in the same folder as the original notebook.
  • Save as: This is used to save with a new name or to a new path.
  • Rename: This command can be used to change the name of the notebook. We can also use the UI method mentioned before.
  • Save and Checkpoint: It saves our progress and saves our states as checkpoints of the file. The shortcut key ctrl + s will perform an equivalent action.
  • Revert to Checkpoint: It helps to revert to our previously saved state if we make mistakes.
  • Print Preview: Used to get only the code part of the Jupyter Notebook for a clean print.
  • Download as: Allows us to download our Jupyter Notebook in various formats. It is beneficial when we want to share with non-technical people. This command helps us to export the Jupyter notebook in the desired format. We have the following options available → HTML, LaTeX, PDF, RevealJS, Markdown, ReStructured Text, and Executable script. It’s similar to exporting the notebook to the required format.We can also use the nbconvert tool, which uses the Jinja template to convert the notebook (.ipynb) format to the above formats. The syntax for that is:
jupyter nbconvert <input notebook> --to <output format>

'''Please note that Jinja is a template made for Python, 
and it depends on Pandoc and Tex libraries used for 
converting from one format to another. So to make sure that 
nbconvert works well, we need to ensure that corresponding 
dependencies are installed.'''
  • Close and Halt: Ends our current running session of the Jupyter Notebook and returns us to the local host.

Let us look into the Kernel option of the menu in the next section.

Kernel option of the Jupyter Notebook’s Menu

A notebook kernel executes the code as mentioned in the notebook. Whenever we run a program, it consumes the RAM. This RAM is freed once the kernel is shut-down.

Let us analyze the various options available in the kernel that are helpful.

  • Interrupt: Used to stop the execution of a cell. It is mainly used when we have got our desired results or made an error.
  • Restart: Used to restart the kernel of the notebook
  • Restart and Clear Output: Used to restart the kernel, and all variables are reset. Also, all the output we previously had was lost.
  • Restart and Run all: Used to restart the kernel, and all the cells are re-run starting from the first cell.
  • Reconnect: Used to reconnect to a dead kernel (The kernel might be dead due to lack of memory which occurs when the code run is too heavy. It can also happen if we are using Jupyter online and our network connection with the server was interrupted due to some reason)
  • Shutdown: Used to shut down the current working Kernel of the Notebook.

We have looked into two options for the menu in detail, and these are the frequently used ones. Let’s look at what other options are present on the menu.

Other Menu Options in Jupyter Notebook

What are the various menu options in Jupyter-notebook?

  • Edit: Used for performing operations like adding, cutting, moving, or deleting cell blocks.
  • View: Used to change the outlook for users by allowing us to remove various headers, toolbars, etc.
  • Insert: Used to insert a new cell. We can specify the position of the new cell with respect to the current cell from the menu.
  • Cell: Gives different options while executing a cell
  • Widgets: We can use widgets to build interactive GUIs for your notebooks.
  • Help: Provides help related to Jupyter. We can also view the keyboard shortcut list from here.

Now let’s discuss the toolbar and its various icons frequently used while working with Jupiter-notebook.

The toolbar in Jupyter Notebook

Let’s list the options available from left to right in the toolbar, just below the menu bar. These icons are frequently used while working with the notebook.

What functionalities does toolbar provides in Jupyter notebook?

  • Save and Checkpoint: It is used to save the current file and create a checkpoint. The shortcut key ctrl + s will perform an equivalent action.
  • Insert Cell Below: Adds a new cell below.
  • Cut Selected Cells: Deletes the selected cells.
  • Copy Selected Cells: Copies the selected cells.
  • Paste cells below: Paste the copied cells below the current cell
  • Move the selected cells upward.
  • Move the selected cells downward.
  • Run: Executes the particular selected cell.
  • Interrupt: Used to stop the execution or running of a particular cell.
  • Restart: Used to restart the Kernel of the Notebook.
  • Restart and Run all: Used to restart the kernel, and all variables are reset. Also, all the output we previously had is lost. Then all the cells are executed from starting.
  • Allows you the option to convert the type of cell into your desired type ranging from Code, Markdown, etc.
  • Command Palette: Opens the command palette

Let us look into tabs present on the home page and their uses.

Viewing What’s Running

There are two tabs present on the home page of the server (where we create the server) Cluster and Running. The running tab lets us view all the running files. It helps us to save the files before closing the server. Usually, this saving is unnecessary as the notebook is autosaved at frequent intervals. It is also helpful to free the RAM if we close some unused notebooks.
Let us look into different cell types in the next section.

Playing with cells in Jupyter

We have three types of cells: Code, Markdown, and Raw NBConvert.

  • Raw NBConvert type of cell is only used when we use the nbconvert tool. It helps us to define the formatting while converting the file from one type to another.
  • Code cell type is where we type the code and execute it. We have seen this type before throughout this blog.
  • Markdown is a markup language that is a superset of HTML. It is commonly used to add text in a formatting style, making it easier to understand notebooks.

Markdown and Code are the most commonly used types of cells. Let’s see Markdown in more detail.

Markdown cell in Jupyter notebook

The operations below are performed when the cell type is markdown. Let's see them 1-by-1 and their corresponding result in the final image in last.

  1. We can enter any text in the cell and execute it, and the output will appear beautifully.
  2. Bold text can be added using a double underscore or double asterisk.
  3. Headers can be added using “#”. The more the number of ‘#’ used, the smaller heading we get.
  4. A list can be created using a plus sign, asterisk, or dashes.
  5. Code can be inserted that we want to use for reference, but we do not want to run it. If we add it as a comment, we lose all formatting that comes while we write the text. So we can use ‘`’ (backticks) to add the code. We can use triple backticks to add a set of codes.

Tips to operate cells in markdown mode used for documentation.

Till now, utilities have been learned, but extra functionalities which make it easier to use still need to be added, like a tool that gives us a table of content. We use extensions for this purpose. Let us learn more about extensions in the next section.

Jupyter notebook extensions

Jupyter supports four types of extensions:

  • kernel
  • notebook
  • notebook server
  • IPython kernel

The most commonly used extension is the IPython kernel for notebooks. We can see the extensions if we have them installed beside the clustering tab as an extension tab. If this tab is available, a suitable extension can be chosen once we click on it.

The extension is not on our computer or enabled if this tab is unavailable. This can be installed using pip in most cases. If pip cannot be used, the following command needs to be used.

jupyter nbextension install extension_name

Installing does not mean that the extension can be used. It needs to be enabled. To enable it, the following command needs to be typed.

jupyter nbextension enable extension_name

A standard extension in Jupyter would be ‘Table of contents, which, when used, helps us to see our notebook in table content format. If needed, a hyperlink can also be added to the table of contents.

Conclusion

Jupyter-notebook is the most used IDE for teaching Machine Learning and Data Science applications. Keeping that in mind, in this blog, we discussed the basics of using the Jupyter-notebook, which will be used in our course to execute Python codes of ML and Data Science projects. We hope you find the article informative and enjoyable.

Enjoy Coding! Enjoy Algorithms!

More from EnjoyAlgorithms

Self-paced Courses and Blogs