This guide walks an instructor through the workflow for generating an assignment and preparing it for release to students.

New in version 0.5.0: Much of the core functionality of nbgrader can now be accessed through the “formgrader” extension.

Installation
Instructions on how to install the formgrader extension.

The formgrader extension provides the core access to nbgrader’s instructor tools. After the extension has been installed, you can access it through the tab in the notebook list:

## Creating a new assignment¶

Managing the database
Instructions on how to manage assignments in the database from the command line
Command line options for nbgrader db assignment add

To create a new assignment, open the formgrader extension and click the “Add new assignment...” button at the bottom of the page. This will ask you to provide some information such as the name of the assignment and its due date. Then, you can add files to the assignment and edit them by clicking the name of the assignment:

### From the command line¶

If you are not using the formgrader extension, you can add a new assignment simply by creating a folder in your course directory with the name of the assignment. You can specify the assignment metadata (such as the duedate) using the nbgrader db assignment command (see Managing the database).

To simplify this example, two notebooks of the assignment have already been stored in the source/ps1 folder:

## Developing assignments with the assignment toolbar¶

Note: As you are developing your assignments, you should save them into the source/{assignment_id}/ folder of the nbgrader hierarchy, where assignment_id is the name of the assignment you are creating (e.g. “ps1”).

The philosophy and the approach
More details on how the nbgrader hierarchy is structured.
Customizing how the student version of an assignment looks
Instructions for customizing how the student version of the assignment looks.

Before you can begin developing assignments, you will need to actually install the nbgrader toolbar. If you do not have it installed, please first follow the installation instructions.

Once the toolbar has been installed, you should see it in the drop down “View -> Cell Toolbar” menu:

Selecting the “Create Assignment” toolbar will create a separate toolbar for each cell which by default will be a dropdown menu with the “-” item selected. For markdown cells, there are two additional options to choose from, either “Manually graded answer” or “Read-only”:

The following sections go into detail about the different cell types, and show cells that are taken from a complete example of an assignment generated with the nbgrader toolbar extension:

If you select the “Manually graded answer” option (available for both markdown and code cells), the nbgrader extension will mark that cell as a cell that contains an answer that must be manually graded by a human grader. Here is an example of a manually graded answer cell:

The most common use case for this type of cell is for written free-response answers (for example, which interpret the results of code that may have been written and/or executed above).

When you specify a manually graded answer, you must additionally tell nbgrader how many points the answer is worth, and an id for the cell. Additionally, when creating the release version of the assignment (see Assign and release an assignment), the bodies of answer cells will be replaced with a code or text stub indicating to the students that they should put their answer or solution there. Please see Customizing how the student version of an assignment looks for details on how to customize this behavior.

Note: the blue border only shows up when the nbgrader extension toolbar is active; it will not be visible to students.

As shown in the image above, solutions can be specified inline, through the use of a special syntax such as ### BEGIN SOLUTION and ### END SOLUTION. When creating the release version (see Assign and release an assignment), the region between the special syntax lines will be replaced with a code stub. If this special syntax is not used, then the entire contents of the cell will be replaced with the code stub. Please see Customizing how the student version of an assignment looks for details on how to customize this behavior.

Note: the blue border only shows up when the nbgrader extension toolbar is active; it will not be visible to students.

If you select the “Autograder tests” option (available only for code cells), the nbgrader extension will mark that cell as a cell that contains tests to be run during autograding. Here is an example of two test cells:

Test cells should contain assert statements (or similar). When run through nbgrader autograde (see Autograde assignments), the cell will pass if no errors are raised, and fail otherwise. You must specify the number of points that each test cell is worth; then, if the tests pass during autograding, students will receive the specified number of points, and otherwise will receive zero points.

The lock icon on the left side of the cell toolbar indicates that the tests are “read-only”. See the next section for further details on what this means.

Note: the blue border only shows up when the nbgrader extension toolbar is active; it will not be visible to students.

### “Autograder tests” cells with hidden tests¶

New in version 0.5.0.

