-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial attempt at pyodide integratiom
- Loading branch information
1 parent
d9a23df
commit 9a5bbdb
Showing
4 changed files
with
63 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<script src="./dist/pyodide.js"></script> | ||
</head> | ||
|
||
<body> | ||
<p> | ||
You can execute any Python code. Just enter something in the box below and | ||
click the button. | ||
</p> | ||
<input id="code" value="sum([1, 2, 3, 4, 5])" /> | ||
<button onclick="evaluatePython()">Run</button> | ||
<br /> | ||
<br /> | ||
<div>Output:</div> | ||
<textarea id="output" style="width: 100%;" rows="6" 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(); | ||
output.value += "Ready!\n"; | ||
return pyodide; | ||
} | ||
let pyodideReadyPromise = main(); | ||
pyodideReadyPromise.then(() => { | ||
await micropip.install(["sortedcontainers", "u-msgpack-python"]) | ||
await pyodide.runPythonAsync(` | ||
from pyodide.http import pyfetch | ||
response = await pyfetch("https://euler.oliviaappleton.com/_static/dist/python.tar.gz") # .zip, .whl, ... | ||
await response.unpack_archive() # by default, unpacks to the current dir | ||
`); | ||
}); | ||
|
||
async function evaluatePython() { | ||
let pyodide = await pyodideReadyPromise; | ||
try { | ||
let output = pyodide.runPython(code.value); | ||
addToOutput(output); | ||
} catch (err) { | ||
addToOutput(err); | ||
} | ||
} | ||
</script> | ||
</body> | ||
</html> |
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