SuperAPI AI Directory

Jupyter Widgets official documentation

The "Widget Basics" example page in the official Jupyter Widgets documentation, introducing the concepts and basic usage of controls in ipywidgets. Widgets are eventful Python objects that have a representation in the browser, often appearing as controls such as sliders and text boxes. They can be used to build interactive graphical interfaces for Notebooks, and can also synchronize stateful and stateless information between Python and JavaScript. The page covers importing ipywidgets, ways to display controls (automatic display and explicit display), keeping front-end views synchronized when the same control is displayed multiple times, closing controls, reading and setting control properties (such as value, keys, description, disabled), setting initial values in the constructor, and linking two control properties via link or jslink and being able to unlink them.

Open website

About this tool

This page is an example chapter in the official Jupyter Widgets documentation, with the topic "Simple Widget Introduction", used to explain the basic concepts and operation methods of controls in ipywidgets.

  1. Purpose Widgets are Python objects with event-handling capabilities, and have a representation in the browser, usually appearing as controls such as sliders and text boxes. They can be used to build interactive graphical user interfaces (GUIs) for Notebooks, and can also be used to synchronize stateful and stateless information between Python and JavaScript.

  2. Main Content

  3. Using controls: ipywidgets must first be imported. Widgets have their own display repr and can be displayed with the help of IPython's display framework. Constructing and returning an IntSlider will automatically display the control, and the control is displayed in the output area below the code cell; clearing the cell output will also remove the control. Controls can also be explicitly displayed using display(...).

  4. Multiple display() calls: If the same control is displayed twice, the instances displayed in the front end will remain synchronized with each other. The documentation suggests dragging the lower slider and observing the changes in the upper slider. The reason is that the control is represented by a single object on the backend, and each time the control is displayed, the front end creates a new representation for that same object, and this representation is called a view.

  5. Closing controls: A control can be closed by calling its close() method.

  6. Control properties: All IPython controls share a similar naming scheme. Reading a control value can be done by querying its value property; setting a control value can be done by setting its value property. In addition to value, most controls also share keys, description, and disabled. To view the list of all synchronized, stateful properties of a specific control, query its keys property.

  7. Shorthand for setting initial values of control properties: When creating a control, some or all initial values can be defined as keyword arguments in the control constructor.

  8. Linking two similar controls: If the same value needs to be displayed in two different ways, two different controls must be used. In this case, there is no need to manually synchronize the values of the two controls; the link or jslink function can be used to link two properties together (the difference between the two is discussed in Widget Events). The documentation example links the values of two controls inTogether.

  9. Unlinking: Unlinking controls is very simple; you just need to call .unlink on the link object. The documentation suggests that after unlinking, you try changing one of the controls to observe that they can change independently.

III. Applicable scenarios Suitable for scenarios such as creating interactive interfaces in Jupyter Notebook, displaying the same state in multiple views, and synchronizing state between Python and JavaScript.

IV. Limitations The page only provides a basic introduction; the difference between link and jslink is pointed to the Widget Events page; the entry titled "Try Jupyter Widgets" and the specific interactive examples mentioned on the page (such as dragging a slider to observe synchronization, and changing independently after unlinking) need to be experienced by running in an actual Notebook environment. The page does not provide installation commands, fees, prices, platform support scope, or additional information beyond the specific usage steps.