Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
":0: FutureWarning: IPython widgets are experimental and may change in the future.\n"
]
}
],
"source": [
"from IPython.html.widgets import FloatText, FloatSlider, Button\n",
"from IPython.display import display\n",
"from IPython.utils.traitlets import link\n",
"\n",
"widgets = []\n",
"for x in list('abcdef'):\n",
" widgets.append(FloatText(description=x))\n",
"\n",
"a = FloatSlider()\n",
"b = FloatSlider()\n",
"\n",
"link((a, 'value'), (b, 'value'))\n",
"\n",
"threads = []\n",
"def go(sender):\n",
" def thread_code():\n",
" from time import sleep\n",
" from random import random\n",
" while len(threads) > 0:\n",
" for w in widgets:\n",
" w.value = random()\n",
" sleep(0.01)\n",
" from threading import Thread\n",
" thread = Thread(target=thread_code)\n",
" threads.append(thread)\n",
" thread.start()\n",
"go_btn = Button(description=\"Go!\")\n",
"go_btn.on_click(go)\n",
"\n",
"def stop(sender):\n",
" del threads[:]\n",
"stop_btn = Button(description=\"Stop\")\n",
"stop_btn.on_click(stop)\n",
"\n",
"display(*widgets)\n",
"display(a,b)\n",
"display(go_btn)\n",
"display(stop_btn)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.