Python Tutorial
Due Sunday, Jan. 28 by 11:59pm

This first lab will be a "warmup," intended to refresh your memory of the Python programming language, as well as to familiarize you with several libraries and tools we will be using this semester.

Starting point code

For this lab you will work individually.

We will be using GIT for all labs; if you need a refresher, check https://www.cs.swarthmore.edu/git/

Do the following steps to get the starting point code. Replace USERNAME with your own username. The $ represents the command prompt.

  1. Connect to the CS machines by doing ONE of the following:

  2. Make a new directory for this class and change into that directory:
    $ mkdir cs63
    $ cd cs63

  3. Clone the repo with the starting point code:
    $ git clone git@github.swarthmore.edu:CS63-S24/lab0-USERNAME.git

  4. While you're at it, clone the repo we'll be using for the reading journals as well:
    $ git clone git@github.swarthmore.edu:CS63-S24/reading_journal-USERNAME.git

Launching Jupyter

For this lab, we'll be using a development environment called Jupyter. The primary advantage of this platform is that it provides "notebooks," which are a way of interweaving Python code segments and their output with descriptive text, equations, graphs, and more.

Like any tool, Jupyter has its advantages and disadvantages, and we won't be using it for every lab this semester, but it's an excellent fit for the goals of this warmup lab.

To run Juypter on one of our CS machines, you just need to do the following:

The first command configures the Python environment so you'll have access to all the packages that we'll be using this semester; it needs to be run once (in any given terminal) before jupyter-lab. If you forget to do this, then jupyter-lab likely won't launch at all; if you get a command not found error, it's probably because you forgot to source the correct Python virtual environment.

The source command should actually change your command prompt, putting a (CS63env) in the front; this is a reminder that it's worked correctly, and you can run Jupyter.

When you run jupyter-lab, it should launch a web browser (or open a tab in an existing browser window). There should be a file browser in the panel on the left; the root directory will be whatever directory you ran the jupyter-lab command in (if you followed the directions above, this should be your cs63 directory). Navigate to the Lab 0 directory and double click on the file named python_warmup.ipynb; doing so should open the notebook in the large panel on the right.

From here, all the remaining instructions are in the notebook; start reading at the top, and follow the directions in the notebook.

When you're finished, you can quit Jupyter by clicking the "File" menu and selecting "Shut Down" at the bottom. Alternatively, if you type [Ctrl-c] twice in the terminal you ran the command in, that will also shut down the program.

Note that if you want the (cs63-s24) to go away, you can deactivate the virtual environment:

  $ deactivate
However, this is not strictly necessary, since the virtual environment really only effects the versions of Python and Python libraries; all other commands should work normally regardless of whether you are in the CS63 environment or not.

When you're done, be sure to remember to use GIT to add/commit/push the changes you've made!

(OPTIONAL) Launching Jupyter Remotely

If you want to run Jupyter remotely (i.e. over an SSH connection, as opposed to on a machine you're physically logged in to), you'll need to add a couple of extra steps to the normal process. This is because Jupyter Lab is actually running a web server that your browser connects to; if the server and the browser are on the same machine, this happens automatically, but if you're connecting remotely you need to tell the two machines how to connect your local browser to the remote server.

The steps for running Jupyter remotely are as follows:

  1. Open a terminal on your local machine. This will let you use SSH to connect to the CS machines. All computers these days come with a terminal that has built in SSH; on Mac it's the "Terminal" app, on Windows it's the "Command Prompt" app, and on Linux it's whichever terminal emulator is your favorite (there's lots of options). The following commands should work the same way regardless of which terminal app you use.

  2. SSH to a CS machine, and specify a port to forward. Note that which port you use doesn't matter, so long as that port is not already in use. This means you shouldn't use a "standard" port number (e.g. 8080), and you need to avoid using the same port as another student logged in to the same machine (e.g. if everyone in class used "1234" there would be conflicts). I recommend using 63 (for our course) followed by the two-digit day-of-the-month of your birthday (as a random-ish two digit suffix), but you can come up with your own strategy.

    In the following command, XXXX stands for the port number, and USERNAME stands for your username:

    $ ssh -L localhost:XXXX:localhost:XXXX USERNAME@cslab.cs.swarthmore.edu

    This needs to be done on your machine! If you connect to a CS machine first and then run this command, it won't work; it's the initial connection from your personal machine to the CS network that needs to have the port-forwarding configured properly.

  3. Once you've logged in, you can run Jupyter similar to the way you normally would, expect you need to tell it what port to use (this needs to match the port number you used when you ran the SSH command:

    $ source /usr/swat/bin/CS63env
    $ jupyter-lab --port=XXXX --no-browser

    The first argument is the port number, replace XXXX with the port you're using. The second argument tells it not to automatically launch a web browser (which won't work over a standard SSH connection anyway).

  4. Once the Jupyter server is running, you should see some lines near the bottom that look something like:
        To access the server, open this file in a browser:
            file:///home/mitchell/.local/share/jupyter/runtime/jpserver-581164-open.html
        Or copy and paste one of these URLs:
            http://localhost:6311/lab?token=833c29214af009d97714c2a852333e47cb8a954dba7d9e7d
         or http://127.0.0.1:6311/lab?token=833c29214af009d97714c2a852333e47cb8a954dba7d9e7d
         
    The details will vary slightly (i.e. you won't have the same port and token number).

  5. Copy and paste one of these lines into the address bar in a web browser and hit enter; this should connect you to the Jupyter server running remotely. From that point on, everything should work just the same as it would if you were sitting in the lab.

  6. If you're working this way, remember that you'll need to do your GIT commands on the CS machine, not your local computer! One way to do this is to open a terminal inside Jupyter Lab; if you click the "File" menu and select "New Launcher", you can then select "Terminal" under the "Other" category. This will give you a terminal on whatever machine you ran the Jupyter Lab command on; you can do normal terminal things in it, including running GIT commands.