Created
November 1, 2021 07:26
-
-
Save ohidurbappy/93cf716e4eff3029882797570430b46e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://cdn.jsdelivr.net/pyodide/v0.18.1/full/pyodide.js"></script> | |
</head> | |
<body> | |
<p> | |
You can execute any Python code. Just enter something in the box below and | |
click the button. | |
</p> | |
<textarea id="code" style="width: 100%;" rows="10" /></textarea> | |
<button onclick="evaluatePython()">Run</button> | |
<br /> | |
<br /> | |
<div>Output:</div> | |
<textarea id="output" style="width: 100%;" rows="10" disabled></textarea> | |
<script> | |
const output = document.getElementById("output"); | |
const code = document.getElementById("code"); | |
function addToOutput(s) { | |
output.value += ">>>" + code.value + "\n" + s + "\n"; | |
} | |
output.value = "Initializing...\n"; | |
// init Pyodide | |
async function main() { | |
let pyodide = await loadPyodide({ | |
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.18.1/full/", | |
}); | |
output.value += "Ready!\n"; | |
return pyodide; | |
} | |
let pyodideReadyPromise = main(); | |
async function evaluatePython() { | |
let pyodide = await pyodideReadyPromise; | |
try { | |
let output = pyodide.runPython(code.value); | |
addToOutput(output); | |
} catch (err) { | |
addToOutput(err); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment