Path: blob/main/examples/reference/custom_components/PyComponent.ipynb
2011 views
A PyComponent
, unlike a Viewer
class inherits the entire API of Panel components, including all layout and styling related parameters. It doesn't just imitate a Panel component, it actually is one and therefore is a good option to build some composite widget made up of other widgets, a layout with some special behavior or a pane that renders some type of object in a way that is useful but does not require novel functionality.
:::{note} If you are looking to create new components using JavaScript, check out JSComponent
, ReactComponent
, or AnyWidgetComponent
instead. :::
API
Attributes
The PyComponent
class inherits the entire Panel Viewable
API including all sizing, layout and styling related parameters such as width
, height
, sizing_mode
etc.
Methods
__panel__
: Must be implemented. Should return the Panel component or object to be displayed. Will be lazily evaluated and cached on render.servable
: This method serves the component using Panel's built-in server when runningpanel serve ...
.show
: Displays the component in a new browser tab when runningpython ...
.
Usage
Styling with CSS
You can style the component by styling the component(s) returned by __panel__
using their styles
or stylesheets
attributes.
See the Apply CSS guide for more information on styling Panel components.
Displaying A Single Child
You can display Panel components (Viewable
s) by defining a Child
parameter.
Let's start with the simplest example:
Calling self.param.object.rx()
creates a reactive expression which updates when the object
parameter is updated.
Let's replace the object
with a Button
:
Let's change it back
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. :::