implemented basic text box and some test function hooks

master
Jake 2025-03-23 14:15:59 +08:00
commit e8a50c2c6b
1 changed files with 53 additions and 0 deletions

53
index.html Normal file
View File

@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Web Executor</title>
<script src="https://cdn.jsdelivr.net/pyodide/v0.24.0/full/pyodide.js"></script>
<style>
body { font-family: Arial, sans-serif; text-align: center; }
textarea { width: 80%; height: 150px; }
button { margin-top: 10px; }
pre { background: #f4f4f4; padding: 10px; }
</style>
</head>
<body>
<h2>Python Web Executor</h2>
<textarea id="code" placeholder="Write Python code here..."></textarea><br>
<button onclick="runPython()">Run</button>
<pre id="output"></pre>
<script>
async function loadPyodideAndSetup() {
window.pyodide = await loadPyodide();
await pyodide.runPythonAsync(`
import sys
from js import console
def move_forward():
print("Moving forward!")
def turn_left():
print("Turning left!")
def fire():
print("Firingg!")
globals()['move_forward'] = move_forward
globals()['turn_left'] = turn_left
globals()['fire'] = fire
`);
}
async function runPython() {
let code = document.getElementById("code").value;
let outputElement = document.getElementById("output");
try {
let result = await pyodide.runPythonAsync(code);
outputElement.textContent = result !== undefined ? result.toString() : "";
} catch (err) {
outputElement.textContent = "Error: " + err;
}
}
loadPyodideAndSetup();
</script>
</body>
</html>