Path: blob/main/examples/reference/custom_components/Viewer.ipynb
2011 views
A Viewer
is a good abstraction for combining some business logic about your application with Panel components and then letting you use it as if it was a Panel component. It provides a useful abstraction extension of a simple Parameterized
class. Note however that it is not actually a Panel component, i.e. it does not behave like a widget, layout or pane. If you want to build a Panel component in Python the PyComponent
class is a better option.
:::{note} If you are looking to create new components using JavaScript, check out JSComponent
, ReactComponent
, or AnyWidgetComponent
instead. :::
API
Attributes
None. The Viewer
class does not have any special attributes. It is a simple param.Parameterized
class with a few additional methods. This also means you will have to add or support parameters like height
, width
, sizing_mode
, etc., yourself if needed.
Methods
__panel__
: Must be implemented. Should return the Panel component or object to be displayed.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. :::