Using Jupyter Notebooks

Using Jupyter Notebooks1

Duane A. Bailey September 16, 2020

In Introduction to Computer Science, many of our lectures make use of Jupyter Notebooks. These documents are an example of literate programming, an approach to writing programs that integrates code and commentary in a cohesive, human-readable document. The approach was introduced and popularized by Donald Knuth.2 Installation. This document assumes you have some familiarity with the unix operating system, perhaps through the "terminal" interface on the Mac. Also, if you don't have controlling access to your machine, you may have to use virtual environments; we assume that you're using that environment, if necessary.3 First, check to see if you have the Jupyter package installed by typing the following in a terminal window (the $ is your unix prompt):

$ type jupyter If it is installed, you will get a response similar to:

jupyter is /usr/local/opt/python@3.8/bin/jupyter If it is not installed, you will get this response:

-bash: type: jupyter: not found (If it does not appear to be installed, perhaps you typically use a virtual environment? If you do, make sure you're in that environment.)

If Jupyter is not installed in your environment, you must install it. The easiest way is to use the Python installer, pip:

$ sudo pip3 install jupyter (If pip3 is not installed, you can fall back to this command:

$ sudo python3 -m pip install jupyter which works in most versions of python3.) After downloading and installing several packages, the Jupyter eco-system will be installed. Rechecking, at this point, will demonstrate that Jupyter is installed. First Experiments. When Jupyter is used in a lecture situtation, your instructor may provide a link to the associated notebook. That link may be provided in the video, or it may be found on the same page you visited to launch the video. Sometimes, the file is part of a zip file, that expands on your machine into a folder. The appropriate file has a suffix of .ipynb.4

Ideally, you will use your notebook in a dedicated directory. For example, if you store your coursework in a directory called cs134, you might keep a sub-directory called play where you can run little experiments. The use of Jupyter notebooks is entirely consistent with the idea of small, controlled experiments.

Drop a copy of your notebook into your play folder. Here are some ways you may be able to do that: 1. If you're in a browser window that has a link to a notebook, select the link to download the file. In Chrome on

a Mac, for example, control-click on the link to bring up a menu and select save link as....

Downloading a Jupyter notebook.

1This document is . 2Donald E. Knuth. Literate Programming. CSLI Lecture Notes Number 27. Stanford University Center for the Study of Language and Information, Stanford, CA, USA, 1992. 3We have a document that describes how (and when!) to use a virtual environment for Python at ~bailey/venv.pdf. 4This stands for Iron Python notebook ; Iron Python is an associated project that uses the same notebook structure.

2. If, instead, you're in a terminal window, you can use curl to download the file. You must know the URL associated with the notebook (replace our example URL in commands that follow):

$ curl -O

This will download the notebook, writing the output (-O) to a similarly named file. 3. You can use, if you wish, the command wget:

$ wget

On occasion, a notebook may have supporting data files associated with it. In those cases, we will either explicitly link to those files, or we will use a compression program (like zip) to ship the notebook and its associated file as a bundle. We may, in those cases, store the notebook and supporting files in a dedicated directory. If that is the case, that directory serves as a dedicated, disposable experiments folder. Launching Jupyter. To make the fullest use of a Jupyter notebook, we will ask Jupyter to open the notebook, explicitly. Let's assume your notebook is called lecture.ipynb, stored in your folder ~/cs134/play. Go to the folder and start the notebook. (Again, if you're using virtual environment, make sure it has been activated.)

$ cd ~/cs134/play $ jupyter notebook lecture.ipynb Jupyter will read the indicated notebook and launch a browser (if necessary) to view the contents of the notebook: $ jupyter notebook lecture.ipynb [I 13:46:37.266 NotebookApp] Serving notebooks from local directory: play [I 13:46:37.266 NotebookApp] The Jupyter Notebook is running at: .... output deleted .... [I 13:46:37.266 NotebookApp] Use Control-C to stop...(twice to skip confirmation). [C 13:46:37.279 NotebookApp]

To access the notebook, open this file in a browser: ....

Or copy and paste one of these URLs: ....

or .... [I 13:46:39.771 NotebookApp] Kernel started: .... At this point, the browser automatically starts up, with the notebook open.

lecture.ipynb, launched in a browser.

Shutting down. When you want to save your work and shut down the notebook, (1) select Save and checkpoint from the File menu, (2) in the terminal window, type control-C twice.

2

Going through the motions. Notebooks are meant to be interactive. Ideally, you read the notebook, top to bottom, running or evaluating the experiments or cells along the way.

Here is how notebook designers expect you will approach your notebook. 1. Go to the Cell menu, select All Output..., then Clear. This will clear the output from experiments that

may have been left behind by your instructor, or whoever used the notebook last. 2. There are two types of cells: markdown--formatted text on a white background--and code--typically written

in Python on a gray background. The extent of the cell is determined by a rectangular box. The current cell is highlighted in blue. 3. Progress through the notebook, evaluating each cell in turn. To evaluate a cell, type Shift-Return (sometimes called Shift-Enter). When you evaluate markdown cells, the text is formatted. When you evaluate code, the code is executed. As cells are evaluated, the highlighted current cell moves down the page. 4. Beside each evaluated code cell is an input serial number, In [ ]:. If the cell has not been evaluated, the brackters are empty. As you evaluate code cells, numbers appear within the brackets. This number increases for every evaluation. It can be helpful in understanding the order that cells were evaluated. We try to make notebooks so that they are evaluated from top to bottom. 5. Make sure you read the running text. Some notebooks may require some active involvement. You may have to run experiements, or modify code. Sometimes it is useful to be able to evaluate a cell and not move on. You can do this with Command-Enter on a Mac. For example, a cell may have behavior that is random, or not predictable. Evaluating a cell several times can be helpful in understanding its subtle behaviors. 6. When you are done using a notebook, you can select Save and Checkpoint from the File menu, and then Close and Halt. The next time you return, the notebook will have the outputs from before. It will be necessary, though, to evaluate the cells, again, from the very top of the notebook. In general, you can think of a notebook as a collection of code cells with interspersed commentary, written in markdown. Code cells are written in an ordered fashion to be evaluated top to bottom, just as would be the case if the code had been written in a script. The hand evaluation of cells lets you watch the effects of calculations, as they unfold. The final result, though, is simply the evaluation of all the code cells from top to bottom. You can force the notebook to be completely evaluated from top to bottom by selecting Run All from the Cell menu.

3

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download