Files
pyscript/pyscript.core/test/worker/index.html
Andrea Giammarchi f6dfc5361e Implement PyScript custom <script type> (#1548)
* updated MicroPython to latest in order to have `globals` API available
  * reduced code around helpers for both MicroPython and Pyodide as now these are more aligned
  * updated all dependencies and brought in latest [coincident/window](https://github.com/WebReflection/coincident#coincidentwindow) goodness to any `xworker`, preserving the `sync` previous behavior
  * using [@ungap/structured-clone/json](https://github.com/ungap/structured-clone#tojson) as *coincident* default `parse` and `stringify` utility to allow recursive and more complex data to travel back from the *Worker* (forward data is still fully [structured clone algorithm compatible](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm))
  * renamed all *plugin/s* references to *custom/s* as plugin as a word was too misleading
  * changed *custom types* helpers logic to allow any single node to have its own version of the interpreter wrapper, and all the extra fields it carries with it, including a way to augment every interpreter execution, among as every worker code execution
  * created a `custom` folder where I've landed the very first `pyscript.js` custom type
  * created an exhaustive test page to demonstrate the current abilities of *PyScript Next* among its ability to expose utilities that can be used to create *PyScript* plugins
2023-06-22 17:29:07 +02:00

63 lines
2.2 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>python workers</title>
<script type="importmap">
{
"imports": {
"basic-devtools": "../../node_modules/basic-devtools/esm/index.js",
"coincident/window": "../../node_modules/coincident/window.js",
"@ungap/with-resolvers": "../../node_modules/@ungap/with-resolvers/index.js",
"@ungap/structured-clone/json": "../../node_modules/@ungap/structured-clone/esm/json.js",
"@pyscript/core": "../../esm/index.js"
}
}
</script>
</head>
<body>
<div>See console ➡️</div>
<!-- XWorker - JavaScript to MicroPython -->
<script type="module">
import { XWorker } from "@pyscript/core";
const w = new XWorker("./worker.py", { type: "micropython" });
w.postMessage("JavaScript: Hello MicroPython 👋");
w.onmessage = (event) => {
console.log(event.data);
};
</script>
<!-- XWorker - MicroPython to MicroPython -->
<script type="micropython">
def handle_message(event):
print(event.data)
w = XWorker('./worker.py')
w.postMessage('MicroPython: Hello MicroPython 👋')
w.onmessage = handle_message
</script>
<!-- XWorker - MicroPython to Pyodide -->
<script type="micropython">
def handle_message(event):
print(event.data)
w = XWorker('./worker.py', **{'type': 'pyodide', 'async': True, 'config': '../fetch.toml'})
w.postMessage('MicroPython: Hello Pyodide 👋')
w.onmessage = handle_message
</script>
<!-- XWorker - MicroPython to Lua -->
<script type="micropython">
def handle_message(event):
print(event.data)
w = XWorker('./worker.lua', type='wasmoon')
w.postMessage('MicroPython: Hello Lua 👋')
w.onmessage = handle_message
</script>
</body>
</html>