Coverage for e2xgrader/tests/test_utils/cells.py: 100%

38 statements  

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

1from typing import Any, Dict, Union 

2 

3from nbformat.notebooknode import NotebookNode 

4from nbformat.v4 import new_code_cell, new_markdown_cell 

5 

6 

7def nbgrader_metadata(**kwargs) -> Dict[str, Union[int, str, bool]]: 

8 """Create an nbgrader metadata dictionary 

9 Updates the metadata with any keyword arguments provided 

10 

11 Returns: 

12 Dict[str, Union[int, str, bool]]: metadata 

13 """ 

14 metadata = { 

15 "grade": False, 

16 "grade_id": "cell1", 

17 "locked": False, 

18 "schema_version": 3, 

19 "solution": False, 

20 "task": False, 

21 } 

22 metadata.update(kwargs) 

23 return metadata 

24 

25 

26def extra_cell_metadata( 

27 type: str, options: Dict[str, Any] = None, **kwargs 

28) -> Dict[str, Any]: 

29 """Create an extra cell metadata dictionary 

30 Updates the metadata with any keyword arguments provided 

31 

32 Args: 

33 type (str): the type of the extra cell 

34 options (Dict[str, Any], optional): An options dictionary. Defaults to None. 

35 

36 Returns: 

37 Dict[str, Any]: metadata 

38 """ 

39 metadata = dict(type=type, options=options if options is not None else dict()) 

40 metadata.update(kwargs) 

41 return metadata 

42 

43 

44def new_autograder_test_cell( 

45 source: str = "", points: int = 0, grade_id: str = "test_cell" 

46) -> NotebookNode: 

47 return new_code_cell( 

48 source=source, 

49 metadata=dict( 

50 nbgrader=nbgrader_metadata(grade=True, points=points, grade_id=grade_id) 

51 ), 

52 ) 

53 

54 

55def new_manually_graded_code_cell( 

56 source: str = "", grade_id: str = "autograded_answer" 

57) -> NotebookNode: 

58 return new_code_cell( 

59 source=source, 

60 metadata=dict( 

61 nbgrader=nbgrader_metadata(grade=True, solution=True, grade_id=grade_id) 

62 ), 

63 ) 

64 

65 

66def new_autograded_code_cell( 

67 source: str = "", points: int = 0, grade_id: str = "manual_answer" 

68) -> NotebookNode: 

69 return new_code_cell( 

70 source=source, 

71 metadata=dict( 

72 nbgrader=nbgrader_metadata( 

73 grade=False, solution=True, points=points, grade_id=grade_id 

74 ) 

75 ), 

76 ) 

77 

78 

79def new_manually_graded_markdown_cell( 

80 source: str = "", points: int = 0, grade_id: str = "manual_text_answer" 

81) -> NotebookNode: 

82 return new_markdown_cell( 

83 source=source, 

84 metadata=dict( 

85 nbgrader=nbgrader_metadata( 

86 grade=True, solution=True, points=points, grade_id=grade_id 

87 ) 

88 ), 

89 ) 

90 

91 

92def new_readonly_code_cell( 

93 source: str = "", grade_id: str = "code_example" 

94) -> NotebookNode: 

95 return new_code_cell( 

96 source=source, 

97 metadata=dict(nbgrader=nbgrader_metadata(locked=True, grade_id=grade_id)), 

98 ) 

99 

100 

101def new_readonly_markdown_cell( 

102 source: str = "", grade_id: str = "description" 

103) -> NotebookNode: 

104 return new_markdown_cell( 

105 source=source, 

106 metadata=dict(nbgrader=nbgrader_metadata(locked=True, grade_id=grade_id)), 

107 ) 

108 

109 

110def new_singlechoice_cell( 

111 source: str = "", points: int = 0, grade_id: str = "singlechoice", **kwargs 

112) -> NotebookNode: 

113 cell = new_manually_graded_markdown_cell( 

114 source=source, grade_id=grade_id, points=points 

115 ) 

116 cell.metadata["extended_cell"] = extra_cell_metadata(type="singlechoice", **kwargs) 

117 return cell 

118 

119 

120def new_multiplechoice_cell( 

121 source: str = "", points: int = 0, grade_id: str = "multiplechoice", **kwargs 

122) -> NotebookNode: 

123 cell = new_manually_graded_markdown_cell( 

124 source=source, grade_id=grade_id, points=points 

125 ) 

126 cell.metadata["extended_cell"] = extra_cell_metadata( 

127 type="multiplechoice", **kwargs 

128 ) 

129 return cell 

130 

131 

132def new_diagram_cell( 

133 source: str = "", 

134 points: int = 0, 

135 grade_id: str = "diagram", 

136 diagram: str = None, 

137 **kwargs, 

138): 

139 attachments = dict() 

140 if diagram is not None: 

141 attachments = {"diagram.png": {"image/png": diagram}} 

142 return new_markdown_cell( 

143 source=source, 

144 attachments=attachments, 

145 metadata=dict( 

146 nbgrader=nbgrader_metadata( 

147 grade=True, solution=True, points=points, grade_id=grade_id 

148 ), 

149 extended_cell=extra_cell_metadata(type="diagram", **kwargs), 

150 ), 

151 ) 

152 

153 

154def new_upload_cell( 

155 source: str = "", 

156 points: int = 0, 

157 grade_id: str = "attachments", 

158 attachments: Dict[str, Dict[str, str]] = None, 

159 **kwargs, 

160): 

161 return new_markdown_cell( 

162 source=source, 

163 attachments=attachments if attachments is not None else dict(), 

164 metadata=dict( 

165 nbgrader=nbgrader_metadata( 

166 grade=True, solution=True, points=points, grade_id=grade_id 

167 ), 

168 extended_cell=extra_cell_metadata(type="attachments", **kwargs), 

169 ), 

170 )