🔍 Code Extractor

function create_static_handler

Maturity: 27

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

File:
/tf/active/vicechatdev/patches/server.py
Lines:
361 - 369
Complexity:
simple

Purpose

This function generates a route configuration tuple for Tornado web server to handle static file requests. It constructs the appropriate URL pattern based on a prefix and key, applies a patch for root path handling, and returns either a StaticFileHandler (if app has a static_path) or a generic StaticHandler. This is used in Panel/Bokeh server applications to serve CSS, JavaScript, images, and other static assets.

Source Code

def create_static_handler(prefix, key, app):
    # patch
    key = '/__patchedroot' if key == '/' else key

    route = prefix
    route += "/static/(.*)" if key == "/" else key + "/static/(.*)"
    if app.static_path is not None:
        return (route, StaticFileHandler, {"path" : app.static_path})
    return (route, StaticHandler, {})

Parameters

Name Type Default Kind
prefix - - positional_or_keyword
key - - positional_or_keyword
app - - positional_or_keyword

Parameter Details

prefix: URL prefix string to prepend to the static route. Used to namespace the static file serving path, typically representing the application's base URL path.

key: Route key identifier, typically '/' for root routes or a specific path string. Gets patched to '/__patchedroot' if it equals '/' to avoid routing conflicts.

app: Application object that must have a 'static_path' attribute. If static_path is not None, it specifies the filesystem directory path where static files are located.

Return Value

Returns a tuple with three elements: (route_pattern, handler_class, config_dict). The route_pattern is a string URL pattern with regex for matching static file requests. The handler_class is either StaticFileHandler (with path config) or StaticHandler (with empty config). The config_dict contains handler configuration, specifically the 'path' key pointing to the static files directory if using StaticFileHandler.

Dependencies

  • tornado
  • bokeh

Required Imports

from tornado.web import StaticFileHandler
from bokeh.server.views.static_handler import StaticHandler

Usage Example

from tornado.web import StaticFileHandler
from bokeh.server.views.static_handler import StaticHandler

class MockApp:
    def __init__(self, static_path=None):
        self.static_path = static_path

# Example 1: With static path configured
app_with_static = MockApp(static_path='/var/www/static')
route_tuple = create_static_handler('/app', '/dashboard', app_with_static)
# Returns: ('/app/dashboard/static/(.*)', StaticFileHandler, {'path': '/var/www/static'})

# Example 2: Without static path (uses default handler)
app_no_static = MockApp(static_path=None)
route_tuple = create_static_handler('/app', '/dashboard', app_no_static)
# Returns: ('/app/dashboard/static/(.*)', StaticHandler, {})

# Example 3: Root path handling
app_root = MockApp(static_path='/static')
route_tuple = create_static_handler('', '/', app_root)
# Returns: ('/static/(.*)', StaticFileHandler, {'path': '/static'})
# Note: key '/' is patched to '/__patchedroot' internally

Best Practices

  • Ensure the app.static_path points to a valid, readable directory when using StaticFileHandler
  • Be aware that root path '/' is automatically patched to '/__patchedroot' to avoid routing conflicts
  • The returned tuple should be added to Tornado's application URL patterns list
  • Use StaticFileHandler (with app.static_path set) for serving actual filesystem directories
  • Use StaticHandler (app.static_path=None) for custom static file handling logic
  • The regex pattern '(.*)' in the route allows serving any file under the static directory

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_static_routes 75.2% similar

    Generates a list of Tornado URL route patterns for serving static files from specified directories, mapping URL slugs to filesystem paths.

    From: /tf/active/vicechatdev/patches/server.py
  • class RootHandler 62.2% similar

    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.

    From: /tf/active/vicechatdev/patches/server.py
  • class AutoloadJsHandler 60.0% 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 DocHandler 58.3% 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
  • function serve_plot 55.0% similar

    Flask route handler that serves generated plot image files from a session-specific plots directory.

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