def test_grade(self, gradebook): """Can files be graded?""" self._copy_file("files/submitted-unchanged.ipynb", "source/ps1/p1.ipynb") run_command('nbgrader assign ps1 --db="{}" '.format(gradebook)) self._copy_file("files/submitted-unchanged.ipynb", "submitted/foo/ps1/p1.ipynb") self._copy_file("files/submitted-changed.ipynb", "submitted/bar/ps1/p1.ipynb") run_command('nbgrader autograde ps1 --db="{}"'.format(gradebook)) assert os.path.isfile("autograded/foo/ps1/p1.ipynb") assert not os.path.isfile("autograded/foo/ps1/timestamp.txt") assert os.path.isfile("autograded/bar/ps1/p1.ipynb") assert not os.path.isfile("autograded/bar/ps1/timestamp.txt") gb = Gradebook(gradebook) notebook = gb.find_submission_notebook("p1", "ps1", "foo") assert notebook.score == 1 assert notebook.max_score == 4 assert notebook.needs_manual_grade == False comment1 = gb.find_comment(0, "p1", "ps1", "foo") comment2 = gb.find_comment(1, "p1", "ps1", "foo") assert comment1.comment == "No response." assert comment2.comment == "No response." notebook = gb.find_submission_notebook("p1", "ps1", "bar") assert notebook.score == 2 assert notebook.max_score == 4 assert notebook.needs_manual_grade == True comment1 = gb.find_comment(0, "p1", "ps1", "bar") comment2 = gb.find_comment(1, "p1", "ps1", "bar") assert comment1.comment == None assert comment2.comment == None
class GetGrades(NbGraderPreprocessor): """Preprocessor for saving grades from the database to the notebook""" def preprocess(self, nb, resources): # pull information from the resources self.notebook_id = resources['nbgrader']['notebook'] self.assignment_id = resources['nbgrader']['assignment'] self.student_id = resources['nbgrader']['student'] self.db_url = resources['nbgrader']['db_url'] # connect to the database self.gradebook = Gradebook(self.db_url) # process the cells nb, resources = super(GetGrades, self).preprocess(nb, resources) submission = self.gradebook.find_submission_notebook( self.notebook_id, self.assignment_id, self.student_id) resources['nbgrader']['score'] = submission.score resources['nbgrader']['max_score'] = submission.max_score return nb, resources def _get_comment(self, cell, resources): """Graders can optionally add comments to the student's solutions, so add the comment information into the database if it doesn't already exist. It should NOT overwrite existing comments that might have been added by a grader already. """ # retrieve or create the comment object from the database comment = self.gradebook.find_comment( cell.metadata['nbgrader']['grade_id'], self.notebook_id, self.assignment_id, self.student_id) # save it in the notebook cell.metadata.nbgrader['comment'] = comment.comment def _get_score(self, cell, resources): grade = self.gradebook.find_grade( cell.metadata['nbgrader']['grade_id'], self.notebook_id, self.assignment_id, self.student_id) cell.metadata.nbgrader['score'] = grade.score cell.metadata.nbgrader['points'] = grade.max_score def preprocess_cell(self, cell, resources, cell_index): # if it's a solution cell, then add a comment if utils.is_solution(cell): self._get_comment(cell, resources) # if it's a grade cell, the add a grade if utils.is_grade(cell): self._get_score(cell, resources) return cell, resources
def test_grade(self): """Can files be graded?""" with self._temp_cwd(["files/submitted-unchanged.ipynb", "files/submitted-changed.ipynb"]): dbpath = self._setup_db() os.makedirs('source/ps1') shutil.copy('submitted-unchanged.ipynb', 'source/ps1/p1.ipynb') self._run_command('nbgrader assign ps1 --db="{}" '.format(dbpath)) os.makedirs('submitted/foo/ps1') shutil.move('submitted-unchanged.ipynb', 'submitted/foo/ps1/p1.ipynb') os.makedirs('submitted/bar/ps1') shutil.move('submitted-changed.ipynb', 'submitted/bar/ps1/p1.ipynb') self._run_command('nbgrader autograde ps1 --db="{}"'.format(dbpath)) assert os.path.isfile("autograded/foo/ps1/p1.ipynb") assert not os.path.isfile("autograded/foo/ps1/timestamp.txt") assert os.path.isfile("autograded/bar/ps1/p1.ipynb") assert not os.path.isfile("autograded/bar/ps1/timestamp.txt") gb = Gradebook(dbpath) notebook = gb.find_submission_notebook("p1", "ps1", "foo") assert_equal(notebook.score, 1) assert_equal(notebook.max_score, 4) assert_equal(notebook.needs_manual_grade, False) comment1 = gb.find_comment(0, "p1", "ps1", "foo") comment2 = gb.find_comment(1, "p1", "ps1", "foo") assert_equal(comment1.comment, "No response.") assert_equal(comment2.comment, "No response.") notebook = gb.find_submission_notebook("p1", "ps1", "bar") assert_equal(notebook.score, 2) assert_equal(notebook.max_score, 4) assert_equal(notebook.needs_manual_grade, True) comment1 = gb.find_comment(0, "p1", "ps1", "bar") comment2 = gb.find_comment(1, "p1", "ps1", "bar") assert_equal(comment1.comment, None) assert_equal(comment2.comment, None)
def test_grade(self, gradebook): """Can files be graded?""" self._copy_file("files/submitted-unchanged.ipynb", "source/ps1/p1.ipynb") run_command(["nbgrader", "assign", "ps1", "--db", gradebook]) self._copy_file("files/submitted-unchanged.ipynb", "submitted/foo/ps1/p1.ipynb") self._copy_file("files/submitted-changed.ipynb", "submitted/bar/ps1/p1.ipynb") run_command(["nbgrader", "autograde", "ps1", "--db", gradebook]) assert os.path.isfile("autograded/foo/ps1/p1.ipynb") assert not os.path.isfile("autograded/foo/ps1/timestamp.txt") assert os.path.isfile("autograded/bar/ps1/p1.ipynb") assert not os.path.isfile("autograded/bar/ps1/timestamp.txt") gb = Gradebook(gradebook) notebook = gb.find_submission_notebook("p1", "ps1", "foo") assert notebook.score == 1 assert notebook.max_score == 7 assert notebook.needs_manual_grade == False comment1 = gb.find_comment("set_a", "p1", "ps1", "foo") comment2 = gb.find_comment("baz", "p1", "ps1", "foo") comment3 = gb.find_comment("quux", "p1", "ps1", "foo") assert comment1.comment == "No response." assert comment2.comment == "No response." assert comment3.comment == "No response." notebook = gb.find_submission_notebook("p1", "ps1", "bar") assert notebook.score == 2 assert notebook.max_score == 7 assert notebook.needs_manual_grade == True comment1 = gb.find_comment("set_a", "p1", "ps1", "bar") comment2 = gb.find_comment("baz", "p1", "ps1", "bar") comment2 = gb.find_comment("quux", "p1", "ps1", "bar") assert comment1.comment == None assert comment2.comment == None
class GetGrades(NbGraderPreprocessor): """Preprocessor for saving grades from the database to the notebook""" def preprocess(self, nb, resources): # pull information from the resources self.notebook_id = resources['nbgrader']['notebook'] self.assignment_id = resources['nbgrader']['assignment'] self.student_id = resources['nbgrader']['student'] self.db_url = resources['nbgrader']['db_url'] # connect to the database self.gradebook = Gradebook(self.db_url) self.comment_index = 0 # process the cells nb, resources = super(GetGrades, self).preprocess(nb, resources) submission = self.gradebook.find_submission_notebook( self.notebook_id, self.assignment_id, self.student_id) resources['nbgrader']['score'] = submission.score resources['nbgrader']['max_score'] = submission.max_score return nb, resources def _get_comment(self, cell, resources): """Graders can optionally add comments to the student's solutions, so add the comment information into the database if it doesn't already exist. It should NOT overwrite existing comments that might have been added by a grader already. """ # retrieve or create the comment object from the database comment = self.gradebook.find_comment( self.comment_index, self.notebook_id, self.assignment_id, self.student_id) # save it in the notebook cell.metadata.nbgrader['comment'] = comment.to_dict() # update the number of comments we have inserted self.comment_index += 1 def _get_score(self, cell, resources): grade = self.gradebook.find_grade( cell.metadata['nbgrader']['grade_id'], self.notebook_id, self.assignment_id, self.student_id) cell.metadata.nbgrader['score'] = grade.score cell.metadata.nbgrader['points'] = grade.max_score def preprocess_cell(self, cell, resources, cell_index): # if it's a solution cell, then add a comment if utils.is_solution(cell): self._get_comment(cell, resources) # if it's a grade cell, the add a grade if utils.is_grade(cell): self._get_score(cell, resources) return cell, resources