-
-
Save tonyfast/0bf98418128c7823f8cf2ecf30bea8fa to your computer and use it in GitHub Desktop.
Unembed widget json into python
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
ipywidgets |
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": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import ipywidgets as W\n", | |
"import ipywidgets.embed\n", | |
"import uuid\n", | |
"import json\n", | |
"import pprint\n", | |
"import asyncio" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def replace_widget_ids(dependency_data):\n", | |
" \"\"\" make this a reusable template by replacing the widget ids\n", | |
" \"\"\"\n", | |
" id_map = {old_id: uuid.uuid4().hex for old_id in dependency_data}\n", | |
" dependency_data_with_new_ids = json.dumps(dependency_data)\n", | |
" for old_id, new_id in id_map.items():\n", | |
" dependency_data_with_new_ids = dependency_data_with_new_ids.replace(old_id, new_id)\n", | |
" return json.loads(dependency_data_with_new_ids)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"async def from_dependency_data(dependency_data):\n", | |
" widgets = {}\n", | |
" for model_id, model in dependency_data.items():\n", | |
" widgets[model_id] = asyncio.ensure_future(restore_one_widget(model_id, model, widgets))\n", | |
" done, pending = await asyncio.wait(widgets.values())\n", | |
" done_keys = dict([f.result() for f in done])\n", | |
" return {wid: done_keys[wid] for wid in dependency_data}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"async def restore_one_trait(name, raw, widgets):\n", | |
" if isinstance(raw, (str,)) and raw.startswith(\"IPY_MODEL_\"):\n", | |
" done, pending = await asyncio.wait([widgets[raw[10:]]])\n", | |
" value = list(done)[0].result()[1]\n", | |
" return name, value\n", | |
" elif isinstance(raw, (dict,)):\n", | |
" done, pending = await asyncio.wait([\n", | |
" restore_one_trait(k, v, widgets) for k, v in raw.items()\n", | |
" ])\n", | |
" values = dict([f.result() for f in done])\n", | |
" elif isinstance(raw, (list, tuple)) and raw:\n", | |
" futures = [restore_one_trait(i, v, widgets) for i, v in enumerate(raw)]\n", | |
" done, pending = await asyncio.wait(futures)\n", | |
" value = [f.result()[1] for f in done]\n", | |
" else:\n", | |
" value = raw\n", | |
" return name, value" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"async def restore_one_widget(model_id, model, widgets):\n", | |
" state = model[\"state\"]\n", | |
" # todo: replace with our custom stuff\n", | |
" widget_class = W.Widget.widget_types.get(\n", | |
" state['_model_module'],\n", | |
" state['_model_module_version'],\n", | |
" state['_model_name'],\n", | |
" state['_view_module'],\n", | |
" state['_view_module_version'],\n", | |
" state['_view_name']\n", | |
" )\n", | |
" trait_futures = {}\n", | |
" for name, raw in model[\"state\"].items():\n", | |
" if name.startswith(\"_model_\") or name.startswith(\"_view_\"):\n", | |
" continue\n", | |
" trait_futures[name] = restore_one_trait(name, raw, widgets)\n", | |
"\n", | |
" kwargs = {}\n", | |
" if trait_futures:\n", | |
" done, pending = await asyncio.wait(trait_futures.values())\n", | |
" kwargs = dict([f.result() for f in done])\n", | |
" \n", | |
" widget = widget_class(model_id=model_id, **kwargs)\n", | |
" return model_id, widget" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"if __name__ == \"__main__\":\n", | |
" box = W.HBox([W.FloatSlider()])\n", | |
" box.children[0].value = 42\n", | |
" display(box)\n", | |
"\n", | |
" template = replace_widget_ids(W.embed.dependency_state(box, drop_defaults=False)) \n", | |
" widgets = await from_dependency_data(template)\n", | |
" display(list(widgets.values())[0])" | |
] | |
} | |
], | |
"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