53 lines
1.8 KiB
HTML
53 lines
1.8 KiB
HTML
<!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> |