-
-
Save tonyfast/a990c66c804e0dbed659b66d222aea87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# awidgetable" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 82, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import ipywidgets as W, traitlets as T, IPython, random\n", | |
"from asyncio.queues import Queue\n", | |
"import asyncio" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class WillHave(object):\n", | |
" _widget = None\n", | |
"\n", | |
" def __init__(self, widget):\n", | |
" self._widget = widget\n", | |
"\n", | |
" async def __get(self, value):\n", | |
" return await self._widget._queues[value][\"queue\"].get()\n", | |
" \n", | |
" def __getattr__(self, value):\n", | |
" return self.__get(value)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Awaitable(T.HasTraits):\n", | |
" _queues = T.Dict()\n", | |
" will_have = T.Instance(WillHave)\n", | |
" \n", | |
" @T.default(\"_queues\")\n", | |
" def _default_queues(self):\n", | |
" queues = {}\n", | |
" for trait in self.trait_names():\n", | |
" queues[trait] = self._enqueue_trait(trait)\n", | |
" return queues\n", | |
" \n", | |
" @T.default(\"will_have\")\n", | |
" def _default_will_have(self):\n", | |
" return WillHave(self)\n", | |
" \n", | |
" def _enqueue_trait(self, trait):\n", | |
" queue = Queue()\n", | |
"\n", | |
" def _on_change(change):\n", | |
" # don't `put` if nobody's `get`ting\n", | |
" queue.empty() and queue.put_nowait({\"old\": change[\"old\"], \"new\": change[\"new\"]})\n", | |
" \n", | |
" self.observe(_on_change, trait)\n", | |
" return {\n", | |
" \"queue\": queue,\n", | |
" \"handler\": _on_change\n", | |
" }" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class AwaitableFloatSlider(Awaitable, W.FloatSlider):\n", | |
" pass" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Some example code that might be awaiting." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "5ad2f895c5ff407db6f202e29a59f7f8", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"AwaitableFloatSlider(value=0.0, layout=Layout(width='100%'))" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"try: del afs, printy\n", | |
"except: pass\n", | |
"afs = AwaitableFloatSlider(layout=dict(width=\"100%\"))\n", | |
"display(afs)\n", | |
"\n", | |
"md = \"text/markdown\"\n", | |
"\n", | |
"max_dv = 0\n", | |
"\n", | |
"async def printy():\n", | |
" global max_dv\n", | |
" disp = IPython.display.display({md: \"> Move the slider quickly!\"}, raw=True, display_id=True)\n", | |
" while True:\n", | |
" c1 = await afs.will_have.value\n", | |
" c2 = await afs.will_have.value\n", | |
" dv = int(c2[\"new\"] - c1[\"old\"])\n", | |
" max_dv = max(max_dv, abs(dv))\n", | |
" tmp = \"# last speed `{}`\\n\\n> # {}\"\n", | |
" for i in range(10):\n", | |
" msg = tmp.format(dv, random.choice(\"⠺⠭⠽⠵\"))\n", | |
" disp.update({md: msg}, raw=True, update=True)\n", | |
" await asyncio.sleep(0.04 * i)\n", | |
" msg += f\"\\n# max speed `{max_dv}`\"\n", | |
" disp.update({md: msg}, raw=True, update=True)\n", | |
" \n", | |
" if abs(dv) == max_dv:\n", | |
" msg += \"\\n# Good Job!\"\n", | |
" disp.update({md: msg}, raw=True, update=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Queue it up." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/markdown": [ | |
"# last speed `75`\n", | |
"\n", | |
"> # ⠺\n", | |
"# max speed `75`\n", | |
"# Good Job!" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"asyncio.ensure_future(printy());" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment