Skip to content

Commit 325f3d0

Browse files
authored
feat: Add confetti effect and return JSON response for challenge result (laike9m#32)
* feat: Add confetti effect and return JSON response for challenge result * Remove console.log statement in challenge.html
1 parent 3e1e6e8 commit 325f3d0

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

templates/challenge.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.15/theme/material-darker.min.css" />
88
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.15/codemirror.min.js"></script>
99
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.15/mode/python/python.min.js"></script>
10+
<script src="https://cdn.jsdelivr.net/npm/js-confetti@latest/dist/js-confetti.browser.js"></script>
1011
<title>Python Type Challenge - {{ name }}</title>
1112

1213
<style type="text/css">
@@ -200,6 +201,7 @@
200201
</div>
201202

202203
<script type="text/javascript">
204+
let confetti = new JSConfetti();
203205
let initTheme = localStorage.getItem('theme') === 'dark' ? "material-darker" : "default"
204206
let sharedCodeMirrorOptions = {
205207
mode: "python",
@@ -238,8 +240,14 @@
238240
method: 'POST',
239241
body: code
240242
})
241-
.then(response => response.text())
242-
.then(result => document.getElementById("result").innerHTML = result)
243+
.then(response => response.json())
244+
.then(json => {
245+
// add confetti effect when passed
246+
if (json.passed) {
247+
confetti.addConfetti()
248+
}
249+
document.getElementById("result").innerHTML = json.message;
250+
})
243251
.catch((error) => {
244252
console.error('Error:', error);
245253
})

views/views.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import platform
22

3-
from flask import Blueprint, redirect, render_template, request
3+
from flask import Blueprint, jsonify, redirect, render_template, request
44

55
from .utils import challenge_manager
66

@@ -36,8 +36,9 @@ def run_challenge(name):
3636

3737
result = challenge_manager.run_challenge(user_code=code, name=name)
3838
if result.passed:
39-
return "<h2>✅ Congratulations! You completed the challenge 🎉</h2>"
39+
message = "<h2>✅ Congratulations! You passed the test 🎉</h2>"
40+
return jsonify({"passed": True, "message": message})
4041

4142
error_message = "<h2>❌ Challenge failed 😢</h2>"
4243
error_message += f"<p>Error:\n{result.stdout}{result.stderr}</p>"
43-
return error_message
44+
return jsonify({"passed": False, "message": error_message})

0 commit comments

Comments
 (0)