Tests in “Autograder tests” cells can be hidden through the use of a special syntax such as ### BEGIN HIDDEN TESTS and ### END HIDDEN TESTS, for example:

When creating the release version (see Assign and release an assignment), the region between the special syntax lines will be removed. If this special syntax is not used, then the contents of the cell will remain as is. Please see Customizing how the student version of an assignment looks for details on how to customize this behavior.

Note

Keep in mind that wrapping all tests (for an “Autograder tests” cell) in this special syntax will remove all these tests in the release version and the students will only see a blank cell. It is recommended to have at least one or more visible tests, or a comment in the cell for the students to see.

These hidden tests are placed back into the “Autograder tests” cells when running nbgrader autograde (see Autograde assignments).

If you select the “Read-only” option (available for both code and markdown cells), the nbgrader extension will mark that cell as one that cannot be modified. This is indicated by a lock icon on the left side of the cell toolbar:

However, this doesn’t actually mean that it is truly read-only when opened in the notebook. Instead, what it means is that during the nbgrader assign step (see Assign and release an assignment), the source of these cells will be recorded into the database. Then, during the nbgrader autograde step (see Autograde assignments), nbgrader will check whether the source of the student’s version of the cell has changed. If it has, it will replace the cell’s source with the version in the database, thus effectively overwriting any changes the student made.

New in version 0.4.0: Read-only cells (and test cells) are now truly read-only! However, at the moment this functionality will only work on the master version of the notebook (5.0.0.dev).

This functionality is particularly important for test cells, which are always marked as read-only. Because the mechanism for autograding is that students receive full credit if the tests pass, an easy way to get around this would be to simply delete or comment out the tests. This read-only functionality will reverse any such changes made by the student.

## Validating the instructor version¶

Command line options for nbgrader validate

### From the validate extension¶

Ideally, the solutions in the instructor version should be correct and pass all the test cases to ensure that you are giving your students tests that they can actually pass. To verify this is the case, you can use the validate extension:

If your assignment passes all the tests, you’ll get a success pop-up:

If it doesn’t pass all the tests, you’ll get a message telling you which cells failed:

### From the command line¶

You can also validate assignments on the command line using the nbgrader validate command:

%%bash


Success! Your notebook passes all the tests.
Success! Your notebook passes all the tests.

[ValidateApp | WARNING] No nbgrader_config.py file found (rerun with --debug to see where nbgrader is looking)
[ValidateApp | INFO] Executing notebook with kernel: python
[ValidateApp | INFO] Executing notebook with kernel: python


## Assign and release an assignment¶

Command line options for nbgrader assign
The philosophy and the approach
Details about how the directory hierarchy is structured
Configuration options
Details on nbgrader_config.py

After an assignment has been created with the assignment toolbar, you will want to generate the version that students will receive. You can do this from the formgrader by clicking the “generate” button:

This should succeed with a pop-up window containing log output:

### From the command line¶

As described in The philosophy and the approach, you need to organize your files in a particular way. For releasing assignments, you should have the master copy of your files saved (by default) in the following source directory structure:

{course_directory}/source/{assignment_id}/{notebook_id}.ipynb


Note: The student_id is not included here because the source and release versions of the assignment are the same for all students.

After running nbgrader assign, the release version of the notebooks will be:

{course_directory}/release/{assignment_id}/{notebook_id}.ipynb


As a reminder, the instructor is responsible for distributing this release version to their students using their institution’s existing student communication and document distribution infrastructure.

When running nbgrader assign, the assignment name (which is “ps1”) is passed. We also specify a header notebook (source/header.ipynb) to prepend at the beginning of each notebook in the assignment. By default, this command should be run from the root of the course directory:

%%bash


[AssignApp | WARNING] No nbgrader_config.py file found (rerun with --debug to see where nbgrader is looking)
[AssignApp | INFO] Updating/creating assignment 'ps1': {}
[AssignApp | INFO] Setting destination file permissions to 644


## Preview the student version¶

After generating the student version of assignment, you should preview it to make sure that it looks correct. You can do this from the formgrader extension by clicking the “preview” button:

Under the hood, there will be a new folder called release with the same structure as source. The release folder contains the actual release version of the assignment files:

If you are working on the command line, you may want to formally verify the student version as well. Ideally, all the tests should fail in the student version if the student hasn’t implemented anything. To verify that this is in fact the case, we can use the nbgrader validate --invert command:

%%bash


Success! The notebook does not pass any tests.
Success! The notebook does not pass any tests.

[ValidateApp | WARNING] No nbgrader_config.py file found (rerun with --debug to see where nbgrader is looking)
[ValidateApp | INFO] Executing notebook with kernel: python
[ValidateApp | INFO] Executing notebook with kernel: python


If the notebook fails all the test cases, you should see the message “Success! The notebook does not pass any tests.”

## Releasing files to students and collecting submissions¶

Managing assignment files
Guide to releasing and collecting submissions.
Command line options for nbgrader release
Command line options for nbgrader collect
The philosophy and the approach
Details about how the directory hierarchy is structured
Configuration options
Details on nbgrader_config.py

Note: the Managing Assignment Files Guide goes into greater depth on how to release and collect assignments, and the various options that are available to do you for doing so.

At this point you will be able to take the files in the release folder and distribute them to students. If you are using nbgrader with JupyterHub, you can do this with either with the formgrader extension or with the nbgrader release command (see Managing assignment files). Otherwise, you will need to do this manually.

Similarly, to collect submissions, you can do this either with the formgrader extension or with the nbgrader collect command. Otherwise, you will need to manually place submitted files into the submitted directory. As described in The philosophy and the approach, you need to organize your files in a particular way. For submitted assignments, you should have the submitted versions of students’ assignments organized as follows:

submitted/{student_id}/{assignment_id}/{notebook_id}.ipynb


Please note: Students must use version 3 or greater of the IPython/Jupyter notebook for nbgrader to work properly. If they are not using version 3 or greater, it is possible for them to delete cells that contain important metadata for nbgrader. With version 3 or greater, there is a feature in the notebook that prevents cells from being deleted. See this issue for more details.

To ensure that students have a recent enough version of the notebook, you can include a cell such as the following in each notebook of the assignment:

import IPython
assert IPython.version_info[0] >= 3, "Your version of IPython is too old, please update it."


Command line options for nbgrader autograde
The philosophy and the approach
Details about how the directory hierarchy is structured
Configuration options
Details on nbgrader_config.py

In the following example, we have an assignment with two notebooks. There are two submissions of the assignment:

Submission 1:

Submission 2:

You can autograde individual submissions from the formgrader directly. To do so, click on the the number of submissions in the “Manage Assignments” view:

This will take you to a new page where you can see all the submissions. For a particular submission, click the “autograde” button to autograde it:

After autograding completes, you will see a pop-up window with log output:

And back on the submissions screen, you will see that the status of the submission has changed to “needs manual grading” and there is now a reported score as well:

### From the command line¶

We can run the autograder for all students at once from the command line:

%%bash


[AutogradeApp | WARNING] No nbgrader_config.py file found (rerun with --debug to see where nbgrader is looking)
[AutogradeApp | INFO] Creating/updating student with ID 'bitdiddle': {}
[AutogradeApp | INFO] SubmittedAssignment<ps1 for bitdiddle> submitted at 2015-02-02 22:58:23.948203
[AutogradeApp | INFO] Overwriting files with master versions from the source directory
[AutogradeApp | WARNING] Attribute 'checksum' for cell correct_squares has changed! (should be: 8f41dd0f9c8fd2da8e8708d73e506b3a, got: 845d4666cabb30b6c75fc534f7375bf5)
[AutogradeApp | WARNING] Attribute 'checksum' for cell squares_invalid_input has changed! (should be: 23c2b667d3b60eff3be46eb3290a6b4a, got: 123394e73f33a622ec057e2eae51a54a)
[AutogradeApp | INFO] Executing notebook with kernel: python
[AutogradeApp | INFO] Executing notebook with kernel: python
[AutogradeApp | INFO] Setting destination file permissions to 444
[AutogradeApp | INFO] Creating/updating student with ID 'hacker': {}
[AutogradeApp | INFO] SubmittedAssignment<ps1 for hacker> submitted at 2015-02-01 17:28:58.749302
[AutogradeApp | INFO] Overwriting files with master versions from the source directory
[AutogradeApp | INFO] Executing notebook with kernel: python
[AutogradeApp | INFO] Executing notebook with kernel: python
[AutogradeApp | INFO] Setting destination file permissions to 444


