🔍 Code Extractor

function create_training_dashboard

Maturity: 54

Factory function that creates and initializes a TrainingDashboard instance with optional user context from a parent application.

File:
/tf/active/vicechatdev/CDocs/ui/training_dashboard.py
Lines:
941 - 984
Complexity:
moderate

Purpose

This factory function serves as the entry point for creating training dashboard components in a controlled document management system. It instantiates a TrainingDashboard, optionally configures it with user context from a parent application, and handles initialization errors gracefully. The dashboard is used for viewing and managing training requirements within the application.

Source Code

def create_training_dashboard(parent_app=None, **params):
    """
    Factory function to create and initialize a TrainingDashboard instance.
    
    This function creates a training dashboard component that can be used
    in the main application for viewing and managing training requirements.
    
    Parameters
    ----------
    parent_app : ControlledDocumentApp, optional
        Reference to the parent application for navigation and user context
    **params : dict
        Additional parameters to pass to the TrainingDashboard constructor
        
    Returns
    -------
    TrainingDashboard
        Fully configured and initialized training dashboard instance
        
    Raises
    ------
    Exception
        If there's an error creating or initializing the dashboard
    """
    try:
        logger.info("Creating training dashboard instance")
        
        # Create the dashboard instance
        dashboard = TrainingDashboard(parent_app=parent_app, **params)
        
        # Initialize any additional setup if needed
        if parent_app and hasattr(parent_app, 'current_user') and parent_app.current_user:
            dashboard.set_user(parent_app.current_user)
            logger.info(f"Training dashboard initialized for user {parent_app.current_user.uid}")
        else:
            logger.info("Training dashboard created without user context")
        
        return dashboard
        
    except Exception as e:
        logger.error(f"Error creating training dashboard: {e}")
        import traceback
        logger.error(f"Traceback: {traceback.format_exc()}")
        raise Exception(f"Failed to create training dashboard: {str(e)}")

Parameters

Name Type Default Kind
parent_app - None positional_or_keyword
**params - - var_keyword

Parameter Details

parent_app: Optional reference to a ControlledDocumentApp instance that provides navigation capabilities and user context. If provided and contains a current_user attribute, the dashboard will be initialized with that user's context. Default is None.

**params: Variable keyword arguments that are passed directly to the TrainingDashboard constructor. These can include any additional configuration parameters supported by TrainingDashboard such as sizing, styling, or behavioral options.

Return Value

Returns a fully configured TrainingDashboard instance. The dashboard will be initialized with user context if parent_app is provided and contains a valid current_user. If initialization fails, raises an Exception instead of returning a value.

Dependencies

  • logging
  • typing
  • datetime
  • panel
  • param
  • pandas
  • inspect
  • traceback

Required Imports

import logging
import traceback
from CDocs.config import settings
from CDocs.models.user_extensions import DocUser
from CDocs.controllers import training_controller
from CDocs.ui.base_ui import BaseUIComponent
from CDocs.ui.training_completion import TrainingCompletion
from CDocs.controllers.document_controller import get_document_edit_url
from CDocs.models.document import ControlledDocument
from CDocs.ui.components.document_access_controls import DocumentAccessControls

Usage Example

# Basic usage without parent app
dashboard = create_training_dashboard()

# Usage with parent app for user context
from CDocs.app import ControlledDocumentApp
app = ControlledDocumentApp()
app.current_user = some_user_instance
dashboard = create_training_dashboard(parent_app=app)

# Usage with additional parameters
dashboard = create_training_dashboard(
    parent_app=app,
    width=800,
    height=600,
    sizing_mode='stretch_width'
)

# Error handling
try:
    dashboard = create_training_dashboard(parent_app=app)
except Exception as e:
    print(f'Failed to create dashboard: {e}')

Best Practices

  • Always wrap calls to this function in try-except blocks to handle initialization failures gracefully
  • Provide a parent_app with current_user when user-specific training data is needed
  • Check the logger output for detailed error information if dashboard creation fails
  • Ensure the parent_app has a valid current_user attribute before passing it to avoid partial initialization
  • The function logs both successful creation and errors, making it easier to debug issues in production
  • Use **params to pass additional configuration to TrainingDashboard without modifying this factory function

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_training_management 65.7% similar

    Factory function that instantiates and returns a TrainingManagement object with optional parent application and document UID parameters.

    From: /tf/active/vicechatdev/CDocs/ui/training_management.py
  • function create_admin_panel 64.9% 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 create_document_detail 63.3% similar

    Factory function that creates and initializes a DocumentDetail panel component, optionally loading a specific document by its UID.

    From: /tf/active/vicechatdev/document_detail_backup.py
  • function create_training_completion 60.1% similar

    Factory function that instantiates and returns a TrainingCompletion object for managing training completion workflows in a document control system.

    From: /tf/active/vicechatdev/CDocs/ui/training_completion.py
  • function create_app 58.1% similar

    Factory function that creates and configures a Flask application instance for a controlled document management system.

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