Jupyter Notebook Tutorial: Installation, Components and Magic Commands – Part II

Articles From: QuantInsti
Website: QuantInsti

See Part I for an overview of Jupyter Notebook.

What are the different components of Jupyter notebooks?

I assume that you are following the chronological order of this article. If so, then you have learned what Jupyter notebooks are, how to install it, and how to run and open it. If not, then I would recommend you to go through them to get an overall picture. Nevertheless, if you are already familiar with those parts, go on and keep learning.

In this section, I will explain the various components of Jupyter software. When you start Jupyter Notebook application, and you’ll be presented with the homepage. Let’s start exploring it. Below shown is the snapshot of the homepage that you’d seen earlier, but with numbers assigned to each component to make our learning easier.

Jupyter Notebook Tutorial - QuantInsti

Here’s the description of each numbered point shown in the snapshot.

  1. It is the URL on which Jupyter server is running. If you are running the Jupyter on localhost, this would be the same URL shown in the console when you started Jupyter software.
  2. The Files tab lists the directories and files in the home folder, which usually is the home directory of the user logged in to the computer.
  3. The Running tab shows you a list of all open notebooks. When you start a new notebook or open an existing notebook, a kernel will get attached to it. All such running kernels will be listed under this tab.
  4. If you want to open an existing Jupyter notebook(that ends with .ipynb extension), it needs to be listed in the Files tab. If it is not listed, you need to upload it using the Upload button which will open a file browser for you to load the file.
  5. Quit and Logout buttons allow you to logout and shutdown the server. When you quit, all the opened notebooks will be closed, and the server will get shutdown.
  6. The New button allows you to create a new notebook, text file, folder, or terminal. The snapshot shown below shows that you can create a notebook in Python, Julia, and R language. The notebook that you create will be associated with a respective kernel. When you install Jupyter environment as shown in the previous section, it is very likely that you will have only one option for the kernel, that is, Python. In order to add new languages, you can refer to this link.
 =

You can create a new notebook by clicking on the respective language name. Regardless of what language you choose, the new notebook that you create will have the same appearance. The difference would be in terms of the kernel attached to it. If I click on Python 3 on the dropdown opened, a new notebook with Python kernel attached to it will be created. The empty Jupyter notebook is shown in the snapshot below. (Obviously without numbers :p)

 wp-image-175965

The newly created notebook has various components which are explained below:

  1. The title is the name of the notebook. The title you set becomes the file name for the notebook, and it will have the extension as .ipynb which stands for IPython NoteBook.
  2. The checkpoint shows you the time when your notebook was saved last.
  3. The menu bar lists various menus that allow you to download the notebook(in multiple formats), open a new notebook, edit the notebook, customise the headers, manipulate cells, nudge the kernel, access help and so on.
  4. The shortcut bar lists commonly used shortcuts such as save to save the notebook, add a cellcutcopy to manipulate cells, up and down to navigate between cells, run to execute the cell, and so on. Any extension that you add to Jupyter will have its shortcut on this bar. We will learn what extensions are in the latter part of this article.
  5. The Kernel shows you the current kernel associated with the notebook. In our case, the kernel is Python 3. The circle beside the kernel shows the status of the kernel. The hollow circle represents that it is ready to take input and run a cell. When a kernel is executing code or processing anything, it changes to solid.
  6. cell is the part of a notebook where all the magic happens. Cells are explained in detail in the following section.

What are cells in a Jupyter Notebook?

Any text or code that you write goes in the cell. Cells are the building block of any Jupyter notebook. Cells operate in two modes: command and edit mode, and they are of mainly three types: codemarkdown, and Raw NBConvert.

The command mode allows you to manipulate cells. That is, the action you perform has to do with the cell as a whole. The command mode is represented by a grey border around the cell with a blue indication, as shown in the below snapshot.

 wp-image-175970

