##// END OF EJS Templates
Updated example notebook
Sylvain Corlay -
Show More
@@ -221,6 +221,150
221 "outputs": []
221 "outputs": []
222 },
222 },
223 {
223 {
224 "cell_type": "heading",
225 "level": 1,
226 "metadata": {},
227 "source": [
228 "Linking Widgets"
229 ]
230 },
231 {
232 "cell_type": "markdown",
233 "metadata": {},
234 "source": [
235 "Often, you may want to simply link widget attributes together. Synchronization of attributes can be done in a simpler way than by using bare traitlets events. \n",
236 "\n",
237 "The first method is to use the `link` and `directional_link` functions from the `traitlets` module. "
238 ]
239 },
240 {
241 "cell_type": "heading",
242 "level": 2,
243 "metadata": {},
244 "source": [
245 "Linking traitlets attributes from the server side"
246 ]
247 },
248 {
249 "cell_type": "code",
250 "collapsed": false,
251 "input": [
252 "from IPython.utils import traitlets"
253 ],
254 "language": "python",
255 "metadata": {},
256 "outputs": []
257 },
258 {
259 "cell_type": "code",
260 "collapsed": false,
261 "input": [
262 "caption = widgets.Latex(value = 'The values of slider1, slider2 and slider3 are synchronized')\n",
263 "sliders1, slider2, slider3 = widgets.IntSlider(description='Slider 1'),\\\n",
264 " widgets.IntSlider(description='Slider 2'),\\\n",
265 " widgets.IntSlider(description='Slider 3')\n",
266 "l = traitlets.link((sliders1, 'value'), (slider2, 'value'), (slider3, 'value'))\n",
267 "display(caption, sliders1, slider2, slider3)"
268 ],
269 "language": "python",
270 "metadata": {},
271 "outputs": []
272 },
273 {
274 "cell_type": "code",
275 "collapsed": false,
276 "input": [
277 "caption = widgets.Latex(value = 'Changes in source values are reflected in target1 and target2')\n",
278 "source, target1, target2 = widgets.IntSlider(description='Source'),\\\n",
279 " widgets.IntSlider(description='Target 1'),\\\n",
280 " widgets.IntSlider(description='Target 2')\n",
281 "traitlets.dlink((source, 'value'), (target1, 'value'), (target2, 'value'))\n",
282 "display(caption, source, target1, target2)"
283 ],
284 "language": "python",
285 "metadata": {},
286 "outputs": []
287 },
288 {
289 "cell_type": "markdown",
290 "metadata": {},
291 "source": [
292 "Function `traitlets.link` returns a `Link` object. The link can be broken by calling the `unlink` method."
293 ]
294 },
295 {
296 "cell_type": "code",
297 "collapsed": false,
298 "input": [
299 "# l.unlink()"
300 ],
301 "language": "python",
302 "metadata": {},
303 "outputs": []
304 },
305 {
306 "cell_type": "heading",
307 "level": 2,
308 "metadata": {},
309 "source": [
310 "Linking widgets attributes from the client side"
311 ]
312 },
313 {
314 "cell_type": "markdown",
315 "metadata": {},
316 "source": [
317 "When synchronizing traitlets attributes, you may experience a lag because of the latency dues to the rountrip to the server side. You can also directly link widgets attributes, either in a unidirectional or a bidirectional fashion using the link widgets. "
318 ]
319 },
320 {
321 "cell_type": "code",
322 "collapsed": false,
323 "input": [
324 "caption = widgets.Latex(value = 'The values of range1, range2 and range3 are synchronized')\n",
325 "range1, range2, range3 = widgets.IntSlider(description='Range 1'),\\\n",
326 " widgets.IntSlider(description='Range 2'),\\\n",
327 " widgets.IntSlider(description='Range 3')\n",
328 "l = widgets.link((range1, 'value'), (range2, 'value'), (range3, 'value'))\n",
329 "display(caption, range1, range2, range3)"
330 ],
331 "language": "python",
332 "metadata": {},
333 "outputs": []
334 },
335 {
336 "cell_type": "code",
337 "collapsed": false,
338 "input": [
339 "caption = widgets.Latex(value = 'Changes in source_range values are reflected in target_range1 and target_range2')\n",
340 "source_range, target_range1, target_range2 = widgets.IntSlider(description='Source range'),\\\n",
341 " widgets.IntSlider(description='Target range 1'),\\\n",
342 " widgets.IntSlider(description='Target range 2')\n",
343 "widgets.dlink((source_range, 'value'), (target_range1, 'value'), (target_range2, 'value'))\n",
344 "display(caption, source_range, target_range1, target_range2)"
345 ],
346 "language": "python",
347 "metadata": {},
348 "outputs": []
349 },
350 {
351 "cell_type": "markdown",
352 "metadata": {},
353 "source": [
354 "Function `widgets.link` returns a `Link` widget. The link can be broken by calling the `close` method."
355 ]
356 },
357 {
358 "cell_type": "code",
359 "collapsed": false,
360 "input": [
361 "# l.close()"
362 ],
363 "language": "python",
364 "metadata": {},
365 "outputs": []
366 },
367 {
224 "cell_type": "markdown",
368 "cell_type": "markdown",
225 "metadata": {},
369 "metadata": {},
226 "source": [
370 "source": [
@@ -231,4 +375,4
231 "metadata": {}
375 "metadata": {}
232 }
376 }
233 ]
377 ]
234 } No newline at end of file
378 }
General Comments 0
You need to be logged in to leave comments. Login now