📒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. Make those files available in the student editor by adding this line to your Setup Script:
Without it, students get
FileNotFoundErrorwhen they run cells that read your files.
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
Click the template file name to open the template editor. Here you can:
add cells with + Cell,
switch a cell between Code and Markdown,
Download File to get the tagged version of the notebook.


3. Internet Access in Jupyter Notebooks
By default, internet access is disabled whenever notebook code runs, both while your AutoTest tests run and while students run their own cells in the notebook editor. Internet is only enabled during the AutoTest Setup phase; after that, all notebook code executes without connectivity.
This means code that downloads a dataset, calls an external API, or runs pip install from within the notebook will fail during testing and when a student runs it in the editor — unless you explicitly allow it.
To grant internet access, wrap the Jupyter Notebook block inside an Allow Internet block:
In the Test phase, add an Allow Internet block.
Nest your Jupyter Notebook block (including its Run In Notebook blocks) inside it.

All blocks nested inside the Allow Internet block will run with internet access enabled. This applies to both AutoTest runs and student cell execution in the editor.
4. 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.

Make your files available to the tests
AutoTest runs the notebook in a separate folder, so files from your Setup are not there automatically, even if you added the cp line in step 2. Add a Run In Notebook step with Cell Location set to at start of notebook, containing:
Example: Testing plots
You can even test plots programmatically. Below is an example of assertions for a simple plot of y = x^2:
These assertions verify that the correct data was plotted and the axis labels and title match expectations.
5. Publishing the snapshot
Click Build Snapshot (bottom right).
The first time, you need a test submission. Choose Automatically generate a test submission, which uses your template.
The generated submission is the empty template, so most of your tests will fail on it. That’s expected. To check that your tests pass, you will need to fill the answers in the Test Submission.
Check the results, then click Publish to students.
Test it as a student. Use Student View to create a submission from the template, run the cells in the editor, and hand in. This is the most reliable way to check the whole setup: packages, files, running cells and tests.

6. Interactive Input in Jupyter Notebooks
The notebook editor supports Python's input() function. When a student runs a cell that calls input(), the prompt appears below the cell with a text field. The student types a value and clicks Answer, and the cell continues running with that value.
If a cell asks for multiple inputs, the prompts appear one at a time, in order. Each answered prompt and its value stays visible in the cell output.

A cell that is waiting for input stays in the running state until the student answers. If a cell appears stuck, check whether it is still waiting for an answer.
Last updated