Skip to content

Commit

Permalink
add initial attempt at pyodide integratiom
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 6, 2024
1 parent d9a23df commit 9a5bbdb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ rust/target
**/.coverage.*
**/coverage
**/dist/*
docs/_static/test-*.html
docs/_static/test-js.html
javascript/src/lib/fallbacks
7 changes: 2 additions & 5 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,16 @@ _static/test-js.html:
cp ../javascript/dist/bundle-js.js _static/dist/bundle-js.js
cp ../javascript/index.html _static/test-js.html

_static/test-py.html:
_static/dist/pyodide.js:
cd ../python && $(MAKE) webpack $(MFLAGS)
mkdir -p _static/dist
cp ../javascript/dist/bundle-py.js _static/dist/bundle-py.js
cat ../javascript/index.html | sed s#_static/bundle-js.js#_static/bundle-py.js > _static/test-py.html

_static/test-rs.html:
cd ../rust && $(MAKE) webpack $(MFLAGS)
mkdir -p _static/dist
cp ../rust/dist/bundle-rs.js _static/dist/bundle-rs.js
cat ../javascript/index.html | sed s#_static/bundle-js.js#_static/bundle-rs.js > _static/test-rs.html

html: _static/test-js.html
html: _static/test-js.html _static/dist/pyodide.js
$(SPHINXBUILD) -M html "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

# Catch-all target: route all unknown targets to Sphinx using the new
Expand Down
55 changes: 55 additions & 0 deletions docs/_static/test-py.html
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>
5 changes: 5 additions & 0 deletions python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ endif

clean:
rm -r ./{*,*/*}{.pyc,__pycache__,.mypy_cache} || echo

webpack:
mkdir -p ../docs/_static/dist
wget https://cdn.jsdelivr.net/pyodide/v0.26.2/full/pyodide.js -O ../docs/_static/dist/pyodide.js
tar --exclude=__pycache__ -czvf ../docs/_static/dist/python.tar.gz src

0 comments on commit 9a5bbdb

Please sign in to comment.