ipywidgets Documentation
repository·main·Indexed 25 days ago
https://github.com/jupyter-widgets/ipywidgetsipywidgets (also known as jupyter-widgets) provides interactive HTML widgets for Jupyter notebooks and the IPython kernel, allowing users to control data and visualize changes interactively. The library includes core widgets such as sliders, progress bars, text boxes, toggle buttons, checkboxes, and display areas. It supports integration with JupyterLab, Classic Notebook, and standalone web applications via packages like @jupyter-widgets/controls, @jupyter-widgets/schema, and @jupyter-widgets/html-manager.
What's inside ipywidgets
- ipywidgets (also known as jupyter-widgets or simply widgets) provides interactive HTML widgets for Jupyter notebooks and the IPython kernel. It is a framework for creating interactive user interfaces within a notebook environment.
Overview of @jupyter-widgets packages
mainThe
@jupyter-widgetsecosystem consists of several specialized packages for managing and rendering Jupyter widgets in different environments. Key packages include:@jupyter-widgets/base: Core functionality for widget management.@jupyter-widgets/base-manager: Base manager for widget lifecycle and communication.@jupyter-widgets/controls: Implementation of standard interactive UI controls.@jupyter-widgets/html-manager: Manager specifically for handling HTML-based widgets.@jupyter-widgets/jupyterlab-manager: Extension for integrating widgets within JupyterLab.@jupyter-widgets/output: Package for managing widget output areas.
Overview of Core Interactive Widgets
mainThe
ipywidgetslibrary provides fundamental interactive HTML widgets for Jupyter notebooks and the IPython kernel. Core widgets include:- Sliders
- Progress bars
- Text boxes
- Toggle buttons and checkboxes
- Display areas
Overview of Jupyter Widgets
mainJupyter Widgets (
ipywidgets) is a framework for creating interactive browser controls within Jupyter notebooks. It allows users to visualize and manipulate data through intuitive graphical interfaces without needing to write code.Key capabilities include:
- Basic form controls: Sliders, checkboxes, and text inputs.
- Container controls: Tabs, accordions, horizontal/vertical layout boxes, and grid layouts.
- Advanced controls: Maps, 2D/3D visualizations, and datagrids.
The project consists of a kernel-side package (e.g., the
ipywidgetsPython package for the IPython kernel) and a browser-side extension for managing the widgets in frontends like JupyterLab or Jupyter Notebook.Understand widget manager specializations for different web contexts
mainThe core
@jupyter-widgets/controlslibrary is context-agnostic. To use widgets in specific environments, you must use a specialized widget manager that extends@jupyter-widgets/base. These managers handle widget display locations and state retrieval logic.Available specializations include:
- Classic Jupyter Notebook: Provided by the
widgetsnbextensionPython package. - JupyterLab: Provided by the
@jupyter-widgets/jupyterlab-managernpm package. - Static Embedding (Sphinx, nbviewer, etc.): Provided by the
@jupyter-widgets/html-managernpm package.
For custom web implementations, refer to the following example patterns in the repository:
web1: Simplistic use of widgets in a web context.web2: Using theapplication/vnd.jupyter.widget-state+jsonmime type.web3: Communicating with a Jupyter kernel in a web context outside of Notebook/JupyterLab.web4: Embedding widgets in an HTML document using the HTML widget manager.
- Classic Jupyter Notebook: Provided by the
Add new UI tests
mainNew test suites can be added to the
ui-tests/testsdirectory.Requirements:
- File names must end with
.test.ts. - If the tests perform visual regression or HTML source regression, you must add their reference images to the corresponding
-snapshotsdirectories.
- File names must end with
Embed Widgets in HTML Web Pages via Notebook Menu
mainThe
Embed widgetsmenu item in the classic notebook interface generates an HTML snippet for embedding widgets into static web pages.Structure of the generated snippet:
- RequireJS: A
<script>tag loads RequireJS from a CDN (can be removed if already present on your page). - Widget Embedder: A
<script>tag loads the RequireJS widget embedder (defines modules and rendering functions). For standard widgets only, you can replace these with a standard embedder script. - Widget State: A
<script>tag withtype="application/vnd.jupyter.widget-state+json"containing the serialized state of all widget models. - Widget Views: Multiple
<script>tags withtype="application/vnd.jupyter.widget-view+json"placed in the<body>. These are replaced by the rendered widget DOM trees.
Note: To ensure a clean embedding, restart the kernel and refresh the page before generating the snippet.
- RequireJS: A
Migrate from Phosphor to Lumino in browser code
mainSince the Phosphor library has been replaced by Lumino, update your imports and property accessors:
- Imports: Change
JupyterPhosphorPanelWidgetandJupyterPhosphorWidgettoJupyterLuminoPanelWidgetandJupyterLuminoWidgetfrom@jupyter-widgets/base. - Property Access: Rename
this.pWidgettothis.luminoWidget(an alias forpWidgetis available for convenience). - Message Handling: Rename
processPhosphorMessagetoprocessLuminoMessage.
To support both 7.x and 8.x, implement both methods and use a helper to call the correct super method.
- import { JupyterPhosphorPanelWidget, JupyterPhosphorWidget } from '@jupyter-widgets/base'; + import { JupyterLuminoPanelWidget, JupyterLuminoWidget } from '@jupyter-widgets/base'; - this.pWidget + this.luminoWidget - processPhosphorMessage(msg: Message): void { - super.processPhosphorMessage(msg); - switch (msg.type) { - case 'resize': - this.resize(); - break; - } - } + _processLuminoMessage(msg: Message, _super: (msg: Message) => void): void { + _super.call(this, msg); + switch (msg.type) { + case 'resize': + this.resize(); + break; + } + } + + processPhosphorMessage(msg: Message): void { + this._processLuminoMessage(msg, super.processPhosphorMessage); + } + + processLuminoMessage(msg: Message): void { + this._processLuminoMessage(msg, super.processLuminoMessage); + }- Imports: Change
Access ipywidgets examples
mainExamples for
ipywidgetsare located in theexamplessubdirectory of the GitHub repository. The examples are organized into two main categories:- notebooks: Jupyter notebooks demonstrating widget usage.
- development: Examples focused on widget development.
You can find these files in the source repository under
examples/notebooksandexamples/development.Migrate description_tooltip to tooltip
mainThe
description_tooltipattribute for certain widgets is deprecated in favor of a universaltooltipattribute available on all widgets that inherit fromDOMWidget.Action: Search and replace
description_tooltipwithtooltipto support ipywidgets 8.0+.Override the ipywidgets Embedded CDN
mainThe default CDN for ipywidgets has changed fromunpkgtojsDelivr. If your deployment requiresunpkg, you can override this by specifying thedata-jupyter-widgets-cdndata attribute on the HTML manager script tag.Install ipywidgets from source using pip
mainTo perform an editable install of the Python ipywidgets package into your user site directory, navigate to the
python/ipywidgetssubdirectory and use the--prefixflag with the user base path. Note thatpip install --user -e .is not supported due to a known pip bug.cd python/ipywidgets pip install --prefix=$(python -m site --user-base) -e .