Created
May 20, 2014 18:11
-
-
Save ellisonbg/95ec49e7c90bb374a8a7 to your computer and use it in GitHub Desktop.
Dual Sliders
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
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:84c7300250d364448fdadb45622e6d0f3de5ce63e3a14f32ff83db494ef9e7ca" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This notebook demonstrates how to create two sliders and constrain one to have a value less than or equal to the other." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from IPython.html import widgets" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Create two sliders by hande and use `on_trait_change` to register callbacks that will be called when either value is changed. These callbacks simple set the `value` of the other slider to the new value." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"s1 = widgets.FloatSliderWidget(min=0.0,max=10.0,step=0.1,value=0.0)\n", | |
"s2 = widgets.FloatSliderWidget(min=0.0,max=10.0,step=0.1,value=5.0)\n", | |
"\n", | |
"def s1_changed(name, new):\n", | |
" if new > s2.value:\n", | |
" s2.value = new\n", | |
"\n", | |
"def s2_changed(name, new):\n", | |
" if new < s1.value:\n", | |
" s1.value = new\n", | |
" \n", | |
"s1.on_trait_change(s1_changed, 'value')\n", | |
"s2.on_trait_change(s2_changed, 'value')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def printer(x, y):\n", | |
" print x, y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"These slider instances can simply be passed to `interact`:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"widgets.interact(printer, x=s1, y=s2);" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"3.2 6.1\n" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment