Created
January 3, 2019 22:22
Revisions
-
bollwyvl created this gist
Jan 3, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,138 @@ { "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 }