🔍 Code Extractor

class RootHandler

Maturity: 33

A custom Tornado request handler that extends Bokeh's RootHandler to handle root URL requests with authentication and conditional redirection based on index file extensions.

File:
/tf/active/vicechatdev/patches/server.py
Lines:
267 - 275
Complexity:
moderate

Purpose

This class serves as the entry point handler for a Bokeh server application, managing root URL requests. It adds authentication requirements and implements special logic to redirect non-HTML index files to their extensionless counterparts. This is typically used in web applications where clean URLs are desired (e.g., redirecting from '/index.py' to '/index'). The handler inherits from Bokeh's BkRootHandler to maintain compatibility with Bokeh's server infrastructure while adding custom routing behavior.

Source Code

class RootHandler(BkRootHandler):

    @authenticated
    async def get(self, *args, **kwargs):
        if self.index and not self.index.endswith('.html'):
            prefix = "" if self.prefix is None else self.prefix
            redirect_to = prefix + '.'.join(self.index.split('.')[:-1])
            self.redirect(redirect_to)
        await super().get(*args, **kwargs)

Parameters

Name Type Default Kind
bases BkRootHandler -

Parameter Details

bases: Inherits from BkRootHandler (Bokeh's root handler), which provides the base functionality for handling root URL requests in a Bokeh server application. The parent class handles serving the main application page and managing Bokeh document lifecycle.

Return Value

The class instantiation returns a RootHandler instance that can be used as a Tornado request handler. The get method returns None (async method that handles the HTTP response internally through Tornado's response mechanisms). The method either redirects the client to a modified URL or serves the requested page through the parent class implementation.

Class Interface

Methods

async get(self, *args, **kwargs) -> None

Purpose: Handles HTTP GET requests to the root URL with authentication, performing conditional redirection for non-HTML index files before delegating to the parent class

Parameters:

  • args: Variable positional arguments passed from Tornado's routing system, typically containing URL path components
  • kwargs: Variable keyword arguments passed from Tornado's routing system, containing query parameters and other request metadata

Returns: None - the method handles the HTTP response internally through Tornado's response mechanisms (either by redirecting or serving content)

Attributes

Name Type Description Scope
index str or None The index file name to serve (e.g., 'index.py', 'index.html'). Set by Bokeh's routing system. Used to determine if redirection is needed for non-HTML files. instance
prefix str or None URL prefix for the application route. Set by Bokeh's routing system. Used to construct the redirect URL while maintaining the application's base path. instance

Dependencies

  • tornado
  • bokeh

Required Imports

from bokeh.server.views.root_handler import RootHandler as BkRootHandler
from tornado.web import authenticated

Usage Example

from bokeh.server.views.root_handler import RootHandler as BkRootHandler
from tornado.web import authenticated
from bokeh.server.server import Server
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler

class RootHandler(BkRootHandler):
    @authenticated
    async def get(self, *args, **kwargs):
        if self.index and not self.index.endswith('.html'):
            prefix = "" if self.prefix is None else self.prefix
            redirect_to = prefix + '.'.join(self.index.split('.')[:-1])
            self.redirect(redirect_to)
        await super().get(*args, **kwargs)

# Usage in Bokeh server setup
def make_document(doc):
    # Your Bokeh application logic
    pass

app = Application(FunctionHandler(make_document))

# Configure server with custom handler
server = Server(
    {'/': app},
    extra_patterns=[('/', RootHandler)],
    port=5006
)
server.start()
server.io_loop.start()

Best Practices

  • This handler must be used within a Bokeh server application context where BkRootHandler is properly configured
  • The @authenticated decorator requires that authentication is properly configured in the Tornado application settings
  • The handler expects 'self.index' and 'self.prefix' attributes to be set by the framework - these are typically set by Bokeh's URL routing system
  • Always call await super().get(*args, **kwargs) to ensure parent class functionality is executed
  • The redirection logic only applies to non-HTML index files, allowing clean URLs without file extensions
  • This handler should be registered in the Tornado application's URL patterns to handle root requests
  • Ensure proper error handling is in place for authentication failures
  • The handler is designed to work asynchronously - do not use blocking operations in the get method

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class DocHandler 64.5% similar

    DocHandler is a Tornado request handler that serves HTML pages for Bokeh sessions, combining session management with document rendering capabilities.

    From: /tf/active/vicechatdev/patches/server.py
  • class SessionPrefixHandler 62.4% similar

    A mixin class that provides a context manager for temporarily modifying URL prefix-related state variables during request handling in a Bokeh server application.

    From: /tf/active/vicechatdev/patches/server.py
  • function create_static_handler 62.2% similar

    Creates a Tornado route handler tuple for serving static files in a Bokeh/Panel web application, with special handling for root path routing.

    From: /tf/active/vicechatdev/patches/server.py
  • class AutoloadJsHandler 60.1% similar

    A custom Tornado request handler that generates and serves the Bokeh autoload JavaScript chunk for embedding Bokeh applications in web pages.

    From: /tf/active/vicechatdev/patches/server.py
  • class ProxyFallbackHandler 57.5% similar

    A Tornado RequestHandler that wraps another HTTP server callback and proxies requests by modifying the request path before delegating to a fallback handler.

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