Starting and running your first Notebook¶

Setup¶

Open Terminal and do the following:

  • if you don't yet have a CS290 directory, make one (for example: mkdir CS290)
  • change to the CS290 directory; all of your Jupyter Notebooks for this class will go in here
  • issue the command conda init bash and if it asks any yes or no questions, just hit return
  • issue the command conda activate to set up an environment for runnning the data science frameworks
  • issue the command jupyter notebook to start up the server

This last starts up the Jupyter server and will automatically start your web browser if it wasn't already running. Among other things, the server executes the code run in the notebook.


Creating the first Notebook page¶

The Jupyter Home Page is ready for you start creating. You should see something like this, but without all the files:
alt text

Click the New button near the upper right hand corner to begin a new Notebook page. It's a pull-down menu; generally we'll want a Python3 page but notice that you can create a text file, a new directory, or even open an online Terminal (try it and see...later).

Your new page looks something like:
alt text

A few cosmetic things to get started:

  • Click on the text Untitled at the top and rename with an actual title for your page (note: the title will also be used to name the notebook when saved)
  • From the pull-down menu which says Code select Markdown
  • In the first cell (labeled In [ ]:) type ## My First Notebook, then type Shift-Enter (shift key and enter key together)

Now your page should look something like this

altText

For more about the formatting you can do in Markdown cells, see this bit or this other bit

Run a Python script¶

Now to make sure of the setup, run some Python. Copy the script below into a code cell and execute by simply typing Shift-Enter:

import matplotlib.pyplot as plt

princ = 10000
int_rate = 0.05
years = 20
values = []

for _ in range(years+1):
    values.append(princ)
    princ += princ*int_rate

plt.plot(values)
plt.title('5% Growth')
plt.xlabel('Years of Compounding')
plt.ylabel('Value of Principlal ($)')

If you get a proper graph from that, terrific. Now to complete the task, save the Notebook page, then go to the File menu, select Download As and pick HTML. You can save this HTML version of the file in the same directory the notebook is in. Now in the browser you can do File -> Open File... and load the HTML version of your notebook into the browser as a static web page. Demonstrate this page to the instructor.

Publishing your Notebooks¶

In various assignments I'll ask students to publish their notebooks so 1) I can check them out and 2) they can be viewed by anyone with an Internet connection and web browser. There are many ways to do this, but the easiest is to generate an HTML file as in the previous step and copy it to the folder used by the CS Webserver.

Everyone should have a directory called html; this directory is used by our webserver to serve WWW content. Since several CS courses use the html folder, it's a good idea to create a separate CS290 folder here to keep things organized. To do this:

  • issue the command mkdir ~/html/CS290
  • if you get any kind of error message, let me know ASAP!

Now to publish any HTML file you save from a notebook you can either copy it manually to the new folder. For example:

cp 'Jupyter Notebook HOWTO.ipynb' ~/html/CS290/.

The single quotes are necessary if the filename contains spaces, which the command line can't deal with the way a GUI file viewer can.

Turns out, this is even easier when using the Jupyterlab interface (start with jupyter lab rather than jupyter notebook). The File->Save and Export Notebook As... action allows you to select the proper folder using a dialogbox.

Wait! Why can't we just store the .ipynb files created in the original CS290 folder in this same directory? For one thing, the webserver doesn't display them in any useful way. But the main issue is that the .ipynb file is essentially the source code for our HTML files. For homework, this would be like posting your solutions to the web - an Honor Code violation.

Shutting down Jupyter¶

To finish up, click the Logout button in the upper right hand corner. You can now close the tab. Go back to the tab labeled Home Page and click the Quit button. Then just to be sure, change back to the Terminal window where you started the Jupyter notebook server to begin with; typing Ctrl-C twice should shut down the server.

Finally, to leave your data science virtual environment, issue the command conda deactivate.