Widget Placement.ipynb
173 lines
| 4.8 KiB
| text/plain
|
TextLexer
Jonathan Frederic
|
r14323 | { | |
"metadata": { | |||
Jonathan Frederic
|
r14342 | "cell_tags": [ | |
[ | |||
"<None>", | |||
null | |||
] | |||
], | |||
Brian E. Granger
|
r16098 | "name": "", | |
"signature": "sha256:60ff1a42e4dbc911ae15b409033e993257425c982809950746a567a4419b9484" | |||
Jonathan Frederic
|
r14323 | }, | |
"nbformat": 3, | |||
"nbformat_minor": 0, | |||
"worksheets": [ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"from IPython.html import widgets # Widget definitions\n", | |||
Jonathan Frederic
|
r14342 | "from IPython.display import display # Used to display widgets in the notebook" | |
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r14342 | "outputs": [], | |
Jonathan Frederic
|
r14323 | "prompt_number": 1 | |
}, | |||
{ | |||
"cell_type": "heading", | |||
"level": 1, | |||
"metadata": {}, | |||
"source": [ | |||
"Parent/Child Relationships" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
MinRK
|
r14798 | "To display widget A inside widget B, widget A must be a child of widget B. Only one instance of any particular widget can be child of another. In other words, *widget A* cannot have *widget B* listed twice in it's list of children.\n", | |
Jonathan Frederic
|
r14323 | "\n", | |
MinRK
|
r14798 | "Widgets that can contain other widgets have a `children` attribute. This attribute can be set via a kwarg in the widget's constructor or after construction. Calling display on an object with children automatically displays those children, too." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
MinRK
|
r14798 | "float_range = widgets.FloatSliderWidget()\n", | |
Jonathan Frederic
|
r14834 | "string = widgets.TextWidget(value='hi')\n", | |
MinRK
|
r14798 | "container = widgets.ContainerWidget(children=[float_range, string])\n", | |
Jonathan Frederic
|
r14323 | "\n", | |
"display(container) # Displays the `container` and all of it's children." | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
Jonathan Frederic
|
r14720 | "prompt_number": 2 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
Jonathan Frederic
|
r14720 | "Children can also be added to parents after the parent has been displayed. The parent is responsible for rendering its children." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r14720 | "container = widgets.ContainerWidget()\n", | |
Jonathan Frederic
|
r14323 | "display(container)\n", | |
"\n", | |||
MinRK
|
r14798 | "int_range = widgets.IntSliderWidget()\n", | |
"container.children=[int_range]\n" | |||
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
Jonathan Frederic
|
r14720 | "prompt_number": 3 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
"cell_type": "heading", | |||
"level": 1, | |||
"metadata": {}, | |||
"source": [ | |||
"Visibility" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
MinRK
|
r14798 | "Sometimes it is necessary to hide or show widgets in place, without having to redisplay the widget.\n", | |
"The `visibility` property of widgets can be used to hide or show widgets that have already been displayed (as seen below)." | |||
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Jonathan Frederic
|
r14720 | "string = widgets.LatexWidget(value=\"Hello World!\")\n", | |
"display(string) " | |||
Jonathan Frederic
|
r14323 | ], | |
"language": "python", | |||
"metadata": {}, | |||
Jason Grout
|
r14505 | "outputs": [], | |
Jonathan Frederic
|
r14720 | "prompt_number": 4 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"string.visible=False" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
Jonathan Frederic
|
r14720 | "prompt_number": 5 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"string.visible=True" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
"outputs": [], | |||
Jonathan Frederic
|
r14720 | "prompt_number": 6 | |
Jonathan Frederic
|
r14323 | }, | |
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
MinRK
|
r14798 | "In the example below, a form is rendered, which conditionally displays widgets depending on the state of other widgets. Try toggling the student checkbox." | |
Jonathan Frederic
|
r14323 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
"form = widgets.ContainerWidget()\n", | |||
Jonathan Frederic
|
r14834 | "first = widgets.TextWidget(description=\"First Name:\")\n", | |
"last = widgets.TextWidget(description=\"Last Name:\")\n", | |||
Jonathan Frederic
|
r14323 | "\n", | |
Jonathan Frederic
|
r14834 | "student = widgets.CheckboxWidget(description=\"Student:\", value=False)\n", | |
Jason Grout
|
r14486 | "school_info = widgets.ContainerWidget(visible=False, children=[\n", | |
Jonathan Frederic
|
r14834 | " widgets.TextWidget(description=\"School:\"),\n", | |
Jonathan Frederic
|
r14720 | " widgets.IntTextWidget(description=\"Grade:\", min=0, max=12)\n", | |
Jason Grout
|
r14486 | " ])\n", | |
Jonathan Frederic
|
r14323 | "\n", | |
Jonathan Frederic
|
r14834 | "pet = widgets.TextWidget(description=\"Pet's Name:\")\n", | |
Jason Grout
|
r14486 | "form.children = [first, last, student, school_info, pet]\n", | |
Jonathan Frederic
|
r14323 | "display(form)\n", | |
"\n", | |||
"def on_student_toggle(name, value):\n", | |||
" if value:\n", | |||
" school_info.visible = True\n", | |||
" else:\n", | |||
" school_info.visible = False\n", | |||
"student.on_trait_change(on_student_toggle, 'value')\n" | |||
], | |||
"language": "python", | |||
"metadata": {}, | |||
Jonathan Frederic
|
r14720 | "outputs": [], | |
"prompt_number": 7 | |||
Jonathan Frederic
|
r14323 | } | |
], | |||
"metadata": {} | |||
} | |||
] | |||
} |