Skip to content

Instantly share code, notes, and snippets.

@declann
Created December 4, 2024 22:19
Show Gist options
  • Save declann/83f3c179cd922cc54679cd26e5c1534d to your computer and use it in GitHub Desktop.
Save declann/83f3c179cd922cc54679cd26e5c1534d to your computer and use it in GitHub Desktop.
positron widget buffers issue
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# This is broke:\n",
"\n",
"When I click the button, `1` should be appended to `msgs` file. Instead `0` is.\n",
"\n",
"(1 or 0 is the length of buffers list. In VSCode it is `1`, which is what I expect)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"vscode": {
"languageId": "python"
}
},
"outputs": [],
"source": [
"import anywidget\n",
"import traitlets\n",
"\n",
"\n",
"class CounterWidget(anywidget.AnyWidget):\n",
" _esm = \"\"\" \n",
" function render({ model, el }) {\n",
" let count = () => model.get(\"value\");\n",
" let btn = document.createElement(\"button\");\n",
" btn.classList.add(\"counter-button\");\n",
" btn.innerHTML = `count is ${count()}`;\n",
" btn.addEventListener(\"click\", () => {\n",
"\n",
" model.set(\"value\", count() + 1);\n",
" model.save_changes();\n",
"\n",
" let bytes = new TextEncoder().encode(\"Hello, world\");\n",
" model.send({message: \"Hello\"}, undefined, [new DataView(bytes.buffer)]);\n",
"\n",
" });\n",
" model.on(\"change:value\", () => {\n",
" btn.innerHTML = `count is ${count()}`;\n",
" });\n",
" el.appendChild(btn);\n",
" }\n",
" export default { render };\n",
" \"\"\"\n",
" _css = \"\"\"\n",
" .counter-button {\n",
" background-image: linear-gradient(to right, #a1c4fd, #c2e9fb);\n",
" border: 0;\n",
" border-radius: 10px;\n",
" padding: 10px 50px;\n",
" color: white;\n",
" }\n",
" \"\"\"\n",
"\n",
" def __init__(self, *args, **kwargs):\n",
" super().__init__(*args, **kwargs)\n",
" self.on_msg(self._handle_custom_msg)\n",
"\n",
" def _handle_custom_msg(self, msg: dict, buffers: list):\n",
" with open(\"msgs\", \"a\") as file:\n",
" file.write(str(len(buffers)) + \"\\n\")\n",
"\n",
" value = traitlets.Int(0).tag(sync=True)\n",
"\n",
"w = CounterWidget()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"vscode": {
"languageId": "python"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b9d4bc5c1ff341e58293790494ccd10b",
"version_major": 2,
"version_minor": 1
},
"text/plain": [
"CounterWidget()"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"w"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# This works: (Fixed issue #3974)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"vscode": {
"languageId": "python"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5be5018e1a954578b5a44c2c90ec0125",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Image(value=b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\x01Y\\x00\\x00\\x01c\\x08\\x06\\x00\\x00\\x00\\x97\\x13x\\x10\\x…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import ipywidgets\n",
"with open('/home/declan/Downloads/visualization.png', 'rb') as f: image = f.read() # you'll need an actual image file\n",
"display(ipywidgets.Image(value=image, format='png'))"
]
}
],
"metadata": {
"language_info": {
"name": "plaintext"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment