Lab 2: Advanced Python
Due Wednesday, September 16th by 11:59pm

Python logo

Learning Goals

Starting point code

To access the starter code, log on to the Swarthmore Github Enterprise site: github.swarthmore.edu, and find your Git repository for this lab, which should be called something like "CS33-F26/lab02-USERNAME", only with your own actual username in place of 'USERNAME'. Open the repo by clicking on the name, then click the green "Code" button, make sure "SSH" is selected, and copy the URL which should be "git@github.swarthmore.edu:" followed by your repo name.

Next, open a terminal, navigate to your CS33 directory, type git clone, then paste the URL for your repo and run the command. This should create a new directory with a copy of the lab files. Run ls in this new directory to make sure the files are there; in this case, make sure you have a file called python_warmup.ipynb.

Running Jupyter

Once you've got the scaffolding, you'll need to launch Jupyter Lab to open it. There are two steps; first, you'll need to activate a Python virtual environment (which contains all the packages we'll be using this semester), then launch the program. It should look something like this (where $ represents the prompt):
$ source /usr/swat/bin/cs33env
(cs33-f26) $ jupyter-lab 
Note the (cs33-f26) in front of the prompt on the second line; this is a reminder that you're in the virtual environment. If you get a 'command not found' error when you try to run jupyter-lab, it's probably because you forgot to source the ML environment first. If you want to get out of the virtual environment, use the command deactivate.

Remember that you'll need to run these commands each time you open a new terminal you want to run Jupyter Lab from.

When you run Jupyter Lab, it should launch a web browser (or open a tab in an existing web browser); this is the main interface. If this doesn't happen automatically, open a browser and paste in the link that will show up in the terminal where you ran the jupyter-lab command.

Jupyter Lab has a built-in file browser on the lefthand side, which by default starts in the directory you were in when you ran the jupyter-lab command. If you ran the command in your home directory, you'll need to navigate to your lab 2 directory. If you ran the command when you were already in that directory, you shouldn't need to change directories.

NOTE: It is generally recommended that you run the jupyter-lab command in your cs33 directory; that way you can access all your course related files without having to close and re-open the program (e.g. if you're working on lab 3 and want to look back at your code from lab 2).

Open the python_warmup.ipynb file by clicking on it; this should cause the lab to appear in the large pane on the right. The file is an Interactive Python NoteBook file, and it's the a format we'll be using for several labs this semester. We'll talk during the lab period about some of the functions of this format and why we're using it.

Completing the Lab

All the instructions for the lab are included in the Jupyter Notebook. The Notebook format allows for mixing instructions, example code, explanations, solution code, and output, all in the same document.

Submitting

When you are finished, use git to add, commit, and push your work. In this case, all your solutions should be contained within python_warmup.ipynb. Remember to also add/commit/push on a regular basis; you should get into the habit of pushing changes to Git at the end of each session working on a lab.

Remote access for Jupyter

It's possible to run Jupyter remotely over SSH and connect to it from your local web browser, but you need to perform a few extra steps to make this work:

  1. Open a command terminal on your local computer
  2. Start a remote session using SSH, but specify a port to connect with the -L flag:

    ssh username@cslab.cs.swarthmore.edu -L 6633:localhost:6633

    Be sure to use your own username. Note that if two users try to use the same port number on the same host you'll have a conflict, so pick a random 4-digit number above 2000 to use for yourself. Be sure to use the same number on both sides of the word 'localhost'.
  3. Start the cs33env as usual.
  4. Tell jupyter lab to use the port you picked when you start it:

    jupyter-lab --port=6633 --no-browser

    Be sure you use the same port number as you did when you started SSH. The last bit tells it not to try to launch a web browser on the remote host.
  5. Make sure that jupyter was actually able to open the port you asked for; if it fails, it will pick a different one...which won't help you at all. If this happens, log out and try to SSH back in using a different port number.
  6. Copy one of the URLs and paste it into the location bar in a web browser on your local computer.

At this point, you should see jupyter lab and be able to interact with it as usual; all your code will be exectued on the remote CS machine, so you don't need to install or configure anything on your personal computer.