Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Jupyter as an Interactive Notebook

User image

Published by

sanya sanya

Published at: 21st May, 2023
11.545 mins read

How to Make Your First Notebook

In this section, we'll become comfortable with the interface, learn how to run and save notebooks, and become familiar with their structure. In this section, we'll get familiar with some fundamental terms that will let you utilize Jupyter Notebooks independently and prepare you for the next section, which talks through an example of data analysis and puts everything we've learned here into practice.

Using Jupyter

Using the shortcut Anaconda adds to your start menu, you may launch Jupyter on Windows. This will create a new tab in your default web browser that should resemble the screenshot below.

https://images.codingblocks.com/data-science/jupyter_1.png

Don't get alarmed just yet; this isn't a notebook. Not much is involved. This is the Jupyter Notebook Dashboard, which was created with management of your Jupyter Notebooks in mind. Consider it your starting point for browsing, modifying, and creating notebooks.

Be careful that the dashboard will only grant you access to the folders and files found in the start-up directory for Jupyter, which is where Anaconda or Jupyter are installed. The start-up directory can be changed, though.

The command jupyter notebook can also be used to launch the dashboard on any system using the command prompt (or terminal on Unix systems); in this case, the start-up directory will be the current working directory.

You may have noticed that the URL for the dashboard looks something like https://localhost:8888/tree while Jupyter Notebook is open in your browser. Although localhost does not refer to a website, it does show that the material is being provided from your local computer.

The dashboard and notebooks from Jupyter are web applications, and Jupyter launches a local Python server to provide these apps to your web browser, effectively making it platform-independent and facilitating simpler web sharing.

(If you're still confused, don't worry; the key point is simply that, despite Jupyter Notebooks opening in your browser, it is actually hosted and executed on your local computer. Until you choose to share your notebooks, they are not actually accessible online.)

Although we will return to it briefly later, the dashboard's interface is largely self-explanatory. So why are we still waiting? Go to the folder where you want to save your first notebook, click "New" in the upper right corner, and then choose "Python 3":

https://images.codingblocks.com/data-science/jupyter_2.png

Your first Jupyter Notebook will open in a new tab; as you can open numerous notebooks at once, each notebook uses its own tab.

When you return to the dashboard, you should notice the newly added file Untitled.ipynb and some green text indicating that your notebook is currently operating.

What is an ipynb File?

The short answer is that since each .ipynb file represents one notebook, a new .ipynb file will be created each time you create a new notebook.

The extended response: Each .ipynb file is a text document that uses the JSON encoding standard to describe the information in your notebook. Each cell contains a listing of the contents, including picture attachments that have been transformed into text strings and some information called as metadata.

If you know what you're doing, you can edit this yourself by choosing "Edit > Edit Notebook Metadata" from the notebook's menu bar. By choosing "Edit" from the controls on the dashboard, you may also access the information in your notebook files.

But the operative word there is "can." You shouldn't typically ever need to manually modify the information for your notebook.

The Notebook Interface

Now that the notebook is open in front of you, hopefully its interface won't appear wholly foreign. Jupyter is essentially just a sophisticated word processor, after all.

Why not explore the area? Take some time to browse down the list of commands in the command palette, which is the small button with the keyboard icon (or Ctrl + Shift + P), to get a feel for it. Check out the menus to get a feel for it.

https://images.codingblocks.com/data-science/jupyter_3.png

There are two fairly prominent terms that you should notice, which are probably new to you: cells and kernels are key both to understanding Jupyter and to what makes it more than just a word processor. Fortunately, these concepts are not difficult to understand.

  • A kernel is a “computational engine” that executes the code contained in a notebook document.
  • A cell is a container for text to be displayed in the notebook or code to be executed by the notebook’s kernel.

You should be aware of two very common terminologies that are probably unfamiliar to you: cells and kernels. These terms are essential to understanding Jupyter and what distinguishes it from simple word processors. Fortunately, it's not hard to comprehend these ideas.

  • The "computational engine" that runs the code in a notebook document is called a kernel.
  • A cell is a container for text that will be displayed in the notebook or for kernel-executable code.

Cells

We'll get back to kernels a little later, but let's understand cells first. A notebook's body is made up of cells. That box with the green edge in the screenshot of a new notebook in the section above represents an empty cell. There are two primary sorts of cells that we shall discuss:

  • A **code cell **holds instructions for the kernel to run. The output is shown in the notebook beneath the code cell where it was generated after the code has been executed.
  • When a Markdown cell is run, the output is shown in-place and contains Markdown text formatting.

Let's try it with the traditional "hello world" example: In the cell, type print('Hello World!'), then hit Ctrl + Enter or click the run button https://images.codingblocks.com/data-science/jupyter_4.png in the toolbar above.

The end result resemble this:

https://images.codingblocks.com/data-science/jupyter_5.png

The output of the cell is shown below, and the label to the left of it will have changed from In [ ] to In [1] when we execute the cell.

You can view the output of a code cell in this article because it is also a part of the content. Since Markdown cells lack that label on the left, you can always discern the difference between code and Markdown cells.

The label's label number and the prefix "In" stand for "Input," respectively. In this situation, the label number shows that the cell was performed first, while the label prefix "In" stands for "Input."

Because the cell was now the second to be executed on the kernel, run it again, and the label will now read In [2]. We'll examine more closely into kernels later on to explain why this is so helpful.

Click Insert and choose Insert Cell _Below _from the menu bar. To see what happens, enter the following code into the blank space below your first code cell. Do you observe any changes?

https://images.codingblocks.com/data-science/jupyter_6.png

Although this cell doesn't emit anything, it does take three seconds to run. Observe how Jupyter changes the cell's label to In [*] to indicate that it is now operating.

The value of the last line in a cell, whether it be a single variable, a function call, or something else, as well as any text data particularly produced during the cell's execution, determine the output of a cell in general. For instance:

https://images.codingblocks.com/data-science/jupyter_7.png

You'll use this practically constantly in your own projects, and we'll see more of it in the following sections.

Keyboard Shortcuts

The border of your cells changes from green to blue when you run them, as opposed to being green when you were editing. One "active" cell is always highlighted in a Jupyter Notebook and has a border whose color indicates the mode it is now in:

  • Cell is in "edit mode" if the outline is green.
  • The "command mode" cell has a blue edge.

Thus, what can we do to a cell in command mode? There are several additional commands we can use in addition to the Ctrl + Enter method we have seen so far for running a cell. Keyboard shortcuts are the ideal method for utilizing them.

The Jupyter environment's keyboard shortcuts are a particularly well-liked feature since they enable a quick cell-based workflow. Many of these can be done while the active cell is in command mode on that cell.

A list of some of Jupyter's keyboard shortcuts may be seen below. Although not all of them must be immediately memorized, this list should give you a decent notion of what is feasible.

  • Use Esc and Enter to switch between edit and command mode, respectively.
  • After entering command mode:
    • Use the Up and Down keys to scroll up and down through your cells.
    • To add a new cell above or below the currently active cell, press A or B.
    • The active cell will become a Markdown cell when you press M.
    • Choosing Y will make the active cell a code cell.
    • The active cell will be deleted if you press D twice.
    • The cell deletion will be undone by pressing Z.
    • To pick many cells at once, hold Shift while pressing Up or Down. Shift + M will combine your pick of numerous cells when you have it made.
  • In edit mode, the active cell at the cursor will be split by pressing Ctrl + Shift + -.
  • To select your cells, you can alternatively click in the margin to the left of your cells while holding down the Shift + Click key.

Try these in your own notebook without hesitation. Create a new Markdown cell when you're prepared so that we can learn how to format the text in our notebooks.

Markdown

For formatting plain text, markdown is a simple, lightweight markup language. Although prior knowledge is not required, it would be beneficial because its syntax is identical to that of HTML tags.

All of the narrative text and graphics you have thus far were created in Markdown because keep in mind that this article was written in a Jupyter notebook. Let's start with the fundamentals and use an example:

https://images.codingblocks.com/data-science/jupyter_8.png

When the cell renders it, the Markdown would appear as follows:

https://images.codingblocks.com/data-science/jupyter_9.png

(Because we didn't actually utilize a valid picture URL in our example, the alt text for the image is displayed here.)

When attaching images, you have three options:

There are three choices available when attaching images:

  • Use the web address of a picture.
  • Use a local picture URL that you will maintain with your notebook, such as one that is in the same git repository.
  • By selecting "Edit > Insert Image" while adding an attachment, the image will be converted to a string and saved within your notebook's .ipynb file. Be aware that doing this can significantly increase the size of your .ipynb file!

There are many other features of Markdown, particularly those related to hyperlinking, and you can even just add plain HTML. When you start testing the boundaries of the aforementioned fundamentals, you can consult John Gruber's official tutorial on his website.

Kernels

Every notebook has a kernel running within. The kernel executes the code when you run a code cell. Any output is transferred back to the visible cell. The kernel's state is applicable to the text as a whole rather than specific cells, and it endures over time and between cells.

For instance, if you declare or import libraries in one cell, they will be accessible in the next. To get a sense of it, let's put this to the test. We'll define a function and import a Python library first.

https://images.codingblocks.com/data-science/jupyter_10.png

Once the cell above has been performed, we can make references to np and square in any other cell.

https://images.codingblocks.com/data-science/jupyter_11.png

1 squared is 1

Whatever the arrangement of the cells in your notebook, this will still function. Any variables you declared or libraries you imported will be accessible in other cells as long as a cell has been run.

You can test it out for yourself, so let's print our variables once more.

https://images.codingblocks.com/data-science/jupyter_12.png

Is 1 squared 1?

Nothing unexpected here! What transpires, though, if we alter the value of y?

https://images.codingblocks.com/data-science/jupyter_13.png

What do you think will happen if we run the cell above?

The response will be something like Is 1 squared 10?. This is due to the fact that y is no longer equal to the square of x in the kernel once the y = 10 code cell has been performed.

The majority of the time, a notebook will be organized from top to bottom. However, going back and making modifications is usual. The order of execution, which is displayed on the left of each cell and looks like In [8], can be used to troubleshoot issues when we actually need to make changes to an earlier cell.

And from the Kernel menu, there are a number of really helpful options if we ever want to reset things

  • Restart: Restarts the kernel, erasing all previously created variables, etc.
  • Restart & Clear Output: similar to the previous step, but it will also clear the output shown beneath your code cells.
  • Restart and run all: The same as above, but all of your cells will be run in chronological sequence from first to last when you.

The Interrupt option can be used to terminate a calculation that has been stuck in your kernel.

Conclusion

Starting from scratch, we have mastered Jupyter Notebooks' natural workflow, explored IPython's more complex features. And we managed to do it all using just a notebook!

It should be obvious how using a notebook makes working more efficient by minimizing context switching and simulating the evolution of ideas across a project. It should also be clear how powerful Jupyter Notebooks are, and we covered a lot of ground to get you started investigating more sophisticated capabilities in your own projects.

Library

WEB DEVELOPMENT

FAANG QUESTIONS

On this page

How to Make Your First Notebook

Using Jupyter

The short answer is that since each .ipynb file represents one notebook, a new .ipynb file will be created each time you create a new notebook.

The Notebook Interface

Conclusion