-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Embed webworker test js files into pytest-pyodide
- Loading branch information
1 parent
bfa37e2
commit 0cdd5cc
Showing
3 changed files
with
56 additions
and
2 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
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; }); | ||
} | ||
}; |
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,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; }); | ||
} | ||
}; |
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