Path: blob/main/examples/reference/custom_components/AnyWidgetComponent.ipynb
2011 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 adefault
object or function that returns an object. The object should contain arender
function and optionally aninitialize
function. In a development environment such as a notebook or when using--dev
flag, 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
_css
attribute.
:::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.get
values,.set
values, and.save_changes
. In addition to the AnyWidgets methods, Panel uniquely provides theget_child
method 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
innerHTML
fromcount is ${value()}
toCOUNT IS ${value()}
and observe the update. Note that you must updateinnerHTML
in two places.Try changing the background color from
#0072B5
to#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.