From 7a15308707dc517b1076766a8592355b79a6352c Mon Sep 17 00:00:00 2001 From: Tim Daniel Metzler Date: Wed, 13 Mar 2024 14:17:41 +0100 Subject: [PATCH] Add SubmissionExporter to turn exam into HTML with hashcode and timestamp --- e2xgrader/exporters/__init__.py | 6 +-- e2xgrader/exporters/submissionexporter.py | 63 +++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 e2xgrader/exporters/submissionexporter.py diff --git a/e2xgrader/exporters/__init__.py b/e2xgrader/exporters/__init__.py index f2d09e08..d527cf81 100644 --- a/e2xgrader/exporters/__init__.py +++ b/e2xgrader/exporters/__init__.py @@ -1,7 +1,5 @@ from .exporter import E2xExporter from .gradeexporter import GradeExporter +from .submissionexporter import SubmissionExporter -__all__ = [ - "E2xExporter", - "GradeExporter", -] +__all__ = ["E2xExporter", "GradeExporter", "SubmissionExporter"] diff --git a/e2xgrader/exporters/submissionexporter.py b/e2xgrader/exporters/submissionexporter.py new file mode 100644 index 00000000..20e24f68 --- /dev/null +++ b/e2xgrader/exporters/submissionexporter.py @@ -0,0 +1,63 @@ +import os +from textwrap import dedent + +from nbformat.v4 import new_markdown_cell +from traitlets import Unicode + +from .exporter import E2xExporter + + +class SubmissionExporter(E2xExporter): + """ + Exporter class for generating HTML submission files from a Jupyter notebook exam. + """ + + hashcode_cell_template_name = Unicode( + "hashcode_cell.html", help="The name of the hashcode cell template." + ).tag(config=True) + + exam_submitted_message = Unicode( + "We have received your exam!", + help="The message to display in the hashcode cell when the exam has been submitted.", + ).tag(config=True) + + verify_exam_message = Unicode( + "Verify your exam below and then close the browser and shut down your computer.", + help=dedent( + """ + The message to display in the hashcode cell telling the student to verify their exam. + """ + ), + ).tag(config=True) + + your_hashcode_message = Unicode( + "Your hashcode:", + help="The message to display in the hashcode cell before the hashcode.", + ).tag(config=True) + + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.template_name = "form" + + @property + def template_paths(self): + return super().template_paths + [ + os.path.join(os.path.dirname(__file__), "templates") + ] + + def from_notebook_node(self, nb, resources=None, **kw): + hashcode_template = self.environment.get_template( + self.hashcode_cell_template_name + ) + hashcode_cell = new_markdown_cell( + source=hashcode_template.render( + hashcode=resources["hashcode"], + timestamp=resources["timestamp"], + exam_submitted_message=self.exam_submitted_message, + verify_exam_message=self.verify_exam_message, + your_hashcode_message=self.your_hashcode_message, + ).replace("\n", ""), + metadata={"name": "exam-submitted"}, + ) + nb.cells = [hashcode_cell] + nb.cells + return super().from_notebook_node(nb, resources=resources, **kw)