Repository: jupyter/nbgrader

Issue:
Allow the exporting of grades per assignment id
The main issue I have with the exporting of grades (csv export) is that it is extremely sluggish exporting the entire gradebook (5min+), especially in cases where there may only be one or two grade changes for a given assignment.

My suggestion is to allow the exporting of grades to individual files per assignment id, if an assignment id is specified, otherwise export the entire gradebook.

A custom plugin can be created to achieve this, but I think it may be better to support this.
@jhamrick what do you think?

This is also something that is specific to the use case of a large number of students.

Discussion:

Patch:
diff --git a/nbgrader/apps/exportapp.py b/nbgrader/apps/exportapp.py
index 0c343861..a4417107 100644
--- a/nbgrader/apps/exportapp.py
+++ b/nbgrader/apps/exportapp.py
@@ -9,7 +9,10 @@ aliases = {
     'log-level' : 'Application.log_level',
     'db': 'CourseDirectory.db_url',
     'to' : 'ExportPlugin.to',
-    'exporter': 'ExportApp.plugin_class'
+    'exporter': 'ExportApp.plugin_class',
+    'assignment' : 'ExportPlugin.assignment',
+    'student': 'ExportPlugin.student',
+
 }
 flags = {}
 
@@ -32,6 +35,16 @@ class ExportApp(NbGrader):
 
             nbgrader export --to mygrades.csv
 
+        You can export the grades for a single (or limited set) of students 
+        or assignments with the --assignment and/or --student flag:
+
+            nbgrader export --assignment [assignmentID] 
+                            --student [studentID1,studentID2]
+
+        Where the studentIDs and assignmentIDs are a list of IDs and 
+        assignments. The assignments or studentIDs need to quoted if they 
+        contain not only numbers. The square brackets are obligatory.
+
         To change the export type, you will need a class that inherits from
         nbgrader.plugins.ExportPlugin. If your exporter is named
         `MyCustomExporter` and is saved in the file `myexporter.py`, then:
diff --git a/nbgrader/plugins/export.py b/nbgrader/plugins/export.py
index 93ff6e01..26896cd5 100644
--- a/nbgrader/plugins/export.py
+++ b/nbgrader/plugins/export.py
@@ -1,4 +1,4 @@
-from traitlets import Unicode
+from traitlets import Unicode, List
 
 from .base import BasePlugin
 from ..api import MissingEntry
@@ -9,6 +9,12 @@ class ExportPlugin(BasePlugin):
 
     to = Unicode("", help="destination to export to").tag(config=True)
 
+    student = List([],
+        help="list of students to export").tag(config=True)
+
+    assignment = List([],
+        help="list of assignments to export").tag(config=True)
+
     def export(self, gradebook):
         """Export grades to another format.
 
@@ -35,7 +41,24 @@ class CsvExportPlugin(ExportPlugin):
         else:
             dest = self.to
 
+        if len(self.student) == 0:
+            allstudents = []
+        else:
+            # make sure studentID(s) are a list of strings
+            allstudents = [str(item) for item in self.student]
+
+        if len(self.assignment) == 0:
+            allassignments = []
+        else:
+            # make sure assignment(s) are a list of strings
+            allassignments = [str(item) for item in self.assignment]
+
         self.log.info("Exporting grades to %s", dest)
+        if allassignments:
+            self.log.info("Exporting only assignments: %s", allassignments)
+
+        if allstudents:
+            self.log.info("Exporting only students: %s", allstudents)
 
         fh = open(dest, "w")
         keys = [
@@ -57,11 +80,19 @@ class CsvExportPlugin(ExportPlugin):
         # Loop over each assignment in the database
         for assignment in gradebook.assignments:
 
+            # only continue if assignment is required
+            if allassignments and assignment.name not in allassignments:
+                continue
+
             # Loop over each student in the database
             for student in gradebook.students:
 
-                # Create a dictionary that will store information about this
-                # student's submitted assignment
+                # only continue if student is required
+                if allstudents and student.id not in allstudents:
+                    continue
+
+                # Create a dictionary that will store information 
+                # about this student's submitted assignment
                 score = {}
                 score['assignment'] = assignment.name
                 score['duedate'] = assignment.duedate
@@ -71,10 +102,10 @@ class CsvExportPlugin(ExportPlugin):
                 score['email'] = student.email
                 score['max_score'] = assignment.max_score
 
-                # Try to find the submission in the database. If it doesn't
-                # exist, the `MissingEntry` exception will be raised, which
-                # means the student didn't submit anything, so we assign them a
-                # score of zero.
+                # Try to find the submission in the database. If it
+                # doesn't exist, the `MissingEntry` exception will be
+                # raised, which means the student didn't submit 
+                # anything, so we assign them a score of zero.
                 try:
                     submission = gradebook.find_submission(
                         assignment.name, student.id)


Test patch:
diff --git a/nbgrader/tests/apps/test_nbgrader_export.py b/nbgrader/tests/apps/test_nbgrader_export.py
index 9f56ff92..5d107248 100644
--- a/nbgrader/tests/apps/test_nbgrader_export.py
+++ b/nbgrader/tests/apps/test_nbgrader_export.py
@@ -44,3 +44,21 @@ class TestNbGraderExport(BaseTestApp):
 
         run_nbgrader(["export", "--db", db, "--exporter=nbgrader.tests.apps.files.myexporter.MyExporter", "--to", "foo.txt"])
         assert os.path.isfile("foo.txt")
+
+        run_nbgrader(["export", "--db", db, "--student", "['bar']"])
+        assert os.path.isfile("grades.csv")
+        with open("grades.csv", "r") as fh:
+            contents = fh.readlines()
+        assert len(contents) == 3
+
+        run_nbgrader(["export", "--db", db, "--assignment", "['ps1']"])
+        assert os.path.isfile("grades.csv")
+        with open("grades.csv", "r") as fh:
+            contents = fh.readlines()
+        assert len(contents) == 3
+
+        run_nbgrader(["export", "--db", db, "--assignment", "['ps1']", "--student", "['foo']"])
+        assert os.path.isfile("grades.csv")
+        with open("grades.csv", "r") as fh:
+            contents = fh.readlines()
+        assert len(contents) == 2