When grading the submission for Bitdiddle, you’ll see some warnings that look like “Checksum for grade cell correct_squares has changed!”. What’s happening here is that nbgrader has recorded what the original contents of the grade cell correct_squares (when nbgrader assign was run), and is checking the submitted version against this original version. It has found that the submitted version changed (perhaps this student tried to cheat by commenting out the failing tests), and has therefore overwritten the submitted version of the tests with the original version of the tests.

You may also notice that there is a note saying “ps1 for Bitdiddle is 21503.948203 seconds late”. What is happening here is that nbgrader is detecting a file in Bitdiddle’s submission called timestamp.txt, reading in that timestamp, and saving it into the database. From there, it can compare the timestamp to the duedate of the problem set, and compute whether the submission is at all late.

Once the autograding is complete, there will be new directories for the autograded versions of the submissions:

autograded/{student_id}/{assignment_id}/{notebook_id}.ipynb


The philosophy and the approach
More details on how the nbgrader hierarchy is structured.
Configuration options
Details on nbgrader_config.py

After assignments have been autograded, they will saved into an autograded directory (see The philosophy and the approach for details):

After running nbgrader autograde, the autograded version of the notebooks will be:

autograded/{student_id}/{assignment_id}/{notebook_id}.ipynb


We can manually grade assignments through the formgrader as well, by clicking on the “Manual Grading” navigation button. This will provide you with an interface for hand grading assignments that it finds in the directory listed above. Note that this applies to all assignments as well – as long as the autograder has been run on the assignment, it will be available for manual grading via the formgrader.

## Feedback on assignments¶

Command line options for nbgrader feedback
The philosophy and the approach
Details about how the directory hierarchy is structured
Configuration options
Details on nbgrader_config.py

As mentioned before, after assignments have been autograded and/or manually graded, they will located in the autograded directory (see The philosophy and the approach for details):

autograded/{student_id}/{assignment_id}/{notebook_id}.ipynb


We can generate feedback based on these notebooks by running the nbgrader feedback command, which will produce HTML versions of these notebooks at the following location:

feedback/{student_id}/{assignment_id}/{notebook_id}.html


Unfortunately, nbgrader feedback is not yet available through the formgrader interface.

### Workflow example: Instructor returning feedback to students¶

In the following example, we have an assignment with two notebooks. There are two submissions of the assignment that have been graded:

Generating feedback is fairly straightforward (and as with the other nbgrader commands for instructors, this must be run from the root of the course directory):

%%bash


[FeedbackApp | WARNING] No nbgrader_config.py file found (rerun with --debug to see where nbgrader is looking)
[FeedbackApp | INFO] Setting destination file permissions to 644
[FeedbackApp | INFO] Setting destination file permissions to 644


Once the feedback has been generated, there will be new directories and HTML files corresponding to each notebook in each submission:

Feedback for submission 1:

Feedback for submission 2:

## Getting grades from the database¶

New in version 0.4.0.

Command line options for nbgrader export
Export plugin
Details on how to write your own custom exporter.

In addition to creating feedback for the students, you may need to upload grades to whatever learning management system your school uses (e.g. Canvas, Blackboard, etc.). nbgrader provides a way to export grades to CSV out of the box, with the nbgrader export command:

%%bash


[ExportApp | WARNING] No nbgrader_config.py file found (rerun with --debug to see where nbgrader is looking)
[ExportApp | INFO] Using exporter: CsvExportPlugin


After running nbgrader export, you will see the grades in a CSV file called grades.csv:

%%bash


assignment,duedate,timestamp,student_id,last_name,first_name,email,raw_score,late_submission_penalty,score,max_score