|  | 
| 2 | 2 | <html lang="en"> | 
| 3 | 3 | <head> | 
| 4 | 4 |   <meta charset="UTF-8" /> | 
| 5 |  | -  <title>Python Editor with Pyodide</title> | 
| 6 |  | - | 
| 7 |  | -  <!-- CodeMirror CSS --> | 
|  | 5 | +  <title>Python Editor (Pyodide)</title> | 
| 8 | 6 |   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.10/codemirror.min.css" /> | 
| 9 |  | - | 
| 10 | 7 |   <style> | 
| 11 | 8 |     body { font-family: Arial, sans-serif; margin: 20px; } | 
| 12 | 9 |     #editor { height: 300px; border: 1px solid #ccc; } | 
|  | 
| 16 | 13 | </head> | 
| 17 | 14 | <body> | 
| 18 | 15 | 
 | 
| 19 |  | -  <h2>Try Python in Your Browser</h2> | 
| 20 |  | -  <p>Write Python code below and click "Run Code".</p> | 
| 21 |  | - | 
| 22 |  | -  <textarea id="editor">print("Hello, Python!")</textarea><br/> | 
| 23 |  | -  <button id="runBtn">▶ Run Code</button> | 
| 24 |  | - | 
| 25 |  | -  <h3>Output:</h3> | 
| 26 |  | -  <div id="output"></div> | 
| 27 |  | - | 
| 28 |  | -  <!-- Load CodeMirror --> | 
| 29 |  | -  <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.10/codemirror.min.js"></script> | 
| 30 |  | -  <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.10/mode/python/python.min.js"></script> | 
| 31 |  | - | 
| 32 |  | -  <!-- Load Pyodide --> | 
| 33 |  | -  <script src="https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.js"></script> | 
| 34 |  | - | 
| 35 |  | -  <script> | 
| 36 |  | -    // Initialize CodeMirror | 
| 37 |  | -    var editor = CodeMirror.fromTextArea(document.getElementById("editor"), { | 
| 38 |  | -      mode: "python", | 
| 39 |  | -      lineNumbers: true, | 
| 40 |  | -      indentUnit: 4, | 
| 41 |  | -      tabSize: 4, | 
| 42 |  | -    }); | 
| 43 |  | - | 
| 44 |  | -    // Load Pyodide | 
| 45 |  | -    let pyodideReadyPromise = loadPyodide(); | 
| 46 |  | - | 
| 47 |  | -    // Run code button | 
| 48 |  | -    document.getElementById("runBtn").addEventListener("click", async () => { | 
| 49 |  | -      const outputElem = document.getElementById("output"); | 
| 50 |  | -      outputElem.textContent = "Running..."; | 
| 51 |  | -      try { | 
| 52 |  | -        const pyodide = await pyodideReadyPromise; | 
| 53 |  | -        let code = editor.getValue(); | 
| 54 |  | -        let result = await pyodide.runPythonAsync(code); | 
| 55 |  | -        outputElem.textContent = result === undefined ? "" : result.toString(); | 
| 56 |  | -      } catch (err) { | 
| 57 |  | -        outputElem.textContent = err; | 
| 58 |  | -      } | 
| 59 |  | -    }); | 
| 60 |  | -  </script> | 
|  | 16 | +<h2>Try Python in Your Browser</h2> | 
|  | 17 | +<textarea id="editor">print("Hello from Python!")</textarea><br> | 
|  | 18 | +<button id="runBtn">▶ Run Code</button> | 
|  | 19 | + | 
|  | 20 | +<h3>Output:</h3> | 
|  | 21 | +<div id="output">...</div> | 
|  | 22 | + | 
|  | 23 | +<!-- CodeMirror --> | 
|  | 24 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.10/codemirror.min.js"></script> | 
|  | 25 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.10/mode/python/python.min.js"></script> | 
|  | 26 | + | 
|  | 27 | +<!-- Pyodide --> | 
|  | 28 | +<script src="https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.js"></script> | 
|  | 29 | + | 
|  | 30 | +<script> | 
|  | 31 | +  // Initialize CodeMirror | 
|  | 32 | +  const cm = CodeMirror.fromTextArea(document.getElementById("editor"), { | 
|  | 33 | +    mode: "python", | 
|  | 34 | +    lineNumbers: true, | 
|  | 35 | +    theme: "default", | 
|  | 36 | +    indentUnit: 4, | 
|  | 37 | +    tabSize: 4, | 
|  | 38 | +  }); | 
|  | 39 | + | 
|  | 40 | +  let pyodideReady = false; | 
|  | 41 | +  let pyodide = null; | 
|  | 42 | + | 
|  | 43 | +  async function loadPyodideAndPackages() { | 
|  | 44 | +    pyodide = await loadPyodide(); | 
|  | 45 | +    pyodideReady = true; | 
|  | 46 | +    document.getElementById("output").textContent = "✅ Pyodide loaded. You can now run Python code."; | 
|  | 47 | +  } | 
|  | 48 | + | 
|  | 49 | +  loadPyodideAndPackages(); | 
|  | 50 | + | 
|  | 51 | +  document.getElementById("runBtn").addEventListener("click", async () => { | 
|  | 52 | +    const output = document.getElementById("output"); | 
|  | 53 | +    if (!pyodideReady) { | 
|  | 54 | +      output.textContent = "⏳ Please wait, Pyodide is still loading..."; | 
|  | 55 | +      return; | 
|  | 56 | +    } | 
|  | 57 | + | 
|  | 58 | +    output.textContent = ""; // clear old output | 
|  | 59 | +    try { | 
|  | 60 | +      const code = cm.getValue(); | 
|  | 61 | +      let result = await pyodide.runPythonAsync(code); | 
|  | 62 | +      output.textContent = result !== undefined ? result.toString() : "✅ Code executed."; | 
|  | 63 | +    } catch (err) { | 
|  | 64 | +      output.textContent = `❌ Error:\n${err}`; | 
|  | 65 | +    } | 
|  | 66 | +  }); | 
|  | 67 | +</script> | 
| 61 | 68 | 
 | 
| 62 | 69 | </body> | 
| 63 | 70 | </html> | 
0 commit comments