Skip to content

Commit cae3a11

Browse files
committed
show markdown solutions
1 parent 17e7cc0 commit cae3a11

3 files changed

Lines changed: 33 additions & 8 deletions

File tree

experimental/fasthtml/gpu_puzzles/client.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const State = {
99
isDispatchReady: true, // don't allow multiple overlapping dispatches
1010
puzzleIndex: 0,
1111
wgslStatus: "",
12+
showSolution: false,
1213
};
1314

1415
codeState = [];
@@ -122,7 +123,6 @@ function initializeEditor() {
122123
});
123124
AppState.editor.setKeyboardHandler("ace/keyboard/vim");
124125
// AppState.editor.setValue(initialContent || "");
125-
console.log("Initial content:\n", initialContent);
126126
console.log("Editor initialized");
127127
}
128128

@@ -151,6 +151,9 @@ function setupEventListeners() {
151151
document.getElementById("next").addEventListener("click", () => {
152152
update({ type: "selectPuzzle", value: "next" });
153153
});
154+
document.getElementById("solution").addEventListener("click", () => {
155+
update({ type: "clickedSolution" });
156+
});
154157
}
155158

156159
////////////////////////////////////////
@@ -271,8 +274,15 @@ async function update(event) {
271274
} else {
272275
AppState.editor.setValue(AppState.module.getTemplate(AppState.puzzleIndex));
273276
}
277+
// reset showSolution when starting a new puzzle
278+
AppState.showSolution = false;
279+
}
280+
if (event.type === "clickedSolution") {
281+
AppState.showSolution = !AppState.showSolution;
282+
console.log("showSolution: ", AppState.showSolution);
274283
}
275284

285+
276286
updateEditor();
277287
render();
278288
}
@@ -286,7 +296,7 @@ function render() {
286296
console.log("AppState.checkAnswer: ", AppState.checkAnswer);
287297
document.getElementById("correct").textContent = AppState.checkAnswer
288298
? "Tests passed!"
289-
: "Some tests failed.";
299+
: "Some tests failed."
290300
if (AppState.checkAnswer) {
291301
document.getElementById("correct").style.color = "LimeGreen";
292302
document.getElementById("correct").style.fontWeight = "bold";
@@ -301,4 +311,14 @@ function render() {
301311
PuzzleSpec[AppState.puzzleIndex].name;
302312
document.getElementById("puzzle_description").textContent =
303313
PuzzleSpec[AppState.puzzleIndex].description;
314+
if (AppState.showSolution) {
315+
document.getElementById("output").style.display = "none";
316+
// use puzzleIndex to get markdown
317+
document.getElementById("writeup").innerHTML = "<zero-md src=\"/assets/markdown/puzzle" + (AppState.puzzleIndex + 1) + ".md\"></zero-md>";
318+
console.log("writeup: ", document.getElementById("writeup").innerHTML);
319+
document.getElementById("writeup").style.display = "block";
320+
} else {
321+
document.getElementById("output").style.display = "block";
322+
document.getElementById("writeup").style.display = "none";
323+
}
304324
}

experimental/fasthtml/gpu_puzzles/evaluator.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,6 @@ bool evaluate(Context &ctx, const std::string &kernelCode, int puzzleIndex) {
10911091
"Number of Workgroups (%s %s %s)\n\033[0m\n\r",
10921092
reset, toString(testCase.gridSize).c_str(), grey);
10931093

1094-
// wprintf("%s", buf);
1095-
10961094
char titleBuf[kBufSize];
10971095
if (testCase.nInputs > 1) {
10981096
for (size_t inp = 0; inp < testCase.nInputs; ++inp) {
@@ -1111,16 +1109,13 @@ bool evaluate(Context &ctx, const std::string &kernelCode, int puzzleIndex) {
11111109
}
11121110
ptr += snprintf(buf + ptr, kBufSize, "\n");
11131111
if (compilePassed) {
1114-
// wprintf("");
11151112
snprintf(titleBuf, sizeof(titleBuf), "%sGot %s", successColor, reset);
11161113
printVecBuf(output, titleBuf, buf, ptr);
1117-
// wprintf("");
11181114
}
11191115

11201116
snprintf(titleBuf, sizeof(titleBuf), "%sExpected %s", successColor, reset);
11211117
printVecBuf(testCase.expectedOutput, titleBuf, buf, ptr);
11221118
wprintf("%s", buf);
1123-
// wprintf("");
11241119
}
11251120

11261121
return allPassed;

experimental/fasthtml/gpu_puzzles/run.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def init_app() -> str:
7676
Link(rel="stylesheet", href="https://unpkg.com/tippy.js@6/dist/tippy.css"),
7777
Script(src="https://unpkg.com/@popperjs/core@2"),
7878
Script(src="https://unpkg.com/tippy.js@6"),
79+
Script(src="https://cdn.jsdelivr.net/npm/zero-md@2.2", type="module"),
7980
Script(src="/client.js"),
8081
Script(init_app()),
8182
*Socials(
@@ -108,6 +109,13 @@ async def serve_js():
108109
async def serve_wasm():
109110
return FileResponse(f"build/run.wasm")
110111

112+
@app.get("/assets/markdown/{fname:path}.md")
113+
async def serve_writeups(fname: str):
114+
return FileResponse(f"assets/markdown/{fname}.md")
115+
116+
@app.get("/assets/images/{fname:path}.png")
117+
async def serve_images(fname: str):
118+
return FileResponse(f"assets/images/{fname}.png")
111119

112120
def output():
113121
correctHeight = 27
@@ -121,7 +129,9 @@ def output():
121129
button("Solution", "solution"),
122130
style=f"width: 49vw; height:{correctHeight / 3 * 2}vh; float: right; font-size: 2rem; text-align: center; align-items: center; justify-content: center; margin-top: {correctHeight / 3}vh;",
123131
),
124-
Div(id="output", style=f"width: 49vw; height:{outputHeight}vh; float: right;"),
132+
Div(id="output", cls="overlay", style=f"width: 49vw; height:{outputHeight}vh; float: right;"),
133+
Div("", id="writeup", cls="overlay hidden", style=f"width: 49vw; height: {outputHeight}vh; float: right; background-color: white; padding: 2rem; overflow-y: scroll;"),
134+
125135
)
126136

127137

0 commit comments

Comments
 (0)