-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
57 lines (48 loc) · 1.6 KB
/
worker.js
File metadata and controls
57 lines (48 loc) · 1.6 KB
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
self.importScripts('https://cdn.jsdelivr.net/pyodide/v0.27.5/full/pyodide.js');
async function loadPyodideAndPackages() {
let pyodide = await loadPyodide();
await pyodide.loadPackage("micropip");
const micropip = pyodide.pyimport("micropip");
await micropip.install('deepdiff');
await micropip.install('endf_parserpy');
// Set the stdout handler
// pyodide.setStdout({
// batched: (message) => {
// self.postMessage({ output: message });
// }
// });
// Set the stderr handler
pyodide.setStderr({
batched: (message) => {
self.postMessage({ warning: message });
}
});
return pyodide;
}
loadPyodideAndPackages().then(pyodide => {
self.onmessage = async (event) => {
const { lhsText, rhsText } = event.data;
// Run the Python code
const pythonCode = `
import json
from deepdiff import DeepDiff
import endf_parserpy as ep
lhs_data = """${lhsText.replace(/"""/g, '\\"""')}""" # Escape triple quotes
rhs_data = """${rhsText.replace(/"""/g, '\\"""')}""" # Escape triple quotes
parser = ep.EndfParser(print_cache_info=False)
tape_lhs = parser.parse(lhs_data)
tape_rhs = parser.parse(rhs_data)
d = DeepDiff(tape_lhs, tape_rhs)
diffs_json = d.to_json()
diffs_json
`;
try {
let output = await pyodide.runPythonAsync(pythonCode);
// Send the result back to the main thread
self.postMessage({ output: output });
} catch (error) {
// Send any errors back to the main thread
self.postMessage({ error: error.message });
}
};
});