Skip to content

Instantly share code, notes, and snippets.

@rubdos
Created June 24, 2021 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubdos/fac49824301ee1fc0a14513e0d127956 to your computer and use it in GitHub Desktop.
Save rubdos/fac49824301ee1fc0a14513e0d127956 to your computer and use it in GitHub Desktop.
Export nbgrader as per-assignment CSV files with per-question columns
import pathlib
from nbgrader.plugins import ExportPlugin
from nbgrader.api import Gradebook, MissingEntry
class VubExporter(ExportPlugin):
def export(self, gradebook: Gradebook) -> None:
if self.to == "":
dest = "grades"
else:
dest = self.to
pathlib.Path(dest).mkdir(parents=True, exist_ok=True)
for assignment in gradebook.assignments:
keys = [
"student_id",
"submission_time",
"needs_manual_grade",
]
# assignment.submissions
if len(assignment.notebooks) == 0:
continue
cells = []
for notebook in assignment.notebooks:
for cell in notebook.grade_cells:
cells.append(notebook.name + "/" + cell.id)
keys.append("cell " + notebook.name + "/" + cell.id + " (" + str(int(cell.max_score)) + ")")
fh = open(dest + "/" + assignment.name + ".csv", "w")
fh.write(", ".join(keys) + "\n")
for student in gradebook.students:
try:
submission = gradebook.find_submission(assignment.name, student.id)
except MissingEntry:
continue
student_grades = ["0"] * len(cells)
for notebook in submission.notebooks:
for grade in notebook.grades:
key = notebook.name + "/" + grade.cell_id
key = cells.index(key)
student_grades[key] = str(grade.score)
fh.write(", ".join([student.id, submission.timestamp.isoformat(), str(submission.needs_manual_grade)]))
fh.write(", ")
fh.write(", ".join(student_grades) + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment