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.
What is a Jupyter server?
The course’s programming environment is conveniently accessible via a JupyterHub server, enabling remote access without requiring special software installations, thanks to Project Jupyter. This allows you to write and run programs on mobile devices using just a web browser. Each server runs the Ubuntu operating system in a container. See the Dockerfile for the setup.
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
- selecting
Hub Control Panel
under theFile
menu, and - click
Stop My Server
and thenStart My Server
.
- selecting
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.
Access via bookmark
If you access JupyterHub from a bookmarked link, you may see a Login box as shown in Figure 1. Since the bookmarked link does not perform LTI login through Canvas, you will need to log in separately:
- Enter your EID and password in the fields
Username
andPassword
respectively. - Click the
Sign in
button.
Note that the bookmarked link also does not pull updates to notebooks from the remote Git repository.
Figure 1:Login box
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 asThe policy applies to LLM as well.
COLLABORATORS: xfwong2, mklee3, GPT-4o, dive:chat
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:
How to use JupyterLab?
Official video tutorial on Jupyter
For more advanced features:
- Checkout the
Help
menu itemsShow Keyboard Shortcuts
to try some of the shortcuts to see their effect, andJupyter Reference
to open the user guide in a new tab.
- Try also the MyST Markdown syntax to format your notes.
An alternative interface: VSCode
You may also use a highly customizable editor Visual Studio Code (VS Code) to open a notebook:
- Click
File->New Launcher->VS Code
. - Click the menu icon on the left and select
Open Folder
and selectcs1302_24a
.
In the file explorer, you can navigate to a notebook to open it. However, to properly run the notebook, you would also need to start the Kernel by choosing an appropriate Python environment such as base
, which is the default (conda) environment for our JupyterLab setup. Unlike JupyterLab, you may install additional extensions yourself to enrich the interface.[3]
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()
It’s important to follow certain guidelines when writing your answers or code in the notebooks:
- Only provide your answers in the cells that are designated for this purpose, which are usually labeled with
YOUR CODE HERE
orYOUR ANSWER HERE
. - For coding exercises, be sure to remove the line
raise NotImplementedError()
from the cell before submitting your work. - Do not clone or duplicate the answer cells, as this can cause issues with version control and grading.
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.
- You can repeatedly modify your solution and run the test cell until your solution passes the test. There is no mark penalty in failing the test before submission.
- You should try to understand the error messages from a failed test.
- To assess your solution thoroughly, we will run new tests hidden from you after you have submitted your notebook. Therefore, you should ensure your solution works in general rather than just the visible tests.
- If you open the same notebook multiple times in different browser windows, be careful in making changes in different windows as you may overwrite your own changes.
- If your notebook fails to run any code, the kernel might have died. You can restart the kernel with
Kernel
Restart
. If restarting fails, check your code cells to see if there is anything that breaks the kernel such as an infinite loop.
How to submit?¶
To submit your notebooks:
- Select the menu item
Nbgrader
Assignment List
. - Expand the Lab folder and click the
validate
button next to the notebook(s) to check if all the visible tests pass. - 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
.
What is Nbgrader?
Nbgrader is a package for grading notebook assignments. It allows students to submit their notebooks directly through the JupyterHub server. Submitted notebooks can be both auto-graded with pre-defined test cases and manually graded with custom feedback. After grading is complete, you can check your scores and access the feedback using the same interface.
In addition to validating your notebook, you may also:
- 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.
- Save the notebook: Unsaved changes are not submitted, even though they are in the memory.
- Restart the kernel:
Kernel
Restart Kernel...
to have a clean state before running visible tests. - run all cells:
Run
Run All Cells
to double check the results of the visible tests are as expected.
A common and costly mistake is forgetting to validate/submit the notebooks after completing the problem set. The grading may also fail under the following circumstances:
- The notebook files have been renamed, for example,
Setup.ipynb
being changed or copied toSetup (Copy).ipynb
. - An HTML file exists with the same name as a notebook, for example,
Setup.html
. - The file size is too large, e.g., exceeds
100MB
. - The code takes too long to run or requires an excessive amount of memory.
It is essential to ensure that your submission meets these guidelines to avoid any issues with grading.
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.
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.
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.