# Jupyter Notebook

## 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.

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2FxhLKqmlMHozazCs0xl1Y%2FScreenshot%202025-06-26%20at%2011.48.55.png?alt=media&#x26;token=8481f77c-c8f2-4aa3-9199-c4de41cea00c" alt=""><figcaption></figcaption></figure>

## 2. Building the AutoTest

### Setup Phase

In the Setup phase:

* Install any required dependencies (e.g., `matplotlib`, `numpy`, `pandas`) using `pip`.
* Upload any additional files needed during testing.

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2FU5APonWEdOkE9vQvknrG%2FScreenshot%202025-06-26%20at%2014.45.59.png?alt=media&#x26;token=edee1c21-9a1d-4750-bce4-9a40e2fba17a" alt=""><figcaption></figcaption></figure>

### Test Phase

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

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2FYS8uj45tTLBkNKgUKRY5%2FScreenshot%202025-06-26%20at%2011.49.23.png?alt=media&#x26;token=a997a3b6-0bf2-4bd3-b394-2ac4dde005f2" alt=""><figcaption></figcaption></figure>

#### 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.

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2F4gmwWM2Nm1PCHhVtYQna%2FScreenshot%202025-06-26%20at%2011.51.51.png?alt=media&#x26;token=9c83cf99-aae6-456a-adbc-8865a33c3d91" alt=""><figcaption></figcaption></figure>

To generate a compatible template notebook with CodeGrade cell tagging:

1. Click on the uploaded notebook in the block to preview it.

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2Fn3aSzqM2ZubiLPFKbgMs%2FScreenshot%202025-06-26%20at%2011.52.00.png?alt=media&#x26;token=5c1d6e9f-98a0-4727-b4d6-cece6e4469ba" alt=""><figcaption></figcaption></figure>

2. 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:

1. Click on "**Create Empty**" in the **Jupyter Notebook** block
2. Upload a test submission (it doesn’t have to be the actual test submission you will use later) and publish the snapshot
3. Enter Student View and create a new submission

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2F6L1JaDDBAJkast3DcjxE%2FScreenshot%202025-06-30%20at%2016.06.39.png?alt=media&#x26;token=5f9901ea-780f-49fd-be14-3d3399cf5ff0" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2Fs14BiWbUsqas3sUjFj2U%2FScreenshot%202025-06-30%20at%2016.06.50.png?alt=media&#x26;token=5148152b-b469-4f08-8efd-f75fce182457" alt=""><figcaption></figcaption></figure>

## 3. Inserting Test Code&#x20;

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:

1. Choose “**After a cell**” from the insertion options.

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2FRnGWbslYGWTv8cgi9fsC%2FScreenshot%202025-06-26%20at%2015.16.56.png?alt=media&#x26;token=b34e3b9d-b79c-42f9-82f0-945fd4924319" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2FtijIU9ptpnZbXP2UPzrH%2FScreenshot%202025-06-26%20at%2012.59.43.png?alt=media&#x26;token=69523e4c-b4e9-4097-a0f5-f615b87ceb1d" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2FUgwu8SlUtyNUVl5jHAGD%2FScreenshot%202025-06-26%20at%2013.01.54.png?alt=media&#x26;token=e48a757a-9ecb-422d-a520-1b6bfa1974a3" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://2172486256-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MKAQsDlg_P20iQy3JDs%2Fuploads%2FBaMdfZ4ooiFJqkrPQUOI%2FScreenshot%202025-06-26%20at%2014.40.24.png?alt=media&#x26;token=348bd18f-2e33-436e-84a7-076e4a4e1529" alt=""><figcaption></figcaption></figure>

## Example: Testing plots

You can even test plots programmatically. Below is an example of assertions for a simple plot of `y = x^2:`

```python
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.
