Skip to main content

Grade Jupyter notebooks with Python scripts

Project description

Jupygrader Logo

Hatch project Codacy Badge

PyPI - Version PyPI - Python Version


📋 Table of Contents

📝 Summary

Jupygrader is a Python package for automated grading of Jupyter notebooks. It provides a framework to:

  1. Execute and grade Jupyter notebooks containing student work and test cases
  2. Generate comprehensive reports in multiple formats (JSON, HTML, TXT)
  3. Extract student code from notebooks into separate Python files
  4. Verify notebook integrity by computing hashes of test cases and submissions

✨ Key Features

  • Executes notebooks in a controlled, temporary environment
  • Preserves the original notebook while creating graded versions
  • Adds grader scripts to notebooks to evaluate test cases
  • Generates detailed grading results including:
    • Individual test case scores
    • Overall scores and summaries
    • Success/failure status of each test
  • Produces multiple output formats for instructors to review:
    • Graded notebook (.ipynb)
    • HTML report
    • JSON result data
    • Plaintext summary
    • Extracted Python code
  • Includes metadata like Python version, platform, and file hashes for verification

Jupygrader is designed for educational settings where instructors need to grade student work in Jupyter notebooks, providing automated feedback while maintaining records of submissions and grading results.

📦 Installation

pip install jupygrader

🔄 Update Jupygrader

pip install --upgrade jupygrader

🚀 Usage

Basic usage

import jupygrader

notebook_file_path = 'path/to/notebook.ipynb'
jupygrader.grade_notebooks(notebook_file_path)

Supplying a pathlib.Path() object is supported.

import jupygrader
from pathlib import Path

notebook_path = Path('path/to/notebook.ipynb')
jupygrader.grade_notebooks(notebook_path)

If the output_dir_path is not specified, the output files will be stored to the same directory as the notebook file.

During grading, Jupygrader preprocesses code cells and comments out lines that start with IPython shell/magic prefixes (! and %). This prevents notebook-only commands from causing syntax errors in the Python-based grading pipeline.

Specify the output directory

import jupygrader

jupygrader.grade_notebooks([{
    "notebook_path": 'path/to/notebook.ipynb',
    "output_path": 'path/to/output'
}])

📒 Create an autogradable notebook

The instructor authors only one "solution" notebook, which contains both the solution code and test cases for all graded parts.

Jupygrader provides a simple drag-and-drop interface to generate a student-facing notebook that removes the solution code and obfuscates test cases if required.

Code cell for learners

Any code between # YOUR CODE BEGINS and # YOUR CODE ENDS are stripped in the student version.

import pandas as pd

# YOUR CODE BEGINS
sample_series = pd.Series([-20, -10, 10, 20])
# YOUR CODE ENDS

print(sample_series)

nbgrader syntax (### BEGIN SOLUTION, ### END SOLUTION) is also supported.

import pandas as pd

### BEGIN SOLUTION
sample_series = pd.Series([-20, -10, 10, 20])
### END SOLUTION

print(sample_series)

In the student-facing notebook, the code cell will look like:

import pandas as pd

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(sample_series)

Grader-only cells

To keep setup notes or helper code in the instructor notebook only, start any cell with one of the following markers. The full cell will be removed in the generated student version:

  • # GRADER_ONLY (case-insensitive)
  • # grader_only (case-insensitive)
  • ! grader_only (case-insensitive)
  • _grader_only = True (case-sensitive; whitespace is ignored)

Graded test cases

A graded test case requires a test case name and an assigned point value.

  • The _test_case variable should store the name of the test case.
  • The _points variable should store the number of points, either as an integer or a float.
_test_case = 'create-a-pandas-series'
_points = 2

pd.testing.assert_series_equal(sample_series, pd.Series([-20, -10, 10, 20]))

Obfuscate test cases

If you want to prevent learners from seeing the test case code, you can optionally set _obfuscate = True to base64-encode the test cases.

Note that this provides only basic obfuscation, and students can easily decode the string to reveal the original code.

We may introduce an encryption method in the future.

Instructor notebook

_test_case = 'create-a-pandas-series'
_points = 2
_obfuscate = True

pd.testing.assert_series_equal(sample_series, pd.Series([-20, -10, 10, 20]))

Student notebook

# DO NOT CHANGE THE CODE IN THIS CELL
_test_case = 'create-a-pandas-series'
_points = 2
_obfuscate = True

import base64 as _b64
_64 = _b64.b64decode('cGQudGVzdGluZy5hc3NlcnRfc2VyaWVzX2VxdWFsKHNhbXBsZV9zZXJpZXMsIHBkLlNlcmllcyhbLT\
IwLCAtMTAsIDEwLCAyMF0pKQ==')
eval(compile(_64, '<string>', 'exec'))

Add hidden test cases

Hidden test cases only run while grading.

Original test case

_test_case = 'create-a-pandas-series'
_points = 2

### BEGIN HIDDEN TESTS
pd.testing.assert_series_equal(sample_series, pd.Series([-20, -10, 10, 20]))
### END HIDDEN TESTS

Converted (before obfuscation)

_test_case = 'create-a-pandas-series'
_points = 2

if 'is_jupygrader_env' in globals():
    pd.testing.assert_series_equal(sample_series, pd.Series([-20, -10, 10, 20]))

🔧 Utility functions

Replace test cases

If a test case needs to be updated before grading, use the jupygrader.replace_test_case() function.

This is useful when learners have already submitted their Jupyter notebooks, but the original notebook contains an incorrect test case.

nb = nbformat.read(notebook_path, as_version=4)

jupygrader.replace_test_case(nb, 'q1', '_test_case = "q1"\n_points = 6\n\nassert my_var == 3')

Below is a sample code snippet demonstrating how to replace multiple test cases using a dictionary.

nb = nbformat.read(notebook_path, as_version=4)

new_test_cases = {
    'test_case_01': '_test_case = "test_case_01"\n_points = 6\n\npass',
    'test_case_02': '_test_case = "test_case_02"\n_points = 3\n\npass'
}

for tc_name, new_tc_code in new_test_cases.items():
    jupygrader.replace_test_case(nb, tc_name, new_tc_code)

📄 License

jupygrader is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jupygrader-0.4.0.tar.gz (14.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jupygrader-0.4.0-py3-none-any.whl (33.0 kB view details)

Uploaded Python 3

File details

Details for the file jupygrader-0.4.0.tar.gz.

File metadata

  • Download URL: jupygrader-0.4.0.tar.gz
  • Upload date:
  • Size: 14.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for jupygrader-0.4.0.tar.gz
Algorithm Hash digest
SHA256 b46030645abfc6138a72a0ce65ab472202ad7d21e7a1ad945030a8226c522dd4
MD5 03783adb1157444d2d253bec19243b93
BLAKE2b-256 78190e2ed9f5464a006f993c7be03729c5acdba9484cebb86e620d65c6fd5cf0

See more details on using hashes here.

File details

Details for the file jupygrader-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: jupygrader-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 33.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for jupygrader-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c32bec9e277bff3283303bc23397b0391f23ee1fd80ced68ff167b355fd07158
MD5 13062acc1c93c5038f654831bd75b2f0
BLAKE2b-256 a0dcb226b4a78968fab1c3381fb41d718fb6ad4b3f2e90b6b85ed31a2ad8efee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page