bokeh.application.handlers.handler

Provide a base class for Bokeh Application handler classes.

When a Bokeh server session is initiated, the Bokeh server asks the Application for a new Document to service the session. To do this, the Application first creates a new empty Document, then it passes this new Document to the modify_document method of each of its handlers. When all handlers have updated the Document, it is used to service the user session.

Below is an example outline of a custom handler that might modify documents based off information in some database:

class DatabaseHandler(Handler):
    """ A Bokeh Application handler to initialize Documents from a database

    """

    def modify_document(self, doc: Document) -> None:
        # do some data base lookup here to generate 'plot'

        # add the plot to the document (i.e modify the document)
        doc.add_root(plot)
class Handler[source]

Provide a mechanism for Bokeh applications to build up new Bokeh Documents.

Public Data Attributes:

error

If the handler fails, may contain a related error message.

error_detail

If the handler fails, may contain a traceback or other details.

failed

True if the handler failed to modify the doc

safe_to_fork

Public Methods:

__init__()

modify_document(doc)

Modify an application document in a specified manner.

on_server_loaded(server_context)

Execute code when the server is first started.

on_server_unloaded(server_context)

Execute code when the server cleanly exits.

on_session_created(session_context)

Execute code when a new session is created.

on_session_destroyed(session_context)

Execute code when a session is destroyed.

process_request(request)

Processes incoming HTTP request returning a dictionary of additional data to add to the session_context.

static_path()

Return a path to app-specific static resources, if applicable.

url_path()

Returns a default URL path, if applicable.


modify_document(doc: bokeh.document.document.Document) None[source]

Modify an application document in a specified manner.

When a Bokeh server session is initiated, the Bokeh server asks the Application for a new Document to service the session. To do this, the Application first creates a new empty Document, then it passes this Document to the modify_document method of each of its handlers. When all handlers have updated the Document, it is used to service the user session.

Subclasses must implement this method

Parameters

doc (Document) – A Bokeh Document to update in-place

Returns

Document

on_server_loaded(server_context: bokeh.application.application.ServerContext) None[source]

Execute code when the server is first started.

Subclasses may implement this method to provide for any one-time initialization that is necessary after the server starts, but before any sessions are created.

Parameters

server_context (ServerContext) –

on_server_unloaded(server_context: bokeh.application.application.ServerContext) None[source]

Execute code when the server cleanly exits. (Before stopping the server’s IOLoop.)

Subclasses may implement this method to provide for any one-time tear down that is necessary before the server exits.

Parameters

server_context (ServerContext) –

Warning

In practice this code may not run, since servers are often killed by a signal.

async on_session_created(session_context: bokeh.application.application.SessionContext) None[source]

Execute code when a new session is created.

Subclasses may implement this method to provide for any per-session initialization that is necessary before modify_doc is called for the session.

Parameters

session_context (SessionContext) –

async on_session_destroyed(session_context: bokeh.application.application.SessionContext) None[source]

Execute code when a session is destroyed.

Subclasses may implement this method to provide for any per-session tear-down that is necessary when sessions are destroyed.

Parameters

session_context (SessionContext) –

process_request(request: tornado.httputil.HTTPServerRequest) Dict[str, Any][source]

Processes incoming HTTP request returning a dictionary of additional data to add to the session_context.

Parameters

request – HTTP request

Returns

A dictionary of JSON serializable data to be included on the session context.

static_path() str | None[source]

Return a path to app-specific static resources, if applicable.

url_path() str | None[source]

Returns a default URL path, if applicable.

Handlers subclasses may optionally implement this method, to inform the Bokeh application what URL it should be installed at.

If multiple handlers specify url_path the Application will use the value from the first handler in its list of handlers.

property error: str | None

If the handler fails, may contain a related error message.

property error_detail: str | None

If the handler fails, may contain a traceback or other details.

property failed: bool

True if the handler failed to modify the doc