📒Jupyter Notebook
This guide explains how to configure automatic grading for Jupyter Notebook submissions using CodeGrade’s AutoTest v2 framework.
1. Submission Settings
To allow students to submit .ipynb
files, ensure that the Editor is enabled in the Submission Settings of your assignment.
You do not need to upload a template file at this stage.

2. Building the AutoTest
Setup Phase
In the Setup phase:
Install any required dependencies (e.g.,
matplotlib
,numpy
,pandas
) usingpip
.Upload any additional files needed during testing.

Test Phase
In the Test phase, begin by adding a Jupyter Notebook block.

Uploading a template file
Within the block, upload your template notebook. This will serve as the reference structure for testing, as well as a starting file for your students.

To generate a compatible template notebook with CodeGrade cell tagging:
Click on the uploaded notebook in the block to preview it.

In the pop-up window, download the file—this version contains the necessary cell tags for referencing individual cells in test blocks.
You can now fill in the answers in this downloaded notebook using your preferred editor. This filled in file will serve as the Test Submission.
Creating a template file from scratch
If you’d like to create a new full template file from within CodeGrade you can:
Click on "Create Empty" in the Jupyter Notebook block
Upload a test submission (it doesn’t have to be the actual test submission you will use later) and publish the snapshot
Enter Student View and create a new submission

You can build your Notebook from within the editor by adding and ordering cells. Submit this file and download it.

3. Inserting Test Code
To test specific cells in the student’s notebook, nest Run In Notebook blocks within the Jupyter Notebook block.
You can insert test code:
At the start of the notebook
At the end of the notebook
Before or after a specific cell
In most use cases, inserting code after each question cell is ideal. For example, you may want to test the function a student writes with simple assertions.
To do this:
Choose “After a cell” from the insertion options.

Click on the "Cell" field to open the notebook preview.

Select the cell after which you want the test code inserted. Write your testing code in the Run In Notebook block.

Repeat this process for each question or notebook cell you wish to test.

Example: Testing plots
You can even test plots programmatically. Below is an example of assertions for a simple plot of y = x^2:
assert list(line.get_xdata()) == x, "X data does not match expected values"
assert list(line.get_ydata()) == y, "Y data does not match expected values"
assert ax.get_xlabel() == 'x', "X-axis label incorrect"
assert ax.get_ylabel() == 'y', "Y-axis label incorrect"
assert ax.get_title() == 'y = x^2', "Plot title incorrect"
These assertions verify that the correct data was plotted and the axis labels and title match expectations.
Last updated