|  | 
|  | 1 | +<!DOCTYPE html> | 
|  | 2 | +<html lang="en"> | 
|  | 3 | +<head> | 
|  | 4 | + <meta charset="UTF-8" /> | 
|  | 5 | + <title>Python Editor with Pyodide</title> | 
|  | 6 | + | 
|  | 7 | + <!-- CodeMirror CSS --> | 
|  | 8 | + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.10/codemirror.min.css" /> | 
|  | 9 | + | 
|  | 10 | + <style> | 
|  | 11 | + body { font-family: Arial, sans-serif; margin: 20px; } | 
|  | 12 | + #editor { height: 300px; border: 1px solid #ccc; } | 
|  | 13 | + #output { border: 1px solid #ccc; padding: 10px; white-space: pre-wrap; margin-top: 10px; background: #f9f9f9; } | 
|  | 14 | + button { margin-top: 10px; padding: 8px 16px; } | 
|  | 15 | + </style> | 
|  | 16 | +</head> | 
|  | 17 | +<body> | 
|  | 18 | + | 
|  | 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> | 
|  | 61 | + | 
|  | 62 | +</body> | 
|  | 63 | +</html> | 
0 commit comments