Replies: 1 comment 1 reply
-
Currently, the recommended approach for sharing static assets in anywidget is through the use of Here's an example creating a Web Worker dynamically: import traitlets
import anywidget
class Widget(anywidget.AnyWidget):
_esm = """
function render({ model, el }) {
const blob = new Blob([model.get("_worker_asset")], { type: "application/javascript" });
const worker = new Worker(URL.createObjectURL(blob));
worker.postMessage({ name: model.get("_worker_name") });
worker.onmessage = function(event) {
console.log("Message from worker:", event.data);
};
}
export default { render };
"""
_worker_asset = traitlets.Unicode("""
self.onmessage = function(event) {
console.log("Worker received:", event.data);
const response = "Hello, " + event.data.name + "!";
self.postMessage(response);
};
""").tag(sync=True)
_worker_name = traitlets.Unicode("anywidget").tag(sync=True) I have considered creating a more declarative API for this, but it would essentially serve as sugar over this type of implementation. Something like JavaScript import maps could simplify handling JavaScript assets specifically, but this approach would require additional support from hosts (e.g., Marimo, Panel). I think it's worth discussing with the maintainers before adding such a requirement and explore the limitations of sending assets over the comms first. |
Beta Was this translation helpful? Give feedback.
-
I am trying to create a medical image viewer widget using cornerstone, but I am hitting several snag. Cornerstone make use of web workers that are not inlined and bundled in the main output. This pretty much prevents me from using esm.sh as cross origin loading of workers seems impossible. Essentially, I believe I now need a way to serve static assets. I am using marimo and they are serving anywidget assets via a special
/@file/
endpoint. I feel like anywidget should mandate a way to serve various static assets (icons, worker src, wasm, etc) outside of the bundle, but can't find any documentation. Am I missing something?I am also opened a ticket on the marimo repo.
Beta Was this translation helpful? Give feedback.
All reactions