|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + |
| 4 | +<head> |
| 5 | + <meta charset="UTF-8"> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 | + <title>Python Friendly Error Messages - Local Demo</title> |
| 8 | + <link rel="stylesheet" href="styles.css"> |
| 9 | +</head> |
| 10 | + |
| 11 | +<body> |
| 12 | + <div class="container"> |
| 13 | + <h1>Python Friendly Error Messages - Local Demo</h1> |
| 14 | + |
| 15 | + <div id="demo-container"> |
| 16 | + Loading demo... |
| 17 | + </div> |
| 18 | + </div> |
| 19 | + |
| 20 | + <script type="module"> |
| 21 | + import { friendlyExplain, loadCopydeck, registerAdapter, loadCopydeckFor, skulptAdapter, pyodideAdapter } from '../dist/index.browser.js'; |
| 22 | + |
| 23 | + // example traces with corresponding code |
| 24 | + const examples = [ |
| 25 | + { |
| 26 | + title: "NameError - Undefined Variable", |
| 27 | + runtime: "skulpt", |
| 28 | + code: `print("Hello") |
| 29 | +print(kittens)`, |
| 30 | + trace: `Traceback (most recent call last): |
| 31 | +File "main.py", line 2, in <module> |
| 32 | +NameError: name 'kittens' is not defined` |
| 33 | + }, |
| 34 | + { |
| 35 | + title: "SyntaxError - Missing Colon", |
| 36 | + runtime: "skulpt", |
| 37 | + code: `for i in range(3) |
| 38 | + print(i)`, |
| 39 | + trace: `Traceback (most recent call last): |
| 40 | +File "main.py", line 1 |
| 41 | + for i in range(3) |
| 42 | + ^ |
| 43 | +SyntaxError: invalid syntax` |
| 44 | + }, |
| 45 | + { |
| 46 | + title: "AttributeError - Using .push() on List", |
| 47 | + runtime: "pyodide", |
| 48 | + code: `items = [] |
| 49 | +items.push(3)`, |
| 50 | + trace: `Traceback (most recent call last): |
| 51 | +File "main.py", line 2, in <module> |
| 52 | + items.push(3) |
| 53 | +AttributeError: 'list' object has no attribute 'push'` |
| 54 | + }, |
| 55 | + { |
| 56 | + title: "TypeError - Adding String and Number", |
| 57 | + runtime: "pyodide", |
| 58 | + code: `age = 10 |
| 59 | +message = "I am " + age + " years old"`, |
| 60 | + trace: `Traceback (most recent call last): |
| 61 | +File "main.py", line 2, in <module> |
| 62 | + message = "I am " + age + " years old" |
| 63 | +TypeError: can only concatenate str (not "int") to str` |
| 64 | + }, |
| 65 | + { |
| 66 | + title: "NameError - Variable Used Before Assignment", |
| 67 | + runtime: "skulpt", |
| 68 | + code: `def calculate(): |
| 69 | + result = x + 5 |
| 70 | + x = 10 |
| 71 | + return result |
| 72 | +
|
| 73 | +calculate()`, |
| 74 | + trace: `Traceback (most recent call last): |
| 75 | +File "main.py", line 5, in <module> |
| 76 | + calculate() |
| 77 | +File "main.py", line 2, in calculate |
| 78 | +UnboundLocalError: local variable 'x' referenced before assignment` |
| 79 | + }, |
| 80 | + { |
| 81 | + title: "IndexError - List Index Out of Range", |
| 82 | + runtime: "pyodide", |
| 83 | + code: `numbers = [1, 2, 3] |
| 84 | +print(numbers[5])`, |
| 85 | + trace: `Traceback (most recent call last): |
| 86 | +File "main.py", line 2, in <module> |
| 87 | + print(numbers[5]) |
| 88 | +IndexError: list index out of range` |
| 89 | + }, |
| 90 | + { |
| 91 | + title: "KeyError - Dictionary Key Not Found", |
| 92 | + runtime: "skulpt", |
| 93 | + code: `person = {"name": "Alice", "age": 30} |
| 94 | +print(person["city"])`, |
| 95 | + trace: `Traceback (most recent call last): |
| 96 | +File "main.py", line 2, in <module> |
| 97 | + print(person["city"]) |
| 98 | +KeyError: 'city'` |
| 99 | + }, |
| 100 | + { |
| 101 | + title: "ZeroDivisionError - Division by Zero", |
| 102 | + runtime: "pyodide", |
| 103 | + code: `result = 10 / 0 |
| 104 | +print(result)`, |
| 105 | + trace: `Traceback (most recent call last): |
| 106 | +File "main.py", line 1, in <module> |
| 107 | + result = 10 / 0 |
| 108 | +ZeroDivisionError: division by zero` |
| 109 | + } |
| 110 | + ]; |
| 111 | + |
| 112 | + async function initDemo() { |
| 113 | + const container = document.getElementById('demo-container'); |
| 114 | + |
| 115 | + try { |
| 116 | + await loadCopydeckFor('en'); |
| 117 | + registerAdapter('skulpt', skulptAdapter); |
| 118 | + registerAdapter('pyodide', pyodideAdapter); |
| 119 | + |
| 120 | + let html = '<div class="demo__grid">'; |
| 121 | + |
| 122 | + for (const example of examples) { |
| 123 | + try { |
| 124 | + const result = friendlyExplain({ |
| 125 | + error: example.trace, |
| 126 | + code: example.code, |
| 127 | + audience: 'beginner', |
| 128 | + verbosity: 'standard' |
| 129 | + }); |
| 130 | + |
| 131 | + html += ` |
| 132 | + <div class="demo__card"> |
| 133 | + <h2> |
| 134 | + ${example.title} |
| 135 | + <span class="demo__badge demo__badge--${example.runtime}">${example.runtime}</span> |
| 136 | + </h2> |
| 137 | + <div class="demo__card-content"> |
| 138 | + <div class="demo__trace-section"> |
| 139 | + <h3>Original Trace</h3> |
| 140 | + <div class="demo__trace-raw">${escapeHtml(example.trace)}</div> |
| 141 | + </div> |
| 142 | + <div class="demo__trace-section"> |
| 143 | + <h3>Original Code</h3> |
| 144 | + <div class="demo__code-example">${escapeHtml(example.code)}</div> |
| 145 | + </div> |
| 146 | + <div class="demo__explanation"> |
| 147 | + <h3>Friendly Explanation</h3> |
| 148 | + ${result.html} |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + `; |
| 153 | + } catch (err) { |
| 154 | + html += ` |
| 155 | + <div class="demo__card"> |
| 156 | + <h2>${example.title}</h2> |
| 157 | + <div class="demo__error">Error processing: ${escapeHtml(err.message)}</div> |
| 158 | + </div> |
| 159 | + `; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + html += '</div>'; |
| 164 | + container.innerHTML = html; |
| 165 | + |
| 166 | + // click handlers for collapsible cards |
| 167 | + const cards = container.querySelectorAll('.demo__card'); |
| 168 | + cards.forEach(card => { |
| 169 | + const h2 = card.querySelector('h2'); |
| 170 | + if (h2) { |
| 171 | + h2.addEventListener('click', () => { |
| 172 | + card.classList.toggle('demo__card--expanded'); |
| 173 | + }); |
| 174 | + } |
| 175 | + }); |
| 176 | + } catch (err) { |
| 177 | + container.innerHTML = `<div class="demo__error">Failed to load demo: ${escapeHtml(err.message)}</div>`; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + function escapeHtml(text) { |
| 182 | + const div = document.createElement('div'); |
| 183 | + div.textContent = text; |
| 184 | + return div.innerHTML; |
| 185 | + } |
| 186 | + |
| 187 | + initDemo(); |
| 188 | + </script> |
| 189 | +</body> |
| 190 | + |
| 191 | +</html> |
0 commit comments