##// END OF EJS Templates
Fix the python side of the add/remove class functions to send custom messages
Fix the python side of the add/remove class functions to send custom messages

File last commit:

r14486:66c82094
r14504:a5d454b3
Show More
Part 1 - Basics.ipynb
389 lines | 11.4 KiB | text/plain | TextLexer
/ examples / widgets / Part 1 - Basics.ipynb
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 {
"metadata": {
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342 "cell_tags": [
[
"<None>",
null
]
],
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342 "To use IPython widgets in the notebook, the widget namespace and display function need to be imported."
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.html import widgets # Widget definitions\n",
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342 "from IPython.display import display # Used to display widgets in the notebook"
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ],
"language": "python",
"metadata": {},
Jonathan Frederic
Remove init_widget_js, use require.js for everything...
r14342 "outputs": [],
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "prompt_number": 1
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Basic Widgets"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The IPython notebook comes preloaded with basic widgets that represent common data types. These widgets are\n",
"\n",
"- BoolWidget : boolean \n",
"- FloatRangeWidget : bounded float \n",
"- FloatWidget : unbounded float \n",
Jonathan Frederic
Add ImageWidget
r14449 "- ImageWidget : image\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "- IntRangeWidget : bounded integer \n",
"- IntWidget : unbounded integer \n",
"- SelectionWidget : enumeration \n",
"- StringWidget : string \n",
"\n",
"A few special widgets are also included, that can be used to capture events and change how other widgets are displayed. These widgets are\n",
"\n",
"- ButtonWidget \n",
"- ContainerWidget \n",
"- MulticontainerWidget \n",
"\n",
"To see the complete list of widgets, one can execute the following"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[widget for widget in dir(widgets) if widget.endswith('Widget')]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"['BoolWidget',\n",
" 'ButtonWidget',\n",
" 'ContainerWidget',\n",
" 'FloatRangeWidget',\n",
" 'FloatWidget',\n",
Jonathan Frederic
Add ImageWidget
r14449 " 'ImageWidget',\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 " 'IntRangeWidget',\n",
" 'IntWidget',\n",
" 'MulticontainerWidget',\n",
" 'SelectionWidget',\n",
" 'StringWidget',\n",
" 'Widget']"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The basic widgets can all be constructed without arguments. The following creates a FloatRangeWidget without displaying it"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mywidget = widgets.FloatRangeWidget()"
],
"language": "python",
"metadata": {},
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['visible', '_css', '_children_attr', '_children_lists_attr', 'default_view_name', 'value', 'step', 'max', 'min', 'disabled', 'orientation', 'description']\n",
"[]\n",
"[]\n"
]
}
],
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Constructing a widget does not display it on the page. To display a widget, the widget must be passed to the IPython `display(object)` method. `mywidget` is displayed by"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"display(mywidget)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "cell_type": "code",
"collapsed": false,
"input": [
"mywidget.value"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"55.1"
]
}
],
"prompt_number": 13
},
{
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "cell_type": "markdown",
"metadata": {},
"source": [
"It's important to realize that widgets are not the same as output, even though they are displayed with `display`. Widgets are drawn in a special widget area. That area is marked with a close button which allows you to collapse the widgets. Widgets cannot be interleaved with output. Doing so would break the ability to make simple animations using `clear_output`.\n",
"\n",
"Widgets are manipulated via special instance properties (traitlets). The names of these instance properties are listed in the widget's `keys` property (as seen below). A few of these properties are common to most, if not all, widgets. The common properties are `value`, `description`, `visible`, and `disabled`. `_css`, `_add_class`, and `_remove_class` are internal properties that exist in all widgets and should not be modified."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mywidget.keys"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "prompt_number": 11,
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "text": [
"['visible',\n",
" '_css',\n",
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 " '_children_attr',\n",
" '_children_lists_attr',\n",
" 'default_view_name',\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 " 'value',\n",
" 'step',\n",
" 'max',\n",
" 'min',\n",
" 'disabled',\n",
" 'orientation',\n",
" 'description']"
]
}
],
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "prompt_number": 11
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Changing a widget's property value will automatically update that widget everywhere it is displayed in the notebook. Here the value of `mywidget` is set. The slider shown above (after input 4) updates automatically to the new value. In reverse, changing the value of the displayed widget will update the property's value."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mywidget.value = 25.0"
],
"language": "python",
"metadata": {},
"outputs": [],
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "prompt_number": 12
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After changing the widget's value in the notebook by hand to 0.0 (sliding the bar to the far left)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mywidget.value"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "prompt_number": 30,
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "text": [
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "25.0"
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 ]
}
],
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "prompt_number": 30
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Widget property values can also be set with kwargs during the construction of the widget (as seen below)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mysecondwidget = widgets.SelectionWidget(values=[\"Item A\", \"Item B\", \"Item C\"], value=\"Nothing Selected\")\n",
"display(mysecondwidget)"
],
"language": "python",
"metadata": {},
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['visible', '_css', '_children_attr', '_children_lists_attr', 'default_view_name', 'value', 'values', 'disabled', 'description']\n",
"[]\n",
"[]\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mysecondwidget.value"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 15,
"text": [
"u'Item C'"
]
}
],
"prompt_number": 15
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Views"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The data types that most of the widgets represent can be displayed more than one way. A `view` is a visual representation of a widget in the notebook. In the example in the section above, the default `view` for the `FloatRangeWidget` is used. The default view is set in the widgets `default_view_name` instance property (as seen below)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mywidget.default_view_name"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "prompt_number": 16,
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "text": [
"u'FloatSliderView'"
]
}
],
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "prompt_number": 16
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When a widget is displayed using `display(...)`, the `default_view_name` is used to determine what view type should be used to display the widget. View names are case sensitive. Sometimes the default view isn't the best view to represent a piece of data. To change what view is used, either the `default_view_name` can be changed or the `view_name` kwarg of `display` can be set. This also can be used to display one widget multiple ways in one output (as seen below)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"display(mywidget)\n",
"display(mywidget, view_name=\"FloatTextView\")"
],
"language": "python",
"metadata": {},
"outputs": [],
Jason Grout
Intermediate changes to javascript side of backbone widgets
r14486 "prompt_number": 17
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Some views work with multiple different widget types and some views only work with one. The complete list of views and supported widgets is below. The default views are italicized.\n",
"\n",
"| Widget Name | View Names |\n",
"|:-----------------------|:--------------------|\n",
"| BoolWidget | *CheckboxView* |\n",
"| | ToggleButtonView |\n",
"| ButtonWidget | *ButtonView* |\n",
"| ContainerWidget | *ContainerView* |\n",
Jonathan Frederic
Updated View list in example notebook (+ModalView)
r14423 "| | ModalView |\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "| FloatRangeWidget | *FloatSliderView* |\n",
"| | FloatTextView |\n",
"| | ProgressView |\n",
"| FloatWidget | *FloatTextView* |\n",
Jonathan Frederic
Add ImageWidget
r14449 "| ImageWidget | *ImageView* |\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "| IntRangeWidget | *IntSliderView* |\n",
"| | IntTextView |\n",
"| | ProgressView |\n",
"| IntWidget | *IntTextView* |\n",
"| MulticontainerWidget | AccordionView |\n",
"| | *TabView* |\n",
"| SelectionWidget | ToggleButtonsView |\n",
"| | RadioButtonsView |\n",
"| | *DropdownView* |\n",
Jonathan Frederic
Added ListBoxView
r14375 "| | ListBoxView |\n",
Jonathan Frederic
s/LabelView/HTMLView
r14446 "| StringWidget | HTMLView |\n",
Jonathan Frederic
Added LatexView
r14447 "| | LatexView |\n",
Jonathan Frederic
Uploaded widget tutorial (example) notebooks.
r14323 "| | TextAreaView |\n",
"| | *TextBoxView* |\n"
]
}
],
"metadata": {}
}
]
}