Skip to content

Commit

Permalink
Embed JS files for testing webworker into pytest-pyodide wheel (#112)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ryanking13 and pre-commit-ci[bot] authored Dec 9, 2023
1 parent 045c569 commit f5b3535
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
- BREAKING: dropped support for Node < 18.
[#113](https://github.com/pyodide/pytest-pyodide/pull/113)

- Added webworker test template files into the package.
[#112](https://github.com/pyodide/pytest-pyodide/pull/112)

## [0.53.1] - 2023-10-10

- Removed the ctypes dependency so it can be used with Python builds with
Expand Down
23 changes: 23 additions & 0 deletions pytest_pyodide/_templates/module_webworker_dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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];
}
}

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; });
}
};
23 changes: 23 additions & 0 deletions pytest_pyodide/_templates/webworker_dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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];
}
}

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; });
}
};
10 changes: 8 additions & 2 deletions pytest_pyodide/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ 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 +55,11 @@ 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 f5b3535

Please sign in to comment.