Cells¶
Solution stripping, marker validation, grading cell injection, and notebook post-processing.
mograder.grading.cells
¶
Notebook cell manipulation: marker validation, solution stripping, grading cell injection, and mark/feedback parsing.
validate_markers(lines, filepath)
¶
Check that all solution and hidden-test markers are properly paired.
Returns a list of error messages (empty if valid).
Source code in src/mograder/grading/cells.py
strip_solutions(lines)
¶
Remove solution blocks from source lines.
Lines between BEGIN SOLUTION / END SOLUTION are replaced with
# YOUR CODE HERE and pass at the correct indentation.
The pass ensures empty function bodies remain syntactically valid.
When a return statement after END SOLUTION references simple
variable names that are only defined inside the removed solution block,
sentinel assignments (name = ...) are inserted before the
placeholder so the return does not raise :class:NameError.
Source code in src/mograder/grading/cells.py
count_markers(lines)
¶
count_hidden_markers(lines)
¶
strip_hidden_tests(lines)
¶
Remove hidden test blocks from source lines.
Lines between BEGIN HIDDEN TESTS / END HIDDEN TESTS are replaced with
a single # HIDDEN TESTS placeholder comment at the correct indentation.
Source code in src/mograder/grading/cells.py
extract_hidden_tests(lines)
¶
Extract hidden test blocks as (indent, lines) tuples.
Each tuple contains the indentation prefix and the list of lines (with their original indentation) from one hidden-test block. Used during autograde to reinject hidden tests into submitted notebooks.
Source code in src/mograder/grading/cells.py
convert_markdown_cells(lines)
¶
Convert stripped markdown answer cells to editable mo.md() blocks.
After strip_solutions(), markdown answer cells look like::
response_text = "placeholder text"
# YOUR CODE HERE
pass
mo.md(response_text)
This function converts them to::
mo.md(r\"\"\"
placeholder text
\"\"\")
so that students see a clean editable markdown cell instead of ugly placeholder code.
Source code in src/mograder/grading/cells.py
build_submit_cell(server_url, assignment_name)
¶
Build a submit cell that uses mograder.remote.submit().
Returns source text for two marimo cells (username input + submit action).
Source code in src/mograder/grading/cells.py
strip_submit_cells(text)
¶
Remove the submit cell pair injected by :func:build_submit_cell.
The submit cell uses mo.ui.run_button which makes marimo export
wait indefinitely for a click in headless mode. Grader snapshots in
submitted/ must therefore not contain it.
Returns text unchanged if no submit cell is present.
Source code in src/mograder/grading/cells.py
read_notebook_type(text)
¶
Read mograder-type from a PEP 723 script block.
Returns "lecture", "assignment", etc. Defaults to
"assignment" when no metadata is found.
Source code in src/mograder/grading/cells.py
strip_layout_metadata(lines)
¶
Remove layout_file and html_head_file kwargs from marimo.App(...).
Handles both single-line and multi-line App() calls. Preserves
other keyword arguments (width, app_title, etc.).
Source code in src/mograder/grading/cells.py
rewrite_notebook_links(lines, base=None)
¶
Rewrite inter-notebook links [text](../Name/Name.py) to hub deep links.
- Lecture links (
Namestarts withL) become<base>run/Name/ - Assignment links (
Namestarts withA) become<base>edit/Name/, which opens the student's own copy (fetched from the release on first visit)
base is the hub URL, for links from outside the hub (e.g. handouts).
By default links are relative to a hub session page, so they work whatever
prefix the hub is served under (e.g. /live/hub/ behind a reverse proxy).
Source code in src/mograder/grading/cells.py
is_student_editable(code, name=None)
¶
Whether students are expected to edit this release cell.
True for solution cells (containing # YOUR CODE HERE) and for written
answer cells, which the release turns into plain markdown and identifies by
their cell name (written_analysis or written_analysis_<suffix>).
Such cells are excluded from integrity checks and cell hashes.
Source code in src/mograder/grading/cells.py
process_file(source, output_dir, dry_run=False, validate_only=False, submit_url=None)
¶
Process a single notebook file. Returns True on success.
Source code in src/mograder/grading/cells.py
is_release_aux_file(path)
¶
Whether a file next to a source notebook belongs in its release.
Data, images and helper modules do. Directories, dotfiles and build artifacts do not: an HTML preview exported from the source notebook contains the solutions, and zips are rebuilt for the release.
Source code in src/mograder/grading/cells.py
release_student_files(release_dir)
¶
Student-facing files of a release: everything except previews and zips.
Source code in src/mograder/grading/cells.py
release_attachment(release_dir)
¶
The file to attach to the Moodle assignment for a release.
The release zip built by generate when there are data files (built
here, reproducibly, if missing), else the single notebook file. Students'
downloads extract a zip next to the notebook.
Source code in src/mograder/grading/cells.py
build_release_zip(release_dir)
¶
Create a zip of student-facing release files, excluding artifacts.
Returns None (and removes any stale zip) when the directory contains only a single file — a zip that wraps one file adds no value.
Uses a fixed timestamp so the zip is reproducible across runs.
Source code in src/mograder/grading/cells.py
extract_marking_scale(source_lines)
¶
Extract the Marking Scale admonition from a source notebook.
Looks for /// details | Marking Scale ... /// block in markdown cells.
Returns the markdown content (without the admonition wrapper), or None.
Source code in src/mograder/grading/cells.py
parse_marks_metadata(source_lines)
¶
Extract marks metadata from a notebook.
Reads _marks = {...} from the MARKS_MARKER cell. All question marks
(both auto-checked and manual) must be listed in this single dict.
Returns None if no MARKS_MARKER cell found.
Source code in src/mograder/grading/cells.py
parse_auto_marks(source_lines)
¶
Extract auto-scored marks from a verification cell with marks data.
Looks for _mograder_marks and _mograder_checks in the verification
cell and computes sum of marks for PASS checks. Supports both 4-tuple
format (label, status, earned_weight, total_weight) (fractional) and
legacy 2-tuple format (label, status) (binary).
Returns None if no marks data found in the verification cell.
Source code in src/mograder/grading/cells.py
has_grading_cells(source_lines)
¶
Detect if grading cells are already injected.
inject_grading_cells(source_lines, checks, cell_errors=0, marks=None, source_check_keys=None)
¶
Insert verification summary + marker feedback cells before if __name__.
Returns modified source lines. Idempotent: if grading cells already exist, returns the input unchanged.
When marks is provided, the verification cell includes a marks column
and the feedback cell is pre-configured for manual-only grading.
source_check_keys, if given, determines which marks-dict keys are
auto-graded (have a check() call in the source notebook). When omitted,
keys are inferred from the student's executed checks — which may be
incomplete if mo.stop guards prevented some checks from running.
Source code in src/mograder/grading/cells.py
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 | |
parse_marker_feedback(source_lines)
¶
Extract _mark and _feedback from a graded notebook.
Looks for the MOGRADER: MARKER FEEDBACK marker and parses the variable assignments that follow it.
Returns (mark, feedback) where mark is None if not yet graded.
Source code in src/mograder/grading/cells.py
write_marker_feedback(file_path, mark, feedback)
¶
Write _mark and _feedback values into a graded notebook.
The file must contain a MOGRADER: MARKER FEEDBACK marker cell.
Always writes _feedback as a triple-quoted string.
Raises ValueError if the feedback marker is not found.