Skip to content

Instantly share code, notes, and snippets.

@bollwyvl
Created January 3, 2019 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bollwyvl/9113380c60353bdfa870cfe4ff5eb0b2 to your computer and use it in GitHub Desktop.
Save bollwyvl/9113380c60353bdfa870cfe4ff5eb0b2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from tornado.ioloop import IOLoop, PeriodicCallback\n",
"from tornado.httpclient import AsyncHTTPClient\n",
"from tornado import gen\n",
"from tornado.platform.asyncio import to_tornado_future\n",
"from asyncio import sleep\n",
"import time\n",
"from ipywidgets import FloatSlider, Checkbox, HBox"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"IOLoop has all the goodies."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"loop = IOLoop.instance()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you name your functions, and then call them indirectly, you can hack on them while it's running."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def increment(*args, **kwargs):\n",
" if running.value:\n",
" x.value += 3\n",
" loop.call_later(0.5, increment)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`stdout` doesn't work so great, but widgets are fine."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0f8537501199496895791ac4f02f7db6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(Checkbox(value=False, description='running'), FloatSlider(value=0.0)))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"running = Checkbox(description=\"running\")\n",
"x = FloatSlider()\n",
"running.observe(lambda _: running.value and loop.add_callback(lambda: increment()), \"value\")\n",
"\n",
"HBox([running, x])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also set up a more regular clock with `PeriodicCallbac`"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def tick():\n",
" if running.value:\n",
" x.value -= 0.5"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"cb = PeriodicCallback(lambda: tick(), 1000)\n",
"cb.start()"
]
}
],
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment