How to Write and Execute Code in a Jupyter Notebook

Writing and Executing Code in a Jupyter Notebook

The Anaconda Python Distribution that you installed in the Before You Begin section comes with the Jupyter Notebook—an interactive, browser­based environment in which you can write and execute code and intermix the code with text, images and video. Jupyter Notebooks are broadly used in the data­science community in particular and the broader scientific community in general. They’re the preferred means of doing Python­based data analytics studies and reproducibly communicating their results. The Jupyter Notebook environment supports a growing number of programming languages.

For your convenience, all of the article’s source code also is provided in Jupyter Notebooks that you can simply load and execute. In this section, you’ll use the JupyterLab interface, which enables you to manage your notebook files and other files that your notebooks use (like images and videos). As you’ll see, JupyterLab also makes it convenient to write code, execute it, see the results, modify the code and execute it again. You’ll see that coding in a Jupyter Notebook is similar to working with IPython—in fact, Jupyter Notebooks use IPython by default. In this section, you’ll create a notebook, add the code from Section 1.5.1 to it and execute that code.

Opening JupyterLab in Your Browser

To open JupyterLab, change to the ch01 examples folder in your Terminal, shell or Anaconda Command Prompt (as in Section 1.5.2), type the following command, then press Enter (or Return):

jupyter lab

This executes the Jupyter Notebook server on your computer and opens JupyterLab in your default web browser, showing the ch01 folder’s contents in the File Browser tab at the left side of the JupyterLab interface:

The Jupyter Notebook server enables you to load and run Jupyter Notebooks in your web browser. From the JupyterLab Files tab, you can double­click files to open them in the right side of the window where the Launcher tab is currently displayed. Each file you open appears as a separate tab in this part of the window. If you accidentally close your browser, you can reopen JupyterLab by entering the following address in your web browser

Creating a New Jupyter Notebook

In the Launcher tab under Notebook, click the Python 3 button to create a new Jupyter Notebook named Untitled.ipynb in which you can enter and execute Python 3 code. The file extension .ipynb is short for IPython Notebook—the original name of the Jupyter Notebook.

Renaming the Notebook

Rename Untitled.ipynb as TestDrive.ipynb:

1. Right­click the Untitled.ipynb tab and select Rename Notebook.

2. Change the name to TestDrive.ipynb and click RENAME.

The top of JupyterLab should now appear as follows:

Evaluating an Expression

The unit of work in a notebook is a cell in which you can enter code snippets. By default, a new notebook contains one cell—the rectangle in the TestDrive.ipynb notebook—but you can add more. To the cell’s left, the notation [ ]: is where the Jupyter Notebook will display the cell’s snippet number after you execute the cell. Click in the cell, then type the expression

45 + 72

To execute the current cell’s code, type Ctrl + Enter (or control + Enter). JupyterLab executes the code in IPython, then displays the results below the cell:

Adding and Executing Another Cell

Let’s evaluate a more complex expression. First, click the + button in the toolbar above the notebook’s first cell—this adds a new cell below the current one:

Click in the new cell, then type the expression

5 * (12.7 ­ 4) / 2

and execute the cell by typing Ctrl + Enter (or control + Enter):

Saving the Notebook

If your notebook has unsaved changes, the X in the notebook’s tab will change to . To save the notebook, select the File menu in JupyterLab (not at the top of your browser’s window), then select Save Notebook.

Notebooks Provided with Each Chapter’s Examples

For your convenience, each chapter’s examples also are provided as ready­to­execute notebooks without their outputs. This enables you to work through them snippet­by­snippet and see the outputs appear as you execute each snippet.

So that we can show you how to load an existing notebook and execute its cells, let’s reset the TestDrive.ipynb notebook to remove its output and snippet numbers. This will return it to a state like the notebooks we provide for the subsequent chapters’ examples. From the Kernel menu select Restart Kernel and Clear All Outputs..., then click the RESTART button. The preceding command also is helpful whenever you wish to re­execute a notebook’s snippets. The notebook should now appear as follows:

From the File menu, select Save Notebook, then click the TestDrive.ipynb tab’s X button to close the notebook.

Opening and Executing an Existing Notebook

When you launch JupyterLab from a given chapter’s examples folder, you’ll be able to open notebooks from that folder or any of its subfolders. Once you locate a specific notebook, double­click it to open it. Open the TestDrive.ipynb notebook again now. Once a notebook is open, you can execute each cell individually, as you did earlier in this section, or you can execute the entire notebook at once. To do so, from the Run menu select Run All Cells. The notebook will execute the cells in order, displaying each cell’s output below that cell.

Closing JupyterLab

When you’re done with JupyterLab, you can close its browser tab, then in the Terminal, shell or Anaconda Command Prompt from which you launched JupyterLab, type Ctrl + c (or control + c) twice.

JupyterLab Tips

While working in JupyterLab, you might find these tips helpful:

  • If you need to enter and execute many snippets, you can execute the current cell and add a new one below it by typing Shift + Enter, rather than Ctrl + Enter (or control + Enter).
  • As you get into the later chapters, some of the snippets you’ll enter in Jupyter Notebooks will contain many lines of code. To display line numbers within each cell, select Show line numbers from JupyterLab’s View menu.

More Information on Working with JupyterLab

JupyterLab has many more features that you’ll find helpful. We recommend that you read the Jupyter team’s introduction to JupyterLab at:

For a quick overview, click Overview under GETTING STARTED. Also, under USER GUIDE read the introductions to The JupyterLab Interface, Working with Files, Text Editor and Notebooks for many additional features.

*

إرسال تعليق (0)
أحدث أقدم