class Inbox
A reactive HTML component that displays a mailbox/notification icon with a badge indicator and tracks user clicks.
/tf/active/vicechatdev/resources/py2html.py
731 - 762
moderate
Purpose
The Inbox class provides an interactive notification widget that displays an envelope icon with an optional red badge indicator. It's designed to show new messages or notifications and track user interactions through click events. Built on Panel's ReactiveHTML framework, it combines HTML templating with reactive parameters to create a dynamic UI component that can be integrated into Panel dashboards or applications.
Source Code
class Inbox(ReactiveHTML):
"""
A mailbox to display new messages
attributes
----------
badge : param.Selector
Whether the red indicator is active or not, badge-on to show the indicator, badge-off to hide it.
clicks : paramm.Integer
The number of clicks
"""
badge = param.Selector(default='badge-off', objects=['badge-on','badge-off'])
clicks = param.Integer(default=0)
_template = """
<a id='notification' onclick="${script('on_click')}" href="#" class="notification">
<i id='img' class="far fa-envelope" aria-hidden="true"></i>
<span id='msgs' class="${badge}"></span>
</a>
"""
_scripts = {
"on_click":
"""
data.clicks += 1
""",
}
def on_click(self, callback):
"""Set a callback on the clicks parameters"""
return self.param.watch(callback, 'clicks', onlychanged=False)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
ReactiveHTML | - |
Parameter Details
badge: A Selector parameter that controls the visibility of the red notification badge. Accepts 'badge-on' to display the indicator or 'badge-off' to hide it. Defaults to 'badge-off'. This allows toggling the visual notification state.
clicks: An Integer parameter that tracks the number of times the notification icon has been clicked. Defaults to 0. Automatically increments each time the user clicks the notification element.
Return Value
Instantiating the Inbox class returns an Inbox object, which is a ReactiveHTML component that can be displayed in Panel applications. The on_click method returns a watcher object that monitors changes to the clicks parameter and executes the provided callback function.
Class Interface
Methods
on_click(callback) -> watcher
Purpose: Registers a callback function to be executed whenever the notification icon is clicked
Parameters:
callback: A callable function that will be invoked when the clicks parameter changes. The callback receives an event object with 'new' and 'old' values.
Returns: A watcher object from param.watch that monitors the clicks parameter. This can be used to remove the watcher later if needed.
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
badge |
param.Selector | Controls the visibility of the red notification badge indicator. Valid values are 'badge-on' (show badge) or 'badge-off' (hide badge). Defaults to 'badge-off'. | class |
clicks |
param.Integer | Tracks the total number of times the notification icon has been clicked by the user. Automatically increments with each click. Defaults to 0. | class |
_template |
str | HTML template string defining the structure of the notification component. Contains placeholders for reactive parameters and script bindings. | class |
_scripts |
dict | Dictionary mapping script names to JavaScript code. The 'on_click' script increments the clicks counter when the notification is clicked. | class |
Dependencies
parampanelpanel.reactivepanel.layout.base
Required Imports
import param
import panel as pn
from panel.reactive import ReactiveHTML
Usage Example
import panel as pn
from panel.reactive import ReactiveHTML
import param
# Define the Inbox class (as provided)
class Inbox(ReactiveHTML):
badge = param.Selector(default='badge-off', objects=['badge-on','badge-off'])
clicks = param.Integer(default=0)
_template = """
<a id='notification' onclick="${script('on_click')}" href="#" class="notification">
<i id='img' class="far fa-envelope" aria-hidden="true"></i>
<span id='msgs' class="${badge}"></span>
</a>
"""
_scripts = {"on_click": "data.clicks += 1"}
def on_click(self, callback):
return self.param.watch(callback, 'clicks', onlychanged=False)
# Instantiate the inbox
inbox = Inbox()
# Set badge to show notification
inbox.badge = 'badge-on'
# Define a callback for click events
def handle_click(event):
print(f'Inbox clicked {event.new} times')
if event.new >= 3:
inbox.badge = 'badge-off'
# Register the callback
inbox.on_click(handle_click)
# Display in a Panel app
pn.serve(inbox)
Best Practices
- Always define CSS styles for .notification, .badge-on, and .badge-off classes to ensure proper visual appearance
- Include Font Awesome CSS in your application for the envelope icon to render correctly
- Use the on_click method to register callbacks rather than directly watching the clicks parameter for cleaner code
- Toggle the badge parameter between 'badge-on' and 'badge-off' to show/hide notifications based on application state
- The clicks parameter increments on every click; reset it manually if needed for your use case
- Remember that this is a ReactiveHTML component, so it must be used within a Panel application context
- The component inherits from ReactiveHTML, so all Panel reactive features are available
- Consider using onlychanged=True in custom watchers if you only want to trigger on actual value changes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class Clickable_header 60.2% similar
-
class Focus_button 57.0% similar
-
class Click_watcher 55.5% similar
-
class Browser_button 55.3% similar
-
class Confirm_button 54.2% similar