🔍 Code Extractor

class Application

Maturity: 38

A custom Bokeh Application subclass that extends BkApplication to integrate with Panel's state management system, handling session creation callbacks and document initialization with templates.

File:
/tf/active/vicechatdev/patches/server.py
Lines:
165 - 176
Complexity:
moderate

Purpose

This class serves as the core application handler for Panel server applications. It overrides Bokeh's Application class to hook into the session lifecycle, executing registered callbacks when sessions are created and initializing documents with Panel templates. It acts as the bridge between Bokeh's server infrastructure and Panel's template/state management system, ensuring proper setup of Panel components when served through Bokeh server.

Source Code

class Application(BkApplication):

    async def on_session_created(self, session_context):
        for cb in state._on_session_created:
            cb(session_context)
        await super().on_session_created(session_context)

    def initialize_document(self, doc):
        super().initialize_document(doc)
        if doc in state._templates:
            template = state._templates[doc]
            template.server_doc(title=template.title, location=True, doc=doc)

Parameters

Name Type Default Kind
bases BkApplication -

Parameter Details

bases: Inherits from BkApplication (Bokeh's Application class), which provides the base server application functionality for handling documents, sessions, and server lifecycle

Return Value

Instantiation returns an Application object that can be passed to Bokeh Server. The on_session_created method returns an awaitable (async) that completes when all session callbacks finish. The initialize_document method returns None but modifies the document in-place.

Class Interface

Methods

async on_session_created(self, session_context) -> None

Purpose: Handles session creation by executing all registered callbacks in state._on_session_created, then delegates to parent class implementation

Parameters:

  • session_context: Bokeh SessionContext object containing session metadata like session ID, request information, and server context

Returns: None (async coroutine that completes when all session callbacks and parent initialization finish)

initialize_document(self, doc) -> None

Purpose: Initializes a Bokeh document by first calling parent initialization, then setting up Panel template if one is registered for this document

Parameters:

  • doc: Bokeh Document object to be initialized with Panel template and configuration

Returns: None (modifies the document in-place by attaching template components)

Attributes

Name Type Description Scope
state._on_session_created list[callable] Global list of callback functions to execute when a new session is created, accessed from the state module instance
state._templates dict[Document, BaseTemplate] Global dictionary mapping Bokeh documents to Panel templates, accessed from the state module instance

Dependencies

  • bokeh
  • param
  • tornado
  • panel

Required Imports

from bokeh.application import Application as BkApplication
from state import state
from template import BaseTemplate

Usage Example

# Typically used internally by Panel's server infrastructure
from bokeh.application import Application as BkApplication
from panel.io.server import Application
from panel.io.state import state
from panel.template import BaseTemplate

# Register a session callback
def my_session_callback(session_context):
    print(f"Session created: {session_context.id}")

state._on_session_created.append(my_session_callback)

# Create a template and register it
template = BaseTemplate(title='My App')

# The Application is typically instantiated by Panel's serve() function
# but can be created manually:
app = Application()

# Use with Bokeh Server
from bokeh.server.server import Server
server = Server({'/': app}, port=5006)
server.start()

Best Practices

  • This class is typically instantiated by Panel's serve() function rather than directly by users
  • Ensure state._on_session_created callbacks are registered before the application starts serving
  • Register templates in state._templates dictionary before documents are initialized
  • Session callbacks should be lightweight and avoid blocking operations since they run during session creation
  • The on_session_created method is async, so all callbacks should handle async context appropriately
  • Always call super() methods to maintain Bokeh's base functionality
  • Document initialization happens once per document, so template setup should be idempotent
  • The class relies on global state module, so be cautious in multi-application scenarios

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function init_doc 69.7% 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
  • class _state 66.5% similar

    A global state management class that tracks and manages the state of running Panel applications, including server information, session data, busy status, and various application-level resources.

    From: /tf/active/vicechatdev/patches/state.py
  • function _eval_panel 63.8% similar

    Evaluates and initializes a panel component (function, template, or panel object) within a Bokeh document context, handling different panel types and modifying the document accordingly.

    From: /tf/active/vicechatdev/patches/server.py
  • class ControlledDocApp 63.6% similar

    A standalone Panel web application class that provides a complete controlled document management system with user authentication, navigation, and document lifecycle management features.

    From: /tf/active/vicechatdev/panel_app.py
  • function _initialize_session_info 61.4% similar

    Initializes and manages session information for a web application session, tracking session lifecycle timestamps and user agent data while maintaining a configurable history limit.

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