function server_html_page_for_session
Generates a complete HTML page for a Bokeh server session by bundling resources and rendering document roots with session token.
/tf/active/vicechatdev/patches/server.py
140 - 153
moderate
Purpose
This function creates a standalone HTML page that embeds a Bokeh server session. It takes a session object, bundles all necessary JavaScript/CSS resources, creates a render item with the session token, and generates the final HTML using a template. This is typically used for serving Bokeh applications through a web server, allowing the rendered page to maintain a connection to the Bokeh server session for interactive updates.
Source Code
def server_html_page_for_session(session, resources, title, template=BASE_TEMPLATE,
template_variables=None):
render_item = RenderItem(
token = session.token,
roots = session.document.roots,
use_for_title = False,
)
if template_variables is None:
template_variables = {}
bundle = bundle_resources(session.document.roots, resources)
return html_page_for_render_items(bundle, {}, [render_item], title,
template=template, template_variables=template_variables)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
session |
- | - | positional_or_keyword |
resources |
- | - | positional_or_keyword |
title |
- | - | positional_or_keyword |
template |
- | BASE_TEMPLATE | positional_or_keyword |
template_variables |
- | None | positional_or_keyword |
Parameter Details
session: A Bokeh server session object that contains the document with roots to render and a unique token for session identification. Must have 'token' and 'document' attributes where document has a 'roots' attribute.
resources: A Resources object or configuration specifying which JavaScript and CSS resources (Bokeh libraries, custom scripts, stylesheets) should be included in the HTML page. Used by bundle_resources to collect all necessary assets.
title: String value for the HTML page title that appears in the browser tab. This is the main title for the page.
template: A Jinja2 template string or template object used to generate the HTML structure. Defaults to BASE_TEMPLATE which provides the standard Bokeh page layout. Should contain placeholders for title, resources, and render items.
template_variables: Optional dictionary of additional variables to pass to the template for custom rendering. Can include any key-value pairs that the template expects. Defaults to an empty dictionary if None.
Return Value
Returns a string containing the complete HTML page markup. This includes the DOCTYPE, head section with all bundled resources (CSS/JS), body with the Bokeh document roots rendered, and embedded JavaScript for establishing the session connection. The HTML is ready to be served directly to a browser.
Dependencies
bokehparam
Required Imports
from bokeh.embed.elements import html_page_for_render_items
from bokeh.embed.util import RenderItem
from resources import BASE_TEMPLATE
from resources import bundle_resources
Usage Example
from bokeh.server.session import ServerSession
from bokeh.document import Document
from bokeh.plotting import figure
from resources import Resources, BASE_TEMPLATE, bundle_resources
from bokeh.embed.util import RenderItem
from bokeh.embed.elements import html_page_for_render_items
# Create a document with a plot
doc = Document()
p = figure(title='Example Plot')
p.line([1, 2, 3], [4, 5, 6])
doc.add_root(p)
# Create a session (simplified example)
class MockSession:
def __init__(self):
self.token = 'abc123'
self.document = doc
session = MockSession()
resources = Resources(mode='cdn')
# Generate HTML page
html_output = server_html_page_for_session(
session=session,
resources=resources,
title='My Bokeh App',
template=BASE_TEMPLATE,
template_variables={'custom_var': 'value'}
)
# Save or serve the HTML
with open('output.html', 'w') as f:
f.write(html_output)
Best Practices
- Ensure the session object is valid and has an active document with roots before calling this function
- Use appropriate resource modes (cdn, inline, server) based on deployment requirements
- Provide template_variables as a dictionary when using custom templates that require additional context
- The returned HTML should be served with appropriate Content-Type header (text/html)
- Session tokens should be kept secure as they provide access to the Bokeh server session
- Consider caching the bundled resources if generating multiple pages with the same resource configuration
- Ensure the Bokeh server is running and accessible when the generated HTML is loaded in a browser
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function autoload_js_script 62.4% similar
-
class DocHandler 60.4% similar
-
function _initialize_session_info 58.9% similar
-
function init_doc 58.4% similar
-
class Application 57.0% similar