function init_doc
Initializes a Bokeh document by registering session information and setting up document lifecycle event handlers for Panel applications.
/tf/active/vicechatdev/patches/server.py
377 - 391
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
bokehdatetimeparam
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function _initialize_session_info 73.0% similar
-
class Application 69.7% similar
-
function _eval_panel 69.1% similar
-
function set_curdoc 64.7% similar
-
function modify_document 63.7% similar