function register_api_routes
Registers API routes for document access and permissions endpoints with Panel's routing system, with error handling and logging.
/tf/active/vicechatdev/CDocs/controllers/api_handler.py
18 - 28
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
panellogging
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
OptionalUsage 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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function init_doc 56.5% similar
-
function create_admin_panel_v1 53.8% similar
-
function create_approval_panel 52.5% similar
-
function create_approval_panel_v2 52.2% similar
-
function main_v31 51.7% similar