function _eval_panel
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.
/tf/active/vicechatdev/patches/server.py
72 - 83
moderate
Purpose
This function serves as a unified entry point for processing different types of Panel components (functions, templates, or panel objects) and integrating them into a Bokeh document. It handles the evaluation of callable panels, distinguishes between BaseTemplate instances and regular panels, and ensures proper document modification with server context. This is typically used internally by Panel's server infrastructure to prepare components for rendering.
Source Code
def _eval_panel(panel, server_id, title, location, doc):
from ..template import BaseTemplate
from ..pane import panel as as_panel
with set_curdoc(doc):
if isinstance(panel, (FunctionType, MethodType)):
panel = panel()
if isinstance(panel, BaseTemplate):
doc = panel._modify_doc(server_id, title, doc, location)
else:
doc = as_panel(panel)._modify_doc(server_id, title, doc, location)
return doc
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
panel |
- | - | positional_or_keyword |
server_id |
- | - | positional_or_keyword |
title |
- | - | positional_or_keyword |
location |
- | - | positional_or_keyword |
doc |
- | - | positional_or_keyword |
Parameter Details
panel: The panel component to evaluate. Can be a FunctionType/MethodType (which will be called to get the actual panel), a BaseTemplate instance, or any other panel object that can be converted via as_panel(). This is the main content to be rendered.
server_id: A unique identifier for the server instance. This is passed to the _modify_doc method to track which server is handling the panel, useful for multi-server scenarios.
title: The title to be used for the document/page. This will be set as the browser tab title or document title when the panel is rendered.
location: Location information for the panel, typically used for routing and URL handling. This helps the panel understand its position in the application's URL structure.
doc: The Bokeh Document object that will contain the panel. This is the core Bokeh document that manages the application state and communication with the browser.
Return Value
Returns the modified Bokeh Document object after the panel has been integrated into it. The document will contain all the necessary models, callbacks, and state from the evaluated panel, ready to be served to a client.
Dependencies
bokehparampanel
Required Imports
from types import FunctionType
from types import MethodType
from bokeh.io.doc import set_curdoc
Conditional/Optional Imports
These imports are only needed under specific conditions:
from ..template import BaseTemplate
Condition: Required when this function is used within the Panel package structure to handle template instances
Required (conditional)from ..pane import panel as as_panel
Condition: Required when this function is used within the Panel package structure to convert objects to panels
Required (conditional)Usage Example
from bokeh.document import Document
from bokeh.io.doc import set_curdoc
from panel.template import BaseTemplate
from panel.pane import panel as as_panel
from types import FunctionType, MethodType
# Example 1: Using with a function that returns a panel
def my_panel_func():
import panel as pn
return pn.pane.Markdown('# Hello World')
doc = Document()
result_doc = _eval_panel(
panel=my_panel_func,
server_id='server_123',
title='My Application',
location=None,
doc=doc
)
# Example 2: Using with a template instance
import panel as pn
template = pn.template.FastListTemplate(title='My App')
doc2 = Document()
result_doc2 = _eval_panel(
panel=template,
server_id='server_456',
title='Template App',
location=None,
doc=doc2
)
# Example 3: Using with a direct panel object
panel_obj = pn.pane.Markdown('# Direct Panel')
doc3 = Document()
result_doc3 = _eval_panel(
panel=panel_obj,
server_id='server_789',
title='Direct Panel App',
location=None,
doc=doc3
)
Best Practices
- Always ensure the 'doc' parameter is a valid Bokeh Document instance before calling this function
- The function modifies the document in-place within the set_curdoc context, so be aware of side effects
- When passing a function or method as 'panel', ensure it returns a valid panel object or template
- This function is typically used internally by Panel's server infrastructure and should not be called directly in user code unless building custom server implementations
- The set_curdoc context manager ensures proper document context during evaluation, which is critical for callbacks and state management
- Handle both BaseTemplate and regular panel objects differently as they have different _modify_doc signatures and behaviors
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function init_doc 69.1% similar
-
class Application 63.8% similar
-
function create_review_panel 60.8% similar
-
function create_approval_panel 60.8% similar
-
function modify_document 59.5% similar