Spaces:
Running
Running
Upload index.html with huggingface_hub
Browse files- index.html +31 -4
index.html
CHANGED
|
@@ -191,6 +191,24 @@ const slotTemplate = () => (mode === "run" ? RUN_TEMPLATE : PROBLEMS[prob].testT
|
|
| 191 |
const saveEditor = () => { if (mode === "run") work.run = editor.getValue(); else work.test[prob] = editor.getValue(); };
|
| 192 |
const loadEditor = () => editor.setValue(mode === "run" ? work.run : work.test[prob]);
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
const output = document.getElementById("output");
|
| 195 |
const summary = document.getElementById("summary");
|
| 196 |
const statusEl = document.getElementById("status");
|
|
@@ -232,6 +250,8 @@ function applyMode() {
|
|
| 232 |
modeTestBtn.classList.toggle("active", mode === "test");
|
| 233 |
leftLabel.textContent = mode === "test" ? "Your Test Code" : "Your Code";
|
| 234 |
rightLabel.textContent = mode === "test" ? "Test Results" : "Output";
|
|
|
|
|
|
|
| 235 |
modinfo.style.display = mode === "test" ? "" : "none";
|
| 236 |
summary.style.display = "none";
|
| 237 |
runBtn.innerHTML = (mode === "test" ? "▶ Run Tests " : "▶ Run ")
|
|
@@ -335,13 +355,20 @@ async function run() {
|
|
| 335 |
// Run the student's code in the (clean) main interpreter to define TESTS,
|
| 336 |
// then send ONLY the TESTS data to the worker. The worker holds the answer
|
| 337 |
// key in its own interpreter; nothing sensitive lives in this one.
|
| 338 |
-
|
| 339 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
let testsJson;
|
| 341 |
try {
|
| 342 |
-
testsJson = pyodide.runPython("import json\njson.dumps(
|
| 343 |
} catch (se) {
|
| 344 |
-
append(
|
| 345 |
return;
|
| 346 |
}
|
| 347 |
const rep = await gradeViaWorker(prob, code, testsJson);
|
|
|
|
| 191 |
const saveEditor = () => { if (mode === "run") work.run = editor.getValue(); else work.test[prob] = editor.getValue(); };
|
| 192 |
const loadEditor = () => editor.setValue(mode === "run" ? work.run : work.test[prob]);
|
| 193 |
|
| 194 |
+
// Pull ONLY the TESTS = [ ... ] list out of whatever the student typed; everything
|
| 195 |
+
// else is ignored (never executed). Balanced-bracket scan, quote-aware.
|
| 196 |
+
function extractTestsLiteral(src) {
|
| 197 |
+
// Anchor to line start so a commented "# TESTS = [ ... ]" line is NOT matched.
|
| 198 |
+
const m = /^[ \t]*TESTS\s*=\s*\[/m.exec(src);
|
| 199 |
+
if (!m) return null;
|
| 200 |
+
const start = src.indexOf("[", m.index);
|
| 201 |
+
let depth = 0, quote = null;
|
| 202 |
+
for (let j = start; j < src.length; j++) {
|
| 203 |
+
const ch = src[j];
|
| 204 |
+
if (quote) { if (ch === quote && src[j - 1] !== "\\") quote = null; continue; }
|
| 205 |
+
if (ch === '"' || ch === "'") { quote = ch; continue; }
|
| 206 |
+
if (ch === "[") depth++;
|
| 207 |
+
else if (ch === "]") { depth--; if (depth === 0) return src.slice(start, j + 1); }
|
| 208 |
+
}
|
| 209 |
+
return null;
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
const output = document.getElementById("output");
|
| 213 |
const summary = document.getElementById("summary");
|
| 214 |
const statusEl = document.getElementById("status");
|
|
|
|
| 250 |
modeTestBtn.classList.toggle("active", mode === "test");
|
| 251 |
leftLabel.textContent = mode === "test" ? "Your Test Code" : "Your Code";
|
| 252 |
rightLabel.textContent = mode === "test" ? "Test Results" : "Output";
|
| 253 |
+
const leftBadge = document.querySelector("#left .badge");
|
| 254 |
+
if (leftBadge) leftBadge.textContent = mode === "test" ? "only TESTS = [...] is read" : "editable";
|
| 255 |
modinfo.style.display = mode === "test" ? "" : "none";
|
| 256 |
summary.style.display = "none";
|
| 257 |
runBtn.innerHTML = (mode === "test" ? "▶ Run Tests " : "▶ Run ")
|
|
|
|
| 355 |
// Run the student's code in the (clean) main interpreter to define TESTS,
|
| 356 |
// then send ONLY the TESTS data to the worker. The worker holds the answer
|
| 357 |
// key in its own interpreter; nothing sensitive lives in this one.
|
| 358 |
+
// Regex out the TESTS = [ ... ] block and evaluate ONLY that literal. Student
|
| 359 |
+
// code is NEVER executed; anything outside the list is ignored. literal_eval
|
| 360 |
+
// accepts numbers/strings/tuples/lists/bools/None and rejects calls/names.
|
| 361 |
+
const lit = extractTestsLiteral(code);
|
| 362 |
+
if (!lit) {
|
| 363 |
+
append('\nNo "TESTS = [ ... ]" found. Define your test cases as a list, e.g.\nTESTS = [ ((50, 2, 30, "Lecture"), "SEATED") ]\n', "err");
|
| 364 |
+
return;
|
| 365 |
+
}
|
| 366 |
+
pyodide.globals.set("__lit", lit);
|
| 367 |
let testsJson;
|
| 368 |
try {
|
| 369 |
+
testsJson = pyodide.runPython("import ast, json\njson.dumps(ast.literal_eval(__lit))");
|
| 370 |
} catch (se) {
|
| 371 |
+
append('\nTESTS must contain only literal values (numbers, strings, tuples), e.g. ((50, 2, 30, "Lecture"), "SEATED").\n' + cleanError(se.message || String(se)) + "\n", "err");
|
| 372 |
return;
|
| 373 |
}
|
| 374 |
const rep = await gradeViaWorker(prob, code, testsJson);
|