108 lines
3.6 KiB
HTML
108 lines
3.6 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>
|
|
async function initializePyodide() {
|
|
let pyodide = await loadPyodide({
|
|
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.23.4/full/"
|
|
});
|
|
|
|
// Redirect Python's stdout and stderr to the console area
|
|
pyodide.runPython(`
|
|
import sys
|
|
import asyncio
|
|
from js import document
|
|
|
|
class ConsoleOutput:
|
|
def write(self, text):
|
|
console = document.getElementById("console")
|
|
|
|
console.innerHTML += text.replace("\\n", "<br>") # Convert newlines to <br>
|
|
console.scrollTop = console.scrollHeight # Auto-scroll to bottom
|
|
|
|
def flush(self):
|
|
pass
|
|
|
|
sys.stdout = ConsoleOutput()
|
|
sys.stderr = ConsoleOutput()
|
|
`);
|
|
|
|
|
|
return pyodide;
|
|
}
|
|
|
|
async function runPythonCode(pyodide, code) {
|
|
try {
|
|
// Clear the console before running new code
|
|
document.getElementById("console").textContent = "";
|
|
|
|
// Run the Python code asynchronously
|
|
console.log(code);
|
|
function addExtraLineWithIndent(inputString, additionalText = "") {
|
|
// Split the string into an array of lines
|
|
const lines = inputString.split('\n');
|
|
const result = lines.map(line => {
|
|
// Check if the line ends with a colon
|
|
if (line.trim().endsWith(":")) {
|
|
return line; // Leave it as is
|
|
}
|
|
// Extract leading whitespace (indentation) from the current line
|
|
const indentation = line.match(/^\s*/)[0];
|
|
// Add the original line and the new inserted line with indentation
|
|
return line + '\n' + indentation + additionalText;
|
|
}).join('\n'); // Combine the lines back into a single string
|
|
return result;
|
|
}
|
|
|
|
|
|
const output = addExtraLineWithIndent(code, "await asyncio.sleep(0.01)");
|
|
console.log(output);
|
|
|
|
|
|
await pyodide.runPythonAsync(output);
|
|
} catch (err) {
|
|
// Display any errors in the console area
|
|
document.getElementById("console").textContent += `Error: ${err}\n`;
|
|
}
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
let pyodide = await initializePyodide();
|
|
|
|
document.getElementById("compile-button").addEventListener("click", async () => {
|
|
let code = document.getElementById("python-code").value;
|
|
await runPythonCode(pyodide, code);
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html> |