Some of the operations (along with their shortcuts) that you can perform when a cell is in the command mode are as follows:

  • Insert a new cell — Key in A to insert a new cell above, and B to insert a new cell below the current cell.
    • When a new cell is inserted, it will be a code type cell, by default. We will look at various types in a while.
  • Merge existing cells — Shift-M allows to merge selected cells or to merge the current cell with the cell below the current cell.
  • Copy cells — C copies selected cells.
  • Cut cells — X cuts selected cells.
  • Paste cells — Use Shift-V to paste cells.
  • Delete existing dells — Pressing DD deletes the current cell. Be careful with this shortcut.
  • Change the type of a cell — The shortcut Y changes the cell type to codeM changes the cell type to markdown, and R changes the cell type to raw.
  • Convert the cell to a heading — There are six heading types in a Jupyter notebook. This works with markdown cell type only. Heading 1 is the largest heading and heading 6 is the smallest heading. 12345, and 6 are used to change the cell type to the respective heading size.
  • Find and Replace in existing cells — Pressing F opens find and replace dialogue box.
  • Save and mark the Checkpoint of the notebook — Use Shift-S to save the notebook.
  • Toggle line numbers in a cell — Pressing L toggles line number in the current cell.
  • Toggle the output of a cell — O allows you to toggle the output of the current cell.
  • Interrupt the kernel — Keying II interrupts the kernel. That is, if any process is being executed by the kernel gets stopped.
  • Scroll the notebook — Space scrolls the notebook down, Shift-Space scrolls the notebook up.
  • Enter the edit mode — Pressing return key changes the mode of a cell to the edit mode.

The shortcuts mentioned above work only in the command mode cells. Another mode that a Jupyter notebook cell supports is the edit mode. This mode specifically allows you to edit the content of a cell and work with it. You can enter into the edit mode of a cell by pressing the return key or by a mouse click inside a cell. The border around cell changes to Green when the cell is in the edit mode, as shown in the below snapshot.

 wp-image-175974

Once the cell is in edit mode, you can start writing code or text. The below-mentioned are some of the operations that you can perform while the cell is in the edit mode.

  • Auto-Completion of code — Use Tab key to use this facility. It works only for code type cells. In markdown cells, it will simply put tab spaces.
  • Indentation of code — Jupyter notebooks inherently perform indentation whenever required. However, if you want to change indentation manually, use Ctrl-] to indent the code in code type cells. In markdown cells, it will insert spaces according to the specifications of the tab key.
  • Dedentation of code — Anytime you want to dedent the code, use Ctrl-[ to do so. In markdown cells, this shortcut works similar to Shift-Tab and dedents the content.
  • Commenting a code — Use Ctrl-/ to comment a code. In markdown cells, this shortcut does not have any effect.
  • Execute of a cell — Once you write code or text in the cell, you need to execute it to process the content written by you. There are three primary ways to do so.
    • Run the current cell and select the next cell – Use Shift-Enter to perform this action.
    • Run selected cells – Use Ctrl-Enter to run selected cells or the current cell.
    • Run the current cell and insert a new cell – Press Alt-Enter to execute the current cell and insert the new cell below the current cell.
  • Split a cell — Use the shortcut Ctrl-Shift-Minus to split the current cell into two separate cells at the cursor.
  • Entering a Command Mode — Use Ctrl-M or press Esc key to exit from the edit mode and enter into the command mode.

By now, you have already encountered code and markdown cell types quite a few times. In case you are not aware of the two, now is the time where I will explain them in detail. I will restrict the discussion for these two types only; I won’t be covering the Raw NBConvert type in this article.

Jupyter notebook cells can be multiple types. Often used types are code and markdown. The code type cells allow you to write live programming code. That is, you can perform any sort of programming in them. Once you run or execute a code cell, Jupyter notebook will present the output just below the cell. This is shown in the below snapshot.

 wp-image-175975

In contrast, whatever written in the markdown cell, will get printed in the cell itself, as shown below:

 wp-image-175976

There are two cells in the above snapshot. The first cell numbered four, is the code cell, which allows tying in Python code as we are working with Python kernel notebook. The next cell is the markdown cell where the normal text is written.

As can be seen, code cells have a number associated with them, whereas markdown cells do not have any numbering. Numbering code cells helps in two ways: First, it shows the sequence in which code executed, and second, it allows us to differentiate between the code cells and markdown cells visually. Now that you know the basics of cells and their workings, let’s see how you can use markdown.

Stay tuned for the next installment to learn how to write down in Markdown in Jupyter Notebook.

Originally posted on QuantInsti Blog: https://blog.quantinsti.com/jupyter-notebook-tutorial-installation-components-magic-commands/.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.