Coverage for e2xgrader/exporters/submissionexporter.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.2, created at 2024-03-14 13:22 +0100

1import os 

2from textwrap import dedent 

3 

4from nbformat.v4 import new_markdown_cell 

5from traitlets import Unicode 

6 

7from .exporter import E2xExporter 

8 

9 

10class SubmissionExporter(E2xExporter): 

11 """ 

12 Exporter class for generating HTML submission files from a Jupyter notebook exam. 

13 """ 

14 

15 hashcode_cell_template_name = Unicode( 

16 "hashcode_cell.html", help="The name of the hashcode cell template." 

17 ).tag(config=True) 

18 

19 exam_submitted_message = Unicode( 

20 "We have received your exam!", 

21 help="The message to display in the hashcode cell when the exam has been submitted.", 

22 ).tag(config=True) 

23 

24 verify_exam_message = Unicode( 

25 "Verify your exam below and then close the browser and shut down your computer.", 

26 help=dedent( 

27 """ 

28 The message to display in the hashcode cell telling the student to verify their exam. 

29 """ 

30 ), 

31 ).tag(config=True) 

32 

33 your_hashcode_message = Unicode( 

34 "Your hashcode:", 

35 help="The message to display in the hashcode cell before the hashcode.", 

36 ).tag(config=True) 

37 

38 def __init__(self, **kwargs): 

39 super().__init__(**kwargs) 

40 self.template_name = "form" 

41 

42 @property 

43 def template_paths(self): 

44 return super().template_paths + [ 

45 os.path.join(os.path.dirname(__file__), "templates") 

46 ] 

47 

48 def from_notebook_node(self, nb, resources=None, **kw): 

49 hashcode_template = self.environment.get_template( 

50 self.hashcode_cell_template_name 

51 ) 

52 hashcode_cell = new_markdown_cell( 

53 source=hashcode_template.render( 

54 hashcode=resources["hashcode"], 

55 timestamp=resources["timestamp"], 

56 exam_submitted_message=self.exam_submitted_message, 

57 verify_exam_message=self.verify_exam_message, 

58 your_hashcode_message=self.your_hashcode_message, 

59 ).replace("\n", ""), 

60 metadata={"name": "exam-submitted"}, 

61 ) 

62 nb.cells = [hashcode_cell] + nb.cells 

63 return super().from_notebook_node(nb, resources=resources, **kw)