Path: blob/main/examples/reference/custom_components/AnyWidgetComponent.ipynb
3218 views
Panel's AnyWidgetComponent class simplifies the creation of custom Panel components using the AnyWidget Front End Module (AFM) specification maintained by AnyWidget.
This allows the Panel, Jupyter and other communities to collaborate and share JavaScript code for widgets.
:::{note} Panel's AnyWidgetComponent supports using the AnyWidget API on the JavaScript side and the param parameters API on the Python side.
If you are looking to create custom components using Python and Panel component only, check out Viewer. :::
API
AnyWidgetComponent Attributes
_esm(str | PurePath): This attribute accepts either a string or a path that points to an ECMAScript module. The ECMAScript module should export adefaultobject or function that returns an object. The object should contain arenderfunction and optionally aninitializefunction. In a development environment such as a notebook or when using--devflag, the module will automatically reload upon saving changes._importmap(dict | None): This optional dictionary defines an import map, allowing you to customize how module specifiers are resolved._stylesheets(list[str] | list[PurePath]): This optional attribute accepts a list of CSS strings or paths to CSS files. It supports automatic reloading in development environments. It works similarly to theAnyWidget_cssattribute.
:::note You may specify a path to a file as a string instead of a PurePath. The path should be specified relative to the file it is referenced in. :::
render Function
The _esm default object must contain a render function. It accepts the following parameters:
model: Represents the parameters of the component and provides methods to.getvalues,.setvalues, and.save_changes. In addition to the AnyWidgets methods, Panel uniquely provides theget_childmethod to enable rendering of child models.el: The parent HTML element to which HTML elements are appended.
For more details, see AnyWidget.
Usage
Styling with CSS
Include CSS within the _stylesheets attribute to style the component. The CSS is injected directly into the component's HTML.
:::{note}
The AnyWidget will automatically add the CSS class counter-widget to the el.
The AnyWidgetComponent does not add this class, but you can do it yourself via el.classList.add("counter-widget");.
:::
Dependency Imports
JavaScript dependencies can be directly imported via URLs, such as those from esm.sh.
Use the _importmap attribute for more concise module references.
See the import map documentation for more information about the import map format.
External Files
You can load JavaScript and CSS from files by providing the paths to these files.
Create the file counter_button.py.
Now create the file counter_button.js.
Now create the file counter_button.css.
Serve the app with panel serve counter_button.py --autoreload.
You can now edit the JavaScript or CSS file, and the changes will be automatically reloaded.
Try changing the
innerHTMLfromcount is ${value()}toCOUNT IS ${value()}and observe the update. Note that you must updateinnerHTMLin two places.Try changing the background color from
#0072B5to#008080.
Displaying A Single Child
You can display Python objects by defining a Child parameter. Please note that this feature is currently not supported by AnyWidget.
Lets start with the simplest example:
If you provide a non-Viewable child it will automatically be converted to a Viewable by pn.panel:
If you want to allow a certain type of Panel components only you can specify the specific type in the class_ argument.
The class_ argument also supports a tuple of types:
Displaying a List of Children
You can also display a List of Viewable objects using the Children parameter type:
:::note You can change the item_type to a specific subtype of Viewable or a tuple of Viewable subtypes. :::
React
You can use React with AnyWidget as shown below.
:::{note} You will notice that Panel's AnyWidgetComponent can be used with React and JSX without any build tools. Instead of build tools, Panel uses Sucrase to transpile the JSX code to JavaScript on the client side.
Important: When using @anywidget/react, always pin the React version in your import map to ensure compatibility. The example above pins React to version 18.2.0 because @anywidget/react currently works best with React 18. Without version pinning, esm.sh will serve the latest React version, which may have breaking changes. :::