| 1 |
Migrating Widgets to IPython 3 |
| 2 |
============================== |
| 3 |
|
| 4 |
Upgrading Notebooks |
| 5 |
------------------- |
| 6 |
|
| 7 |
1. The first thing you'll notice when upgrading an IPython 2.0 widget |
| 8 |
notebook to IPython 3.0 is the "Notebook converted" dialog. Click |
| 9 |
"ok". |
| 10 |
2. All of the widgets distributed with IPython have been renamed. The |
| 11 |
"Widget" suffix was removed from the end of the class name. i.e. |
| 12 |
``ButtonWidget`` is now ``Button``. |
| 13 |
3. ``ContainerWidget`` was renamed to ``Box``. |
| 14 |
4. ``PopupWidget`` was removed from IPython, because bootstrapjs was |
| 15 |
problematic (creates global variables, etc.). If you use the |
| 16 |
``PopupWidget``, try using a ``Box`` widget instead. If your notebook |
| 17 |
can't live without the popup functionality, subclass the ``Box`` |
| 18 |
widget (both in Python and JS) and use JQuery UI's ``draggable()`` |
| 19 |
and ``resizable()`` methods to mimic the behavior. |
| 20 |
5. ``add_class`` and ``remove_class`` were removed. More often than not |
| 21 |
a new attribute exists on the widget that allows you to achieve the |
| 22 |
same explicitly. i.e. the ``Button`` widget now has a |
| 23 |
``button_style`` attribute which you can set to 'primary', 'success', |
| 24 |
'info', 'warning', 'danger', or '' instead of using ``add_class`` to |
| 25 |
add the bootstrap class. ``VBox`` and ``HBox`` classes (flexible |
| 26 |
``Box`` subclasses) were added that allow you to avoid using |
| 27 |
``add_class`` and ``remove_class`` to make flexible box model |
| 28 |
layouts. As a last resort, if you can't find a built in attribute for |
| 29 |
the class you want to use, a new ``_dom_classes`` list trait was |
| 30 |
added, which combines ``add_class`` and ``remove_class`` into one |
| 31 |
stateful list. |
| 32 |
6. ``set_css`` and ``get_css`` were removed in favor of explicit style |
| 33 |
attributes - ``visible``, ``width``, ``height``, ``padding``, |
| 34 |
``margin``, ``color``, ``background_color``, ``border_color``, |
| 35 |
``border_width``, ``border_radius``, ``border_style``, |
| 36 |
``font_style``, ``font_weight``, ``font_size``, and ``font_family`` |
| 37 |
are a few. If you can't find a trait to see the css attribute you |
| 38 |
need, you can, in order of preference, (A) subclass to create your |
| 39 |
own custom widget, (B) use CSS and the ``_dom_classes`` trait to set |
| 40 |
``_dom_classes``, or (C) use the ``_css`` dictionary to set CSS |
| 41 |
styling like ``set_css`` and ``get_css``. |
| 42 |
7. For selection widgets, such as ``Dropdown``, the ``values`` argument |
| 43 |
has been renamed to ``options``. |
| 44 |
|
| 45 |
Upgrading Custom Widgets |
| 46 |
------------------------ |
| 47 |
|
| 48 |
Javascript |
| 49 |
~~~~~~~~~~ |
| 50 |
|
| 51 |
1. If you are distributing your widget and decide to use the deferred |
| 52 |
loading technique (preferred), you can remove all references to the |
| 53 |
WidgetManager and the register model/view calls (see the Python |
| 54 |
section below for more information). |
| 55 |
2. In 2.0 require.js was used incorrectly, that has been fixed and now |
| 56 |
loading works more like Python's import. Requiring |
| 57 |
``widgets/js/widget`` doesn't import the ``WidgetManager`` class, |
| 58 |
instead it imports a dictionary that exposes the classes within that |
| 59 |
module: |
| 60 |
|
| 61 |
.. code:: javascript |
| 62 |
|
| 63 |
{ |
| 64 |
'WidgetModel': WidgetModel, |
| 65 |
'WidgetView': WidgetView, |
| 66 |
'DOMWidgetView': DOMWidgetView, |
| 67 |
'ViewList': ViewList, |
| 68 |
} |
| 69 |
|
| 70 |
If you decide to continue to use the widget registry (by registering |
| 71 |
your widgets with the manager), you can import a dictionary with a |
| 72 |
handle to the WidgetManager class by requiring |
| 73 |
``widgets/js/manager``. Doing so will import: |
| 74 |
|
| 75 |
.. code:: javascript |
| 76 |
|
| 77 |
{'WidgetManager': WidgetManager} |
| 78 |
|
| 79 |
3. Don't rely on the ``IPython`` namespace for anything. To inherit from |
| 80 |
the DOMWidgetView, WidgetView, or WidgetModel, require |
| 81 |
``widgets/js/widget`` as ``widget``. If you were inheriting from |
| 82 |
DOMWidgetView, and the code looked like this: |
| 83 |
|
| 84 |
.. code:: javascript |
| 85 |
|
| 86 |
IPython.DOMWidgetView.extend({...}) |
| 87 |
|
| 88 |
It would become this: |
| 89 |
|
| 90 |
.. code:: javascript |
| 91 |
|
| 92 |
widget.DOMWidgetView.extend({...}) |
| 93 |
|
| 94 |
4. Custom models are encouraged. When possible, it's recommended to move |
| 95 |
your code into a custom model, so actions are performed 1 time, |
| 96 |
instead of N times where N is the number of displayed views. |
| 97 |
|
| 98 |
Python |
| 99 |
~~~~~~ |
| 100 |
|
| 101 |
Generally, custom widget Python code can remain unchanged. If you |
| 102 |
distribute your custom widget, you may be using ``display`` and |
| 103 |
``Javascript`` to publish the widget's Javascript to the front-end. That |
| 104 |
is no longer the recommended way of distributing widget Javascript. |
| 105 |
Instead have the user install the Javascript to his/her nbextension |
| 106 |
directory or their profile's static directory. Then use the new |
| 107 |
``_view_module`` and ``_model_module`` traitlets in combination with |
| 108 |
``_view_name`` and ``_model_name`` to instruct require.js on how to load |
| 109 |
the widget's Javascript. The Javascript is then loaded when the widget |
| 110 |
is used for the first time. |
| 111 |
|
| 112 |
Details |
| 113 |
------- |
| 114 |
|
| 115 |
Asynchronous |
| 116 |
~~~~~~~~~~~~ |
| 117 |
|
| 118 |
In the IPython 2.x series the only way to register custom widget views |
| 119 |
and models was to use the registry in the widget manager. Unfortunately, |
| 120 |
using this method made distributing and running custom widgets difficult. The widget |
| 121 |
maintainer had to either use the rich display framework to push the |
| 122 |
widget's Javascript to the notebook or instruct the users to install the |
| 123 |
Javascript by hand in a custom profile. With the first method, the |
| 124 |
maintainer would have to be careful about when the Javascript was pushed |
| 125 |
to the front-end. If the Javascript was pushed on Python widget |
| 126 |
``import``, the widgets wouldn't work after page refresh. This is |
| 127 |
because refreshing the page does not restart the kernel, and the Python |
| 128 |
``import`` statement only runs once in a given kernel instance (unless |
| 129 |
you reload the Python modules, which isn't straight forward). This meant |
| 130 |
the maintainer would have to have a separate ``push_js()`` method that |
| 131 |
the user would have to call after importing the widget's Python code. |
| 132 |
|
| 133 |
Our solution was to add support for loading widget views and models |
| 134 |
using require.js paths. Thus the comm and widget frameworks now support |
| 135 |
lazy loading. To do so, everything had to be converted to asynchronous |
| 136 |
code. HTML5 promises are used to accomplish that |
| 137 |
(`#6818 <https://github.com/ipython/ipython/pull/6818>`__, |
| 138 |
`#6914 <https://github.com/ipython/ipython/pull/6914>`__). |
| 139 |
|
| 140 |
Symmetry |
| 141 |
~~~~~~~~ |
| 142 |
|
| 143 |
In IPython 3.0, widgets can be instantiated from the front-end |
| 144 |
(`#6664 <https://github.com/ipython/ipython/pull/6664>`__). On top of |
| 145 |
this, a widget persistence API was added |
| 146 |
(`#7163 <https://github.com/ipython/ipython/pull/7163>`__, |
| 147 |
`#7227 <https://github.com/ipython/ipython/pull/7227>`__). With the |
| 148 |
widget persistence API, you can persist your widget instances using |
| 149 |
Javascript. This makes it easy to persist your widgets to your notebook |
| 150 |
document (with a small amount of custom JS). By default, the widgets are |
| 151 |
persisted to your web browsers local storage which makes them reappear |
| 152 |
when your refresh the page. |
| 153 |
|
| 154 |
Smaller Changes |
| 155 |
~~~~~~~~~~~~~~~ |
| 156 |
|
| 157 |
- Latex math is supported in widget ``description``\ s |
| 158 |
(`#5937 <https://github.com/ipython/ipython/pull/5937>`__). |
| 159 |
- Widgets can be display more than once within a single container |
| 160 |
widget (`#5963 <https://github.com/ipython/ipython/pull/5963>`__, |
| 161 |
`#6990 <https://github.com/ipython/ipython/pull/6990>`__). |
| 162 |
- ``FloatRangeSlider`` and ``IntRangeSlider`` were added |
| 163 |
(`#6050 <https://github.com/ipython/ipython/pull/6050>`__). |
| 164 |
- "Widget" was removed from the ends of all of the widget class names |
| 165 |
(`#6125 <https://github.com/ipython/ipython/pull/6125>`__). |
| 166 |
- ``ContainerWidget`` was renamed to ``Box`` |
| 167 |
(`#6125 <https://github.com/ipython/ipython/pull/6125>`__). |
| 168 |
- ``HBox`` and ``VBox`` widgets were added |
| 169 |
(`#6125 <https://github.com/ipython/ipython/pull/6125>`__). |
| 170 |
- ``add\_class`` and ``remove\_class`` were removed in favor of a |
| 171 |
``_dom_classes`` list |
| 172 |
(`#6235 <https://github.com/ipython/ipython/pull/6235>`__). |
| 173 |
- ``get\_css`` and ``set\_css`` were removed in favor of explicit |
| 174 |
traits for widget styling |
| 175 |
(`#6235 <https://github.com/ipython/ipython/pull/6235>`__). |
| 176 |
- ``jslink`` and ``jsdlink`` were added |
| 177 |
(`#6454 <https://github.com/ipython/ipython/pull/6454>`__, |
| 178 |
`#7468 <https://github.com/ipython/ipython/pull/7468>`__). |
| 179 |
- An ``Output`` widget was added, which allows you to ``print`` and |
| 180 |
``display`` within widgets |
| 181 |
(`#6670 <https://github.com/ipython/ipython/pull/6670>`__). |
| 182 |
- ``PopupWidget`` was removed |
| 183 |
(`#7341 <https://github.com/ipython/ipython/pull/7341>`__). |
| 184 |
- A visual cue was added for widgets with 'dead' comms |
| 185 |
(`#7227 <https://github.com/ipython/ipython/pull/7227>`__). |
| 186 |
- A ``SelectMultiple`` widget was added (a ``Select`` widget that |
| 187 |
allows multiple things to be selected at once) |
| 188 |
(`#6890 <https://github.com/ipython/ipython/pull/6890>`__). |
| 189 |
- A class was added to help manage children views |
| 190 |
(`#6990 <https://github.com/ipython/ipython/pull/6990>`__). |
| 191 |
- A warning was added that shows on widget import because it's expected |
| 192 |
that the API will change again by IPython 4.0. This warning can be |
| 193 |
suppressed (`#7107 <https://github.com/ipython/ipython/pull/7107>`__, |
| 194 |
`#7200 <https://github.com/ipython/ipython/pull/7200>`__, |
| 195 |
`#7201 <https://github.com/ipython/ipython/pull/7201>`__, |
| 196 |
`#7204 <https://github.com/ipython/ipython/pull/7204>`__). |
| 197 |
|
| 198 |
Comm and Widget PR Index |
| 199 |
------------------------ |
| 200 |
|
| 201 |
Here is a chronological list of PRs affecting the widget and comm frameworks for IPython 3.0. Note that later PRs may revert changes |
| 202 |
made in earlier PRs: |
| 203 |
|
| 204 |
- Add placeholder attribute to text widgets |
| 205 |
`#5652 <https://github.com/ipython/ipython/pull/5652>`__ |
| 206 |
- Add latex support in widget labels, |
| 207 |
`#5937 <https://github.com/ipython/ipython/pull/5937>`__ |
| 208 |
- Allow widgets to display more than once within container widgets. |
| 209 |
`#5963 <https://github.com/ipython/ipython/pull/5963>`__ |
| 210 |
- use require.js, |
| 211 |
`#5980 <https://github.com/ipython/ipython/pull/5980>`__ |
| 212 |
- Range widgets |
| 213 |
`#6050 <https://github.com/ipython/ipython/pull/6050>`__ |
| 214 |
- Interact on\_demand option |
| 215 |
`#6051 <https://github.com/ipython/ipython/pull/6051>`__ |
| 216 |
- Allow text input on slider widgets |
| 217 |
`#6106 <https://github.com/ipython/ipython/pull/6106>`__ |
| 218 |
- support binary buffers in comm messages |
| 219 |
`#6110 <https://github.com/ipython/ipython/pull/6110>`__ |
| 220 |
- Embrace the flexible box model in the widgets |
| 221 |
`#6125 <https://github.com/ipython/ipython/pull/6125>`__ |
| 222 |
- Widget trait serialization |
| 223 |
`#6128 <https://github.com/ipython/ipython/pull/6128>`__ |
| 224 |
- Make Container widgets take children as the first positional |
| 225 |
argument `#6153 <https://github.com/ipython/ipython/pull/6153>`__ |
| 226 |
- once-displayed |
| 227 |
`#6168 <https://github.com/ipython/ipython/pull/6168>`__ |
| 228 |
- Validate slider value, when limits change |
| 229 |
`#6171 <https://github.com/ipython/ipython/pull/6171>`__ |
| 230 |
- Unregistering comms in Comm Manager |
| 231 |
`#6216 <https://github.com/ipython/ipython/pull/6216>`__ |
| 232 |
- Add EventfulList and EventfulDict trait types. |
| 233 |
`#6228 <https://github.com/ipython/ipython/pull/6228>`__ |
| 234 |
- Remove add/remove\_class and set/get\_css. |
| 235 |
`#6235 <https://github.com/ipython/ipython/pull/6235>`__ |
| 236 |
- avoid unregistering widget model twice |
| 237 |
`#6250 <https://github.com/ipython/ipython/pull/6250>`__ |
| 238 |
- Widget property lock should compare json states, not python states |
| 239 |
`#6332 <https://github.com/ipython/ipython/pull/6332>`__ |
| 240 |
- Strip the IPY\_MODEL\_ prefix from widget IDs before referencing |
| 241 |
them. `#6377 <https://github.com/ipython/ipython/pull/6377>`__ |
| 242 |
- "event" is not defined error in Firefox |
| 243 |
`#6437 <https://github.com/ipython/ipython/pull/6437>`__ |
| 244 |
- Javascript link |
| 245 |
`#6454 <https://github.com/ipython/ipython/pull/6454>`__ |
| 246 |
- Bulk update of widget attributes |
| 247 |
`#6463 <https://github.com/ipython/ipython/pull/6463>`__ |
| 248 |
- Creating a widget registry on the Python side. |
| 249 |
`#6493 <https://github.com/ipython/ipython/pull/6493>`__ |
| 250 |
- Allow widget views to be loaded from require modules |
| 251 |
`#6494 <https://github.com/ipython/ipython/pull/6494>`__ |
| 252 |
- Fix Issue #6530 |
| 253 |
`#6532 <https://github.com/ipython/ipython/pull/6532>`__ |
| 254 |
- Make comm manager (mostly) independent of InteractiveShell |
| 255 |
`#6540 <https://github.com/ipython/ipython/pull/6540>`__ |
| 256 |
- Add semantic classes to top-level containers for single widgets |
| 257 |
`#6609 <https://github.com/ipython/ipython/pull/6609>`__ |
| 258 |
- Selection Widgets: forcing 'value' to be in 'values' |
| 259 |
`#6617 <https://github.com/ipython/ipython/pull/6617>`__ |
| 260 |
- Allow widgets to be constructed from Javascript |
| 261 |
`#6664 <https://github.com/ipython/ipython/pull/6664>`__ |
| 262 |
- Output widget |
| 263 |
`#6670 <https://github.com/ipython/ipython/pull/6670>`__ |
| 264 |
- Minor change in widgets.less to fix alignment issue |
| 265 |
`#6681 <https://github.com/ipython/ipython/pull/6681>`__ |
| 266 |
- Make Selection widgets respect values order. |
| 267 |
`#6747 <https://github.com/ipython/ipython/pull/6747>`__ |
| 268 |
- Widget persistence API |
| 269 |
`#6789 <https://github.com/ipython/ipython/pull/6789>`__ |
| 270 |
- Add promises to the widget framework. |
| 271 |
`#6818 <https://github.com/ipython/ipython/pull/6818>`__ |
| 272 |
- SelectMultiple widget |
| 273 |
`#6890 <https://github.com/ipython/ipython/pull/6890>`__ |
| 274 |
- Tooltip on toggle button |
| 275 |
`#6923 <https://github.com/ipython/ipython/pull/6923>`__ |
| 276 |
- Allow empty text box \*while typing\* for numeric widgets |
| 277 |
`#6943 <https://github.com/ipython/ipython/pull/6943>`__ |
| 278 |
- Ignore failure of widget MathJax typesetting |
| 279 |
`#6948 <https://github.com/ipython/ipython/pull/6948>`__ |
| 280 |
- Refactor the do\_diff and manual child view lists into a separate |
| 281 |
ViewList object |
| 282 |
`#6990 <https://github.com/ipython/ipython/pull/6990>`__ |
| 283 |
- Add warning to widget namespace import. |
| 284 |
`#7107 <https://github.com/ipython/ipython/pull/7107>`__ |
| 285 |
- lazy load widgets |
| 286 |
`#7120 <https://github.com/ipython/ipython/pull/7120>`__ |
| 287 |
- Fix padding of widgets. |
| 288 |
`#7139 <https://github.com/ipython/ipython/pull/7139>`__ |
| 289 |
- Persist widgets across page refresh |
| 290 |
`#7163 <https://github.com/ipython/ipython/pull/7163>`__ |
| 291 |
- Make the widget experimental error a real python warning |
| 292 |
`#7200 <https://github.com/ipython/ipython/pull/7200>`__ |
| 293 |
- Make the widget error message shorter and more understandable. |
| 294 |
`#7201 <https://github.com/ipython/ipython/pull/7201>`__ |
| 295 |
- Make the widget warning brief and easy to filter |
| 296 |
`#7204 <https://github.com/ipython/ipython/pull/7204>`__ |
| 297 |
- Add visual cue for widgets with dead comms |
| 298 |
`#7227 <https://github.com/ipython/ipython/pull/7227>`__ |
| 299 |
- Widget values as positional arguments |
| 300 |
`#7260 <https://github.com/ipython/ipython/pull/7260>`__ |
| 301 |
- Remove the popup widget |
| 302 |
`#7341 <https://github.com/ipython/ipython/pull/7341>`__ |
| 303 |
- document and validate link, dlink |
| 304 |
`#7468 <https://github.com/ipython/ipython/pull/7468>`__ |
| 305 |
- Document interact 5637 |
| 306 |
`#7525 <https://github.com/ipython/ipython/pull/7525>`__ |
| 307 |
- Update some broken examples of using widgets |
| 308 |
`#7547 <https://github.com/ipython/ipython/pull/7547>`__ |
| 309 |
- Use Output widget with Interact |
| 310 |
`#7554 <https://github.com/ipython/ipython/pull/7554>`__ |
| 311 |
- don't send empty execute\_result messages |
| 312 |
`#7560 <https://github.com/ipython/ipython/pull/7560>`__ |
| 313 |
- Validation on the python side |
| 314 |
`#7602 <https://github.com/ipython/ipython/pull/7602>`__ |
| 315 |
- only show prompt overlay if there's a prompt |
| 316 |
`#7661 <https://github.com/ipython/ipython/pull/7661>`__ |
| 317 |
- Allow predictate to be used for comparison in selection widgets |
| 318 |
`#7674 <https://github.com/ipython/ipython/pull/7674>`__ |
| 319 |
- Fix widget view persistence. |
| 320 |
`#7680 <https://github.com/ipython/ipython/pull/7680>`__ |
| 321 |
- Revert "Use Output widget with Interact" |
| 322 |
`#7703 <https://github.com/ipython/ipython/pull/7703>`__ |