🔍 Code Extractor

function init_doc

Maturity: 32

Initializes a Bokeh document by registering session information and setting up document lifecycle event handlers for Panel applications.

File:
/tf/active/vicechatdev/patches/server.py
Lines:
377 - 391
Complexity:
moderate

Purpose

This function is part of Panel's server infrastructure that manages Bokeh document initialization. It updates session tracking information with a start timestamp, registers a document_ready event handler to initialize the session state, and ensures proper session lifecycle management. It's typically called during the setup phase of a Panel server application to track and manage user sessions.

Source Code

def init_doc(doc):
    doc = doc or curdoc()
    if not doc.session_context:
        return doc

    session_id = doc.session_context.id
    sessions = state.session_info['sessions']
    if session_id not in sessions:
        return doc

    sessions[session_id].update({
        'started': dt.datetime.now().timestamp()
    })
    doc.on_event('document_ready', state._init_session)
    return doc

Parameters

Name Type Default Kind
doc - - positional_or_keyword

Parameter Details

doc: A Bokeh Document object representing the current document context. If None or not provided, the function will use curdoc() to get the current document. The document must have a session_context attribute with an id property for session tracking to work properly.

Return Value

Returns the Bokeh Document object that was passed in (or retrieved via curdoc()). The document is returned after being potentially modified with session tracking information and event handlers. If the document has no session_context or the session_id is not found in state.session_info['sessions'], the document is returned unmodified.

Dependencies

  • bokeh
  • datetime
  • param

Required Imports

import datetime as dt
from bokeh.io import curdoc
from state import state

Usage Example

from bokeh.io import curdoc
from state import state
import datetime as dt

# Ensure state is initialized
if not hasattr(state, 'session_info'):
    state.session_info = {'sessions': {}}

# Get current document
doc = curdoc()

# Initialize session tracking if session exists
if doc.session_context:
    session_id = doc.session_context.id
    state.session_info['sessions'][session_id] = {}

# Initialize the document with session tracking
initialized_doc = init_doc(doc)

# The document now has session tracking and event handlers registered

Best Practices

  • Always ensure the state.session_info dictionary is properly initialized before calling this function
  • This function should be called early in the document lifecycle, typically during application setup
  • The function is idempotent - calling it multiple times on the same document is safe
  • Ensure state._init_session method is defined before using this function
  • This function is designed for server-side Panel applications and may not be necessary for standalone scripts
  • The session_id must already exist in state.session_info['sessions'] for tracking to occur

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function _initialize_session_info 73.0% 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
  • class Application 69.7% similar

    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.

    From: /tf/active/vicechatdev/patches/server.py
  • function _eval_panel 69.1% 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
  • function set_curdoc 64.7% similar

    A context manager that temporarily sets the current Bokeh document (curdoc) in the application state, ensuring it is properly cleaned up after use.

    From: /tf/active/vicechatdev/patches/server.py
  • function modify_document 63.7% similar

    Modifies a Bokeh document by executing a Python script/module within the document's context, with support for autoreload functionality and error handling.

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