78 lines
2.5 KiB
HTML
78 lines
2.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Pyodide Real-Time Python Execution with Console</title>
|
|
<script src="https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.js"></script>
|
|
<style>
|
|
#console {
|
|
width: 100%;
|
|
height: 200px;
|
|
background-color: black;
|
|
color: white;
|
|
font-family: monospace;
|
|
padding: 10px;
|
|
overflow-y: auto;
|
|
border: 1px solid #ccc;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<h1>Real-Time Python Execution with Pyodide</h1>
|
|
<textarea id="python-code" rows="10" cols="50" placeholder="Enter your Python code here..."></textarea>
|
|
<br><br>
|
|
<button id="compile-button">Compile and Run</button>
|
|
<br><br>
|
|
<h2>Console Output:</h2>
|
|
<div id="console"></div>
|
|
|
|
<script>
|
|
const consoleElement = document.getElementById("console");
|
|
let pyodideWorker = new Worker("pyodide-worker.js");
|
|
|
|
pyodideWorker.onmessage = (event) => {
|
|
switch (event.data.type) {
|
|
case "console":
|
|
logToConsole(event.data.data);
|
|
break;
|
|
case "error":
|
|
logToConsole(`<span style="color:red;">${event.data.message}</span>`);
|
|
break;
|
|
case "fire":
|
|
fire(); // Call the JavaScript fire() function
|
|
break;
|
|
case "turn":
|
|
turn(event.data.data); // Call the JavaScript turn(deg) function
|
|
break;
|
|
}
|
|
};
|
|
|
|
function logToConsole(text) {
|
|
console.log(text.replace("<b>", "").replace("</b>", ""));
|
|
consoleElement.innerHTML += text.replace(/\n/g, "<br>") + "<br>";
|
|
consoleElement.scrollTop = consoleElement.scrollHeight;
|
|
}
|
|
|
|
// ✅ JavaScript functions to handle events
|
|
function fire() {
|
|
logToConsole("<b>🔥 Gun Fired! 🔥</b>");
|
|
}
|
|
|
|
function turn(deg) {
|
|
logToConsole(`<b>🔄 Turned ${deg} degrees</b>`);
|
|
}
|
|
|
|
// Send code to worker
|
|
document.getElementById("compile-button").addEventListener("click", () => {
|
|
const code = document.getElementById("python-code").value;
|
|
consoleElement.innerHTML = ""; // Clear console before running new code
|
|
pyodideWorker.postMessage({ code });
|
|
});
|
|
|
|
</script>
|
|
</body>
|
|
|
|
</html> |