🔍 Code Extractor

function create_workflow_panel

Maturity: 45

Factory function that creates and initializes workflow management panels (Review or Approval) with appropriate configuration, error handling, and fallback mechanisms.

File:
/tf/active/vicechatdev/CDocs single class/ui/workflow_panel_base.py
Lines:
1561 - 1623
Complexity:
moderate

Purpose

This function serves as a factory for creating workflow management UI panels in a Panel-based application. It dynamically imports and instantiates either a ReviewPanel or ApprovalPanel based on the workflow_type parameter, configures the panel with a Bootstrap template, sets up user context, and provides graceful error handling with a minimal fallback panel if initialization fails. It supports both standalone and embedded modes for flexible integration into larger applications.

Source Code

def create_workflow_panel(
    workflow_type='REVIEW',
    controller=None,
    session_manager=None, 
    parent_app=None, 
    embedded=False
):
    """Create and initialize an appropriate workflow panel based on type"""
    try:
        # Verify valid workflow type
        if workflow_type not in ['REVIEW', 'APPROVAL']:
            logger.error(f"Invalid workflow type: {workflow_type}")
            workflow_type = 'REVIEW'  # Default to review
            
        # Create title based on workflow type
        title = "Review Management" if workflow_type == 'REVIEW' else "Approval Management"
            
        # Create template for panel
        template = pn.template.BootstrapTemplate(
            title=title,
            header_background="#3DCAB1" if workflow_type == 'REVIEW' else "#007bff"
        )
        
        # Import appropriate panel subclass
        if workflow_type == 'REVIEW':
            from CDocs.ui.review_panel import ReviewPanel
            panel_class = ReviewPanel
        else:
            from CDocs.ui.approval_panel import ApprovalPanel
            panel_class = ApprovalPanel
        
        # Create and initialize panel
        panel = panel_class(
            template=template,
            session_manager=session_manager,
            parent_app=parent_app,
            embedded=embedded or parent_app is not None,
            controller=controller
        )
        
        # If parent_app has a current_user, set it directly
        if parent_app and hasattr(parent_app, 'current_user') and parent_app.current_user:
            panel.set_user(parent_app.current_user)
        
        return panel
        
    except Exception as e:
        logger.error(f"Error creating workflow panel: {e}")
        logger.error(traceback.format_exc())
        
        # Return a minimal functioning panel
        minimal_panel = pn.Column(
            pn.pane.Markdown(f"# {'Review' if workflow_type == 'REVIEW' else 'Approval'} Management"),
            pn.pane.Markdown(f"**Error loading panel:** {str(e)}")
        )
        minimal_panel.main_content = minimal_panel
        
        # Add minimal set_user method to avoid errors
        def minimal_set_user(obj, user):
            pass
        minimal_panel.set_user = types.MethodType(minimal_set_user, minimal_panel)
        
        return minimal_panel

Parameters

Name Type Default Kind
workflow_type - 'REVIEW' positional_or_keyword
controller - None positional_or_keyword
session_manager - None positional_or_keyword
parent_app - None positional_or_keyword
embedded - False positional_or_keyword

Parameter Details

workflow_type: String specifying the type of workflow panel to create. Valid values are 'REVIEW' or 'APPROVAL'. Defaults to 'REVIEW'. Invalid values are logged and defaulted to 'REVIEW'.

controller: Optional controller object for managing workflow operations. Can be None. Passed directly to the panel class constructor for handling business logic.

session_manager: Optional SessionManager instance for managing user sessions and authentication state. Can be None. Passed to the panel for session-aware operations.

parent_app: Optional reference to the parent application object. If provided and has a 'current_user' attribute, the user is automatically set on the created panel. Also affects the 'embedded' mode.

embedded: Boolean flag indicating whether the panel should run in embedded mode (as part of a larger application) or standalone. Defaults to False. Automatically set to True if parent_app is provided.

Return Value

Returns an initialized panel object (ReviewPanel or ApprovalPanel instance) with configured template, session manager, and user context. In case of errors, returns a minimal Panel Column object with error message display and a stub set_user method to prevent downstream errors. The returned object always has a 'set_user' method and 'main_content' attribute.

Dependencies

  • panel
  • logging
  • types
  • traceback
  • CDocs.ui.review_panel
  • CDocs.ui.approval_panel

Required Imports

import logging
import types
import traceback
import panel as pn

Conditional/Optional Imports

These imports are only needed under specific conditions:

from CDocs.ui.review_panel import ReviewPanel

Condition: only when workflow_type is 'REVIEW'

Required (conditional)
from CDocs.ui.approval_panel import ApprovalPanel

Condition: only when workflow_type is 'APPROVAL'

Required (conditional)

Usage Example

import panel as pn
from CDocs.auth.session_manager import SessionManager
from your_module import create_workflow_panel

# Create a review panel with session manager
session_mgr = SessionManager()
review_panel = create_workflow_panel(
    workflow_type='REVIEW',
    session_manager=session_mgr,
    embedded=False
)

# Create an approval panel embedded in parent app
class ParentApp:
    def __init__(self):
        self.current_user = {'username': 'john_doe', 'id': 123}

parent = ParentApp()
approval_panel = create_workflow_panel(
    workflow_type='APPROVAL',
    parent_app=parent,
    embedded=True
)

# Serve the panel
pn.serve(review_panel.main_content, port=5006)

Best Practices

  • Always validate workflow_type before calling if you need specific behavior, though the function defaults to 'REVIEW' for invalid types
  • Ensure the logger object is properly configured in the module scope before using this function
  • If using parent_app parameter, ensure it has a 'current_user' attribute if you want automatic user context setting
  • The function returns a minimal fallback panel on errors, so always check if the returned object has expected methods before calling specialized panel methods
  • When embedding in a larger application, set parent_app parameter to enable proper integration and user context propagation
  • The returned panel object will always have a 'set_user' method, making it safe to call even on error fallback panels
  • Consider wrapping calls in try-except blocks if you need to handle panel creation failures differently than the built-in error handling

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_review_panel_v1 87.1% similar

    Factory function that creates and initializes a review panel UI component for document review workflow management, with error handling and fallback to a minimal panel on failure.

    From: /tf/active/vicechatdev/CDocs single class/ui/review_panel.py
  • function create_approval_panel_v1 82.3% similar

    Factory function that creates and initializes an approval workflow panel UI component for managing document approval cycles.

    From: /tf/active/vicechatdev/CDocs single class/ui/approval_panel.py
  • function create_approval_panel_v2 81.3% 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 80.1% 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
  • function create_approval_panel 78.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
← Back to Browse