-
Notifications
You must be signed in to change notification settings - Fork 0
/
webworker.js
64 lines (53 loc) · 1.86 KB
/
webworker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
importScripts("https://cdn.jsdelivr.net/pyodide/v0.18.1/full/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({ indexURL: "./" });
}
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; });
}
};
async function loadPyodideAndPackages() {
self.pyodide = await loadPyodide({
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.18.1/full/",
});
// Double imports here are unnecessary, maybe needed later...
//
// await self.pyodide.loadPackage(["numpy", "pandas", "micropip"])
// pyodide.runPythonAsync(`
// import micropip
// micropip.install('https://files.pythonhosted.org/packages/69/bf/f0f194d3379d3f3347478bd267f754fc68c11cbf2fe302a6ab69447b1417/beautifulsoup4-4.10.0-py3-none-any.whl')
// `);
}
let pryodideReadyPromise = loadPyodideAndPackages();
self.onmessage = async (event) => {
//
//Ensure loading is Done
await pryodideReadyPromise;
const { python, ...context} = event.data;
for (const key of Object.keys(context)) {
self[key] = context[key];
}
try {
await self.pyodide.loadPackagesFromImports(python);
let results = await self.pyodide.runPythonAsync(python);
self.postMessage({ results });
} catch (error) {
self.postMessage({ error: error.message })
}
}