Skip to content

Instantly share code, notes, and snippets.

@tubaman
Created September 28, 2024 13:16
Show Gist options
  • Save tubaman/72ecf37ce6f818b66348015eb4b218bd to your computer and use it in GitHub Desktop.
Save tubaman/72ecf37ce6f818b66348015eb4b218bd to your computer and use it in GitHub Desktop.
AnyWidget prototype of pyodide-worker to main thread RPC
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"kernelspec": {
"name": "python",
"display_name": "Python (Pyodide)",
"language": "python"
},
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
}
},
"nbformat_minor": 5,
"nbformat": 4,
"cells": [
{
"id": "b5eb7556-92c3-4007-af20-6c35571cac3d",
"cell_type": "code",
"source": "%pip install -q anywidget==0.9.13\n# needs to be same version as we compiled into jupyterlite\n# https://github.com/vega/altair/issues/3409#issuecomment-2075540659",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 1
},
{
"id": "b478c8eb-5eb2-4009-adf0-378a675139cc",
"cell_type": "code",
"source": "import anywidget",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 2
},
{
"id": "b670de73-d893-4a13-b8dc-d865a25cd908",
"cell_type": "code",
"source": "import ipywidgets",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 3
},
{
"id": "6422d25a-cd01-40d9-9246-a95217dfc304",
"cell_type": "code",
"source": "import traitlets",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 4
},
{
"id": "9119307e-399e-4d2f-968a-fa9d88721bbb",
"cell_type": "code",
"source": "import pathlib",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 5
},
{
"id": "98887ade-c48b-4ff7-b102-0c380e3aba0f",
"cell_type": "code",
"source": "class MainThreadRPC(anywidget.AnyWidget):\n _esm = pathlib.Path(\"index.js\")\n request = traitlets.Unicode(\"\").tag(sync=True)\n response = traitlets.Unicode(\"\").tag(sync=True)",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 6
},
{
"id": "9ede3cd2-fc68-4264-8a90-cc987dfdbc29",
"cell_type": "code",
"source": "rpc = MainThreadRPC();",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 7
},
{
"id": "392814a8-88d6-4133-b0f1-08b8934c3dd0",
"cell_type": "code",
"source": "rpc",
"metadata": {
"trusted": true
},
"outputs": [
{
"execution_count": 8,
"output_type": "execute_result",
"data": {
"text/plain": "MainThreadRPC()",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 1,
"model_id": "0fc26334d70347eebd848fa13297aa6c"
}
},
"metadata": {}
}
],
"execution_count": 8
},
{
"id": "344a073e-b54a-4ff1-920e-5b911b59da63",
"cell_type": "code",
"source": "rpc.request = 'foo'",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 37
},
{
"id": "6dc1af26-d613-4e99-8b06-d3467d916fcc",
"cell_type": "code",
"source": "",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": null
}
]
}
export function render({model, el}) {
model.on("change:request", () => {
//btn.innerHTML = `count is ${count()}`;
console.log("calling " + window.GM.info);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment