🔍 Code Extractor

function register_api_routes

Maturity: 44

Registers API routes for document access and permissions endpoints with Panel's routing system, with error handling and logging.

File:
/tf/active/vicechatdev/CDocs/controllers/api_handler.py
Lines:
18 - 28
Complexity:
simple

Purpose

This function initializes and registers two API endpoints ('/api/document_access' and '/api/document_permissions') with the Panel web framework. It's designed to be called during application startup to make document-related API endpoints available. The function includes comprehensive error handling with logging and traceback reporting to aid in debugging registration failures.

Source Code

def register_api_routes():
    """Register API routes with Panel."""
    try:
        # Register document access endpoint
        pn.state.add_route('/api/document_access', endpoint=get_document_access)
        pn.state.add_route('/api/document_permissions', endpoint=get_document_permissions)
        logger.info("Registered document access API endpoints")
    except Exception as e:
        logger.error(f"Error registering API routes: {e}")
        import traceback
        logger.error(traceback.format_exc())

Return Value

This function does not return any value (implicitly returns None). It performs side effects by registering routes with Panel's state management system.

Dependencies

  • panel
  • logging

Required Imports

import logging
import panel as pn

Conditional/Optional Imports

These imports are only needed under specific conditions:

import traceback

Condition: only used when an exception occurs during route registration for error reporting

Optional

Usage Example

import logging
import panel as pn

# Setup logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
logger.addHandler(handler)

# Define endpoint functions (examples)
def get_document_access():
    return {'status': 'ok', 'access': []}

def get_document_permissions():
    return {'status': 'ok', 'permissions': []}

# Initialize Panel application
pn.extension()

# Register API routes
register_api_routes()

# Routes are now available at:
# - /api/document_access
# - /api/document_permissions

Best Practices

  • Call this function during application initialization, before starting the Panel server
  • Ensure the logger is properly configured before calling this function to capture registration status
  • Define the endpoint functions (get_document_access and get_document_permissions) before calling this function
  • This function should only be called once during application startup to avoid duplicate route registration
  • Monitor logs for 'Registered document access API endpoints' message to confirm successful registration
  • If registration fails, check the error logs with traceback for debugging information
  • Ensure Panel's state management is initialized (pn.extension() called) before registering routes

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function init_doc 56.5% similar

    Initializes a Bokeh document by registering session information and setting up document lifecycle event handlers for Panel applications.

    From: /tf/active/vicechatdev/patches/server.py
  • function create_admin_panel_v1 53.8% similar

    Factory function that creates and initializes an AdminPanel instance with optional session management, parent application integration, and embedded mode support.

    From: /tf/active/vicechatdev/CDocs single class/ui/admin_panel.py
  • function create_approval_panel 52.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_v2 52.2% 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 main_v31 51.7% similar

    Entry point function that initializes and serves the CDocs Panel web application with configurable port and debug mode options.

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