Skip to content

Commit

Permalink
Embed webworker test js files into pytest-pyodide
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanking13 committed Oct 19, 2023
1 parent bfa37e2 commit 0cdd5cc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
26 changes: 26 additions & 0 deletions pytest_pyodide/_templates/module_webworker_dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { loadPyodide } from "./pyodide.mjs";

onmessage = async function (e) {
try {
const data = e.data;
for (let key of Object.keys(data)) {
if (key !== "python") {
// Keys other than python must be arguments for the python script.
// Set them on self, so that `from js import key` works.
self[key] = data[key];
}
}

if (!loadPyodide.inProgress) {
self.pyodide = await loadPyodide();
}
await self.pyodide.loadPackagesFromImports(data.python);
let results = await self.pyodide.runPythonAsync(data.python);
self.postMessage({ results });
} catch (e) {
// if you prefer messages with the error
self.postMessage({ error: e.message + "\n" + e.stack });
// if you prefer onerror events
// setTimeout(() => { throw err; });
}
};
26 changes: 26 additions & 0 deletions pytest_pyodide/_templates/webworker_dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
importScripts("./pyodide.js");

onmessage = async function (e) {
try {
const data = e.data;
for (let key of Object.keys(data)) {
if (key !== "python") {
// Keys other than python must be arguments for the python script.
// Set them on self, so that `from js import key` works.
self[key] = data[key];
}
}

if (!loadPyodide.inProgress) {
self.pyodide = await loadPyodide();
}
await self.pyodide.loadPackagesFromImports(data.python);
let results = await self.pyodide.runPythonAsync(data.python);
self.postMessage({ results });
} catch (e) {
// if you prefer messages with the error
self.postMessage({ error: e.message + "\n" + e.stack });
// if you prefer onerror events
// setTimeout(() => { throw err; });
}
};
6 changes: 4 additions & 2 deletions pytest_pyodide/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def _default_templates() -> dict[str, bytes]:
templates_dir = pathlib.Path(__file__).parent / "_templates"

templates = {}
for template_file in templates_dir.glob("*.html"):
template_files = list(templates_dir.glob("*.html")) + list(templates_dir.glob("*.js"))
for template_file in template_files:
templates[f"/{template_file.name}"] = template_file.read_bytes()

return templates
Expand Down Expand Up @@ -52,8 +53,9 @@ def get_template(self, path: str) -> bytes | None:
def do_GET(self):
body = self.get_template(self.path)
if body:
content_type = "application/javascript" if self.path.endswith(".js") else "text/html"
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.send_header("Content-type", f"{content_type}; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.end_headers()

Expand Down

0 comments on commit 0cdd5cc

Please sign in to comment.