Last active
October 20, 2022 22:09
-
-
Save posita/af9aac40be4a145b6c3e395ba60ac428 to your computer and use it in GitHub Desktop.
TODO
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, | |
"id": "f297af25-3380-47d2-9ee9-aef62a2a949a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"try:\n", | |
" import matplotlib\n", | |
"except ImportError:\n", | |
" import pip\n", | |
" pip.main([\"install\", \"matplotlib\"])\n", | |
"\n", | |
"%matplotlib inline\n", | |
"\n", | |
"# Copied from\n", | |
"# <https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html#Debouncing>\n", | |
"import asyncio\n", | |
"\n", | |
"class Timer:\n", | |
" def __init__(self, timeout, callback):\n", | |
" self._timeout = timeout\n", | |
" self._callback = callback\n", | |
"\n", | |
" async def _job(self):\n", | |
" await asyncio.sleep(self._timeout)\n", | |
" self._callback()\n", | |
"\n", | |
" def start(self):\n", | |
" self._task = asyncio.ensure_future(self._job())\n", | |
"\n", | |
" def cancel(self):\n", | |
" self._task.cancel()\n", | |
"\n", | |
"def debounce(wait):\n", | |
" \"\"\" Decorator that will postpone a function's\n", | |
" execution until after `wait` seconds\n", | |
" have elapsed since the last time it was invoked. \"\"\"\n", | |
" def decorator(fn):\n", | |
" timer = None\n", | |
" def debounced(*args, **kwargs):\n", | |
" nonlocal timer\n", | |
" def call_it():\n", | |
" fn(*args, **kwargs)\n", | |
" if timer is not None:\n", | |
" timer.cancel()\n", | |
" timer = Timer(wait, call_it)\n", | |
" timer.start()\n", | |
" return debounced\n", | |
" return decorator" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "f5307615-88e7-41c8-a97d-26d9f29f2ea5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "e851928f1f9d4f0196d0983f9da58355", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"interactive(children=(Output(),), _dom_classes=('widget-interact',))" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"import matplotlib.patheffects\n", | |
"import numpy\n", | |
"from ipywidgets import widgets\n", | |
"\n", | |
"@widgets.interact\n", | |
"# Uncomment this and restart the kernel. Plots will now appear at level \"info\" in the log console\n", | |
"@debounce(1)\n", | |
"def widget(lo=1, hi=10, scale=5):\n", | |
" display(f\"scaling to {scale}\")\n", | |
" data = {i: 2 ** i for i in numpy.arange(lo, hi, -0.1 if hi < lo else 0.1)}\n", | |
" matplotlib.rcParams.update(matplotlib.rcParamsDefault)\n", | |
"\n", | |
" with matplotlib.style.context(\"ggplot\"):\n", | |
" fig = matplotlib.pyplot.figure(\n", | |
" figsize=(scale, scale / 16 * 9),\n", | |
" )\n", | |
" xmin, ymin, dx, dy = 0, 0, 1, 1\n", | |
" ax = fig.add_axes((xmin, ymin, dx, dy))\n", | |
" xs, ys = zip(*data.items())\n", | |
" ax.plot(xs, ys, path_effects=[\n", | |
" matplotlib.patheffects.SimpleLineShadow(),\n", | |
" matplotlib.patheffects.Normal(),\n", | |
" ])\n", | |
" matplotlib.pyplot.show()\n", | |
"\n", | |
"widget()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.10.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment