class BaseUIComponent
Base class for UI components that provides common notification functionality for displaying error, success, warning, and info messages.
/tf/active/vicechatdev/CDocs/ui/base_ui.py
14 - 48
simple
Purpose
BaseUIComponent serves as a foundation class for building UI components with standardized notification capabilities. It inherits from param.Parameterized to leverage Panel's parameter system and provides a consistent interface for displaying messages to users. The class delegates notification display to a parent application if available, otherwise falls back to logging. This design pattern enables loose coupling between UI components and the notification system while ensuring messages are always captured.
Source Code
class BaseUIComponent(param.Parameterized):
"""Base class for UI components with common functionality."""
def __init__(self, parent_app=None, **params):
super().__init__(**params)
self.parent_app = parent_app
self.notification_area = None
def show_error(self, message: str):
"""Show an error message."""
if self.parent_app and hasattr(self.parent_app, 'show_notification'):
self.parent_app.show_notification(message, level="error")
else:
logger.error(f"UI Error: {message}")
def show_success(self, message: str):
"""Show a success message."""
if self.parent_app and hasattr(self.parent_app, 'show_notification'):
self.parent_app.show_notification(message, level="success")
else:
logger.info(f"UI Success: {message}")
def show_warning(self, message: str):
"""Show a warning message."""
if self.parent_app and hasattr(self.parent_app, 'show_notification'):
self.parent_app.show_notification(message, level="warning")
else:
logger.warning(f"UI Warning: {message}")
def show_info(self, message: str):
"""Show an info message."""
if self.parent_app and hasattr(self.parent_app, 'show_notification'):
self.parent_app.show_notification(message, level="info")
else:
logger.info(f"UI Info: {message}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
param.Parameterized | - |
Parameter Details
parent_app: Optional reference to the parent application object that should implement a 'show_notification' method. If provided, notifications will be delegated to this parent; if None, messages will be logged instead. Expected to have a show_notification(message, level) method.
**params: Additional keyword arguments passed to the param.Parameterized base class constructor, allowing for parameter initialization and configuration of the Parameterized system.
Return Value
Instantiation returns a BaseUIComponent object with initialized parent_app and notification_area attributes. The notification methods (show_error, show_success, show_warning, show_info) do not return values (implicit None return), they perform side effects by displaying notifications or logging messages.
Class Interface
Methods
__init__(self, parent_app=None, **params)
Purpose: Initialize the BaseUIComponent with optional parent application reference and parameter configuration
Parameters:
parent_app: Optional parent application object that implements show_notification method. Defaults to None.**params: Additional keyword arguments passed to param.Parameterized base class
Returns: None (constructor)
show_error(self, message: str)
Purpose: Display an error message through the parent app's notification system or log it if no parent app is available
Parameters:
message: The error message string to display or log
Returns: None - performs side effect of displaying notification or logging
show_success(self, message: str)
Purpose: Display a success message through the parent app's notification system or log it if no parent app is available
Parameters:
message: The success message string to display or log
Returns: None - performs side effect of displaying notification or logging
show_warning(self, message: str)
Purpose: Display a warning message through the parent app's notification system or log it if no parent app is available
Parameters:
message: The warning message string to display or log
Returns: None - performs side effect of displaying notification or logging
show_info(self, message: str)
Purpose: Display an informational message through the parent app's notification system or log it if no parent app is available
Parameters:
message: The informational message string to display or log
Returns: None - performs side effect of displaying notification or logging
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
parent_app |
Optional[object] | Reference to the parent application object that handles notification display. Expected to have a show_notification method if not None. | instance |
notification_area |
Optional[object] | Placeholder for a notification area component. Initialized to None and can be set by subclasses to manage their own notification display mechanism. | instance |
Dependencies
parampanellogging
Required Imports
import param
import panel as pn
import logging
Usage Example
import param
import panel as pn
import logging
logger = logging.getLogger(__name__)
# Example parent app with notification support
class MyApp:
def show_notification(self, message, level):
print(f"[{level.upper()}] {message}")
# Instantiate with parent app
parent = MyApp()
component = BaseUIComponent(parent_app=parent)
component.show_success("Operation completed successfully")
component.show_error("An error occurred")
# Instantiate without parent app (falls back to logging)
standalone_component = BaseUIComponent()
standalone_component.show_warning("This will be logged")
standalone_component.show_info("Information message")
Best Practices
- Always ensure a logger is configured in the module before instantiating this class to handle fallback logging
- When creating subclasses, call super().__init__(parent_app=parent_app, **params) to properly initialize the base class
- If providing a parent_app, ensure it implements the show_notification(message, level) method with support for 'error', 'success', 'warning', and 'info' levels
- Use this as a base class for UI components that need consistent notification behavior across an application
- The notification_area attribute is initialized to None but can be set by subclasses if they need to manage their own notification display
- This class follows the delegation pattern - it delegates notification display to the parent app when available, promoting loose coupling
- Methods can be called in any order as they are independent and stateless (no method dependencies)
- Consider this class as an abstract base - it's designed to be inherited rather than used directly
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class AdminPanel 50.5% similar
-
class ApprovalPanel 50.0% similar
-
class NotificationManager 49.8% similar
-
class ProgressIndicator 48.7% similar
-
class ApprovalPanel_v1 48.7% similar