Ling/CSE 472 Using Jupyter Notebooks

Jupyter Notebooks is a technology for presenting executable chunks of code. Below is an example from your Assignment 1.

Executing code from a jupyter cell

In this example, we have taken a line of code from your Assignment 1, put it in a separate cell, and then supplied it with a fake input and output.

You can now modify and run just this chunk separately, to see what the output is at this stage and what effect your modifications have.

Another way to see what your string variable contains at each step is to run the program under the visual debugger; see below.

Jupyter Notebooks are convenient for interweaving code with commentary and illustrations, and especially for working with programs chunk by chunk without the need to create and manage separate code files or modules.

This makes this technology good for learning, though ultimately, it is important to understand the program as a whole. We will be using Jupyter Notebooks experimentally in this class, as an option.

For some of the assignments (including Assignment 1), you will get a Jupyter Notebook file containing a comlete exposition of the assignment, laid out for you such that it may be easier for you to do it (as illustrated above). You do not have to use Jupyter Notebooks for any of the assignments.

You may submit either a Jupyter Notebook containing both your code and your (nicely formatted) write up, or a python file plus a PDF with the write up. If you choose to submit a Jupyter Notebook, it must contain your full program in the last cell. Your write up, while it may be contained fully in the notebook, must be well-formatted, very well edited, and easy to read.

1. Installing Jupyter Notebooks

Mac OSX

You should be able to install Jupyter Notebooks painlessly via pip. In your terminal:

pip install --upgrade pip

pip install --upgrade ipython jupyter

You should now be able to run jupyter by typing, in your terminal (first navigating to the directory where you downloaded the notebook, or to a parent directory):

jupyter notebook

In the terminal, you should see something like this:

Starting a jupyter notebook from the terminal

You should see a directory in your browser. Navigate to the jupyter notebook file (.ipynb) and click on it.

Starting a jupyter notebook from the terminal

The notebook should open in the browser. Now you should be able to run the code in each cell by putting the cursor in the cell you want and clicking the play button. Note that you may need to execute the cells in order, if you want the state of the program to carry over from one cell to another.

Executing code from a jupyter cell

All other operating systems

Please read the installation guide and let the TA know if you cannot figure it out. There is no guarantee we will be able to set this up for everybody. If you cannot painlessly install and run Jupyter Notebooks, don't worry about it. Just go back to simply running python on your computer, be it via command line or in PyCharm or in another IDE. Please do not hesitate to post any questions about any of this on Canvas.

Optional: Working with the visual debugger

Try also installing the visual debugger. The visual debugger allows you to step through the program's execution line by line, while also inspecting the contents of each of the variables. This can make your life a lot easier, although it it entirely possible to complete all assignments in our class without using the debugger.

In your terminal:

pip install pixiedust

If all goes well, you will now be able to uncomment and run:

Running the visual debugger

...and run the program under the debugger, which you see below: Executing code under the visual debugger

You can set breakpoints in the debugger by clicking to the left of the line number, to indicate which lines of code you want the execution to pause at after you click Run/Continue, so that you can inspect the state of the program (particularly the current content of the variables). Note how you can click on the variable name string on the right and see its full contents below.

Inspecting a variable at a breakpoint

Now that the execution stopped where you wanted, you will usually want to Step Over to the next line and inspect how the variable changed.

Inspecting the variable after stepping over a line

If it seems like your notebook became confused and is not executing things properly (particularly the debugger), just restart the kernel and try again:

Restarting the kernel

Back to main course page