🔍 Code Extractor

function create_embedded_admin_panel

Maturity: 48

Creates and returns an embedded AdminPanel instance configured for integration within other application interfaces without its own template or session manager.

File:
/tf/active/vicechatdev/CDocs/ui/admin_panel.py
Lines:
1734 - 1758
Complexity:
moderate

Purpose

This function instantiates an AdminPanel component in embedded mode, designed to be integrated into existing application interfaces. It bypasses the need for a standalone template and session manager by relying on the parent application's infrastructure. This is useful when you want to embed administrative functionality within a larger application without creating a separate admin interface. The function includes error handling and logging for debugging purposes.

Source Code

def create_embedded_admin_panel(parent_app=None):
    """
    Create an embedded admin panel for use within other interfaces.
    
    Args:
        parent_app: Reference to the parent application
        
    Returns:
        AdminPanel: Configured admin panel instance in embedded mode
    """
    try:
        admin_panel = AdminPanel(
            template=None,  # No template needed for embedded mode
            session_manager=None,  # Will use parent app's user
            parent_app=parent_app,
            embedded=True
        )
        
        logger.info("Embedded admin panel created successfully")
        return admin_panel
        
    except Exception as e:
        logger.error(f"Error creating embedded admin panel: {e}")
        logger.error(f"Traceback: {traceback.format_exc()}")
        raise

Parameters

Name Type Default Kind
parent_app - None positional_or_keyword

Parameter Details

parent_app: Optional reference to the parent application object. This allows the embedded admin panel to access parent application resources, user sessions, and context. Defaults to None if no parent application is provided. The parent_app should typically be an application instance that has its own session management and user authentication.

Return Value

Returns an AdminPanel instance configured in embedded mode. The AdminPanel object has template=None (no standalone UI template), session_manager=None (uses parent app's session), parent_app set to the provided parent_app parameter, and embedded=True flag. If an error occurs during creation, the function raises an exception after logging the error details.

Dependencies

  • logging
  • traceback
  • panel
  • pandas
  • numpy
  • matplotlib
  • param

Required Imports

import logging
import traceback
import panel as pn
from CDocs.models.user_extensions import DocUser
from CDocs.controllers.admin_controller import get_system_stats, get_users, create_user, update_user, delete_user, get_departments, get_user_activity, get_document_stats
from CDocs.controllers import ResourceNotFoundError, ValidationError, PermissionError, BusinessRuleError
from CDocs.auth.session_manager import SessionManager
from CDocs.utils.notifications import NotificationManager
from CDocs.db.db_operations import run_query
from CDocs.config import settings, permissions

Usage Example

import logging
import traceback
from my_app import MyParentApp

# Initialize logger
logger = logging.getLogger(__name__)

# Create parent application instance
parent_app = MyParentApp()

# Create embedded admin panel
try:
    admin_panel = create_embedded_admin_panel(parent_app=parent_app)
    # Use the admin panel within your application
    # For example, add it to a layout:
    # layout = pn.Column(parent_app.main_content, admin_panel)
except Exception as e:
    print(f"Failed to create admin panel: {e}")

# Or create without parent app
admin_panel_standalone = create_embedded_admin_panel()

Best Practices

  • Always wrap calls to this function in try-except blocks to handle potential initialization errors
  • Ensure the parent_app parameter has proper session management and user authentication before passing it
  • Initialize a logger before calling this function to capture error messages
  • The AdminPanel class must be defined in the same module or imported before calling this function
  • When using in embedded mode, ensure the parent application handles user permissions and authentication
  • Check that all CDocs dependencies (config, models, controllers, auth, utils, db) are properly installed and configured
  • Consider the lifecycle of the parent_app - the embedded panel may maintain references to it

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_admin_panel 84.5% similar

    Factory function that creates and returns a configured AdminPanel instance with optional template, session management, parent application reference, and embedding mode.

    From: /tf/active/vicechatdev/CDocs/ui/admin_panel.py
  • function get_admin_view 75.1% similar

    Retrieves an admin panel view component for a Panel-based application, handling user authentication and error cases gracefully.

    From: /tf/active/vicechatdev/CDocs/ui/admin_panel.py
  • function create_approval_panel 64.5% similar

    Factory function that creates and initializes an ApprovalPanel instance with error handling, supporting both standalone and embedded modes for document approval management.

    From: /tf/active/vicechatdev/CDocs/ui/approval_panel_bis.py
  • function create_approval_panel_v1 62.4% similar

    Factory function that creates and initializes an ApprovalPanel instance for managing document approvals, with error handling and fallback to a minimal panel on failure.

    From: /tf/active/vicechatdev/CDocs/ui/approval_panel.py
  • function create_review_panel 58.6% similar

    Factory function that creates and initializes a ReviewPanel instance with error handling, supporting both standalone and embedded modes for document review management.

    From: /tf/active/vicechatdev/CDocs/ui/review_panel.py
← Back to Browse