Skip to content

Instantly share code, notes, and snippets.

@jdfreder
Created February 6, 2015 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdfreder/7051ca96984863d13dd7 to your computer and use it in GitHub Desktop.
Save jdfreder/7051ca96984863d13dd7 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": {
"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