function create_static_handler
Creates a Tornado route handler tuple for serving static files in a Bokeh/Panel web application, with special handling for root path routing.
/tf/active/vicechatdev/patches/server.py
361 - 369
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
tornadobokeh
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_static_routes 75.2% similar
-
class RootHandler 62.2% similar
-
class AutoloadJsHandler 60.0% similar
-
class DocHandler 58.3% similar
-
function serve_plot 55.0% similar