Skip to article frontmatterSkip to article content

Notebooks

Tutorial materials can be launched from the course homepage:

If you have not yet registered the course, visit the static site:

Tutorials are written in the form of Jupyter notebooks, which provide an interactive literate programming experience:

  • Jupyter Notebooks allow programs to be mixed with other contents, including figures, videos, and formatted textual explanations.
  • The notebooks can also run in a Jupyter server, which enables users to write and run their programs while adding formatted notes to it.
  • The Jupyter server can access local and remote large language models, which enables an AI pair programming experience that speed up tedious coding tasks.

How to run notebook cells?

If you already have your notebook opened in a Jupyter server, let’s run some code. Otherwise, jump to the next section to learn how to fetch and open notebooks.

To run a cell, select the cell and press Shift + Enter. The cell prompt with change from [ ] to [*]. When the run completes, the asterisk will be changed to a number like [1]. Try running the following cell to import the Mathematical Animation Engine (Manim).[1]

import os

if not os.getenv(
    "NBGRADER_EXECUTION"
):  # Skip the code or auto-grading may take too long to complete.
    import manim

After the import is complete, run the following cell to create an animation:

%%manim -qm --progress_bar=none --disable_caching --flush_cache -v ERROR Welcome
class Welcome(manim.Scene):
    def construct(self):
        self.play(manim.Write(manim.Text("Welcome to CS5483!")))

As you can see, the code creates a scene that displays the message Welcome to CS5483! with a writing animation.[2] To verify that the program runs live, feel free to modify the message to anything you want.

How to fetch and open notebooks?

To fetch the notebooks, follow the links to the notebooks on the course homepage such as the one in Table 1.

After clicking a notebook link, you may be asked to

  • login with your CityU account if you are not yet logged into Canvas, and
  • specify a server option if you do not yet have a running Jupyter server.

Select the Default option from the list of available Jupyter servers, and click Start to begin your session.

Each student and project group will have their own Jupyter server with individual computing resources including CPU, memory, and GPU:

  • The GPU server option(s) are available initially for testing. Once the project groups are formed, the GPU server option(s) will be available to project groups to work on projects that rely on GPU resources.
  • You can switch between different server options by restarting your server and selecting the desired option. Restarting the server is simple and can be done by
    1. selecting Hub Control Panel under the File menu, and
    2. click Stop My Server and then Start My Server.

If the server is spawned successfully, the JupyterLab interface should appear. For instance, to see the available storage and disk usage, you can run the commands within a notebook cell as follows:

if not os.getenv("NBGRADER_EXECUTION"):
    # `!` is used to execute a shell command from within the notebook
    !df .
    !du -ahd1 ${HOME} | sort -rh | head -n10

Monitor your disk usage to avoid exceeding the storage limit. The tutorial notebooks are under the course directory:

if not os.getenv("NBGRADER_EXECUTION"):
    !ls "${HOME}/${COURSE_ID}"

To prevent data loss, it’s essential to regularly download or backup your work. You may use the JupyterLab Archiver extension to download an entire folder as a zip file.

You may also run the course notebooks outside the jupyterhub server and locally on your computer. For more details, see the Docker notebook in the Appendices.

Problem Sets

The tutorial notebooks consisting of problems that provide hands-on practice to prepare you for the individual Project 1 and group Project 2.

Each problem set are are due the night before the following lecture, i.e., you normally have a week to work on the problem set.

  • You can check the due dates of all the labs from the course homepage.
  • You may seek help from us or your classmates. However, you must write your own solution and indicate who your collaborators are by including their fullnames or EIDs such as
    COLLABORATORS: xfwong2, mklee3, GPT-4o, dive:chat
    The policy applies to LLM as well.

How to complete an assignment?

The notebooks can be edited in JupyterLab to include your answers for submission. If this is your first time using JupyterLab, take a look at the official video tutorial:

For more advanced features:

  • Checkout the Help menu items
    • Show Keyboard Shortcuts to try some of the shortcuts to see their effect, and
    • Jupyter Reference to open the user guide in a new tab.
  • Try also the MyST Markdown syntax to format your notes.

As a example, you will write a “Hello, World!” program in python, which says Hello to the world:

The following code cell is a solution cell. You can simply expand the hint above and copy the answer to the solution cell instead.

def say_hello():
    # YOUR CODE HERE
    raise NotImplementedError

say_hello()

To test your program:

  • Run your program by selecting the solution cell and press Shift + Enter.
  • Then, run the following visible test to check whether your program prints the correct message.
# Run this test cell right after running your "Hello, World!" program.
import io
import sys

old_stdout, sys.stdout = sys.stdout, io.StringIO()
say_hello()
printed = sys.stdout.getvalue()
sys.stdout = old_stdout
assert printed == "Hello, World!\n"

The test returns an assertion error if your program does not print the correct message.

How to submit?

To submit your notebooks:

  1. Select the menu item Nbgrader\toAssignment List.
  2. Expand the Lab folder and click the validate button next to the notebook(s) to check if all the visible tests pass.
  3. Click Submit to submit your notebook.

Validation is important as it runs the notebook cells in order starting from a clean state. Re-executing cells in order in a Jupyter Notebook ensures that all dependencies and state changes are correctly applied, preventing misleading results from out-of-order execution. The autograding behavior may also be modified by the environment variable NBGRADER_EXECUTION.

In addition to validating your notebook, you may also:

  1. Git-pull the notebooks: Follow any one of the link on the course homepage to a notebook, which will git-pull any updates/corrections to (all) your notebooks.
  2. Save the notebook: Unsaved changes are not submitted, even though they are in the memory.
  3. Restart the kernel: Kernel\to Restart Kernel... to have a clean state before running visible tests.
  4. run all cells: Run\to Run All Cells to double check the results of the visible tests are as expected.
Footnotes
  1. Importing a library in programming is like bringing a toolbox into your workshop, giving you access to all the tools you need to complete your tasks.

  2. If you are interested in what the first line does, it sets the video quality to medium while disabling caching and progress bars, and setting the verbosity to reporting only ERROR but not other critical information.

  3. The VSCode interface is actually code-server, so some VSCode extensions may not be listed under the extension panel. You may still try to install them using the command install-vscode-extension in a terminal.