bokeh.application.handlers.function

Provide a Bokeh Application Handler to build up documents by running a specified Python function.

This Handler is not used by the Bokeh server command line tool, but is often useful if users wish to embed the Bokeh server programmatically:

def make_doc(doc: Document):
    # do work to modify the document, add plots, widgets, etc.
    return doc

app = Application(FunctionHandler(make_doc))

server = Server({'/bkapp': app}, io_loop=IOLoop.current())
server.start()

For complete examples of this technique, see examples/howto/server_embed

class FunctionHandler(func: Callable[[bokeh.document.document.Document], None], *, trap_exceptions: bool = False)[source]

A Handler that accepts a plain python function to use for modifying Bokeh Documents.

For example, the following code configures a handler with a function that adds an empty plot to a Document:

def add_empty_plot(doc: Document):
    p = figure(x_range=(0, 10), y_range=(0, 10))
    doc.add_root(p)
    return doc

handler = FunctionHandler(add_empty_plot)

This handler could be configured on an Application, and the Application would run this function every time a new session is created.

Public Data Attributes:

safe_to_fork

Whether it is still safe for the Bokeh server to fork new workers.

Inherited from Handler

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

Whether it is still safe for the Bokeh server to fork new workers.

Public Methods:

__init__(func, *[, trap_exceptions])

param func

a function to modify and return a Bokeh Document.

modify_document(doc)

Execute the configured func to modify the document.

Inherited from Handler

__init__(func, *[, trap_exceptions])

param func

a function to modify and return a Bokeh Document.

modify_document(doc)

Execute the configured func to modify the document.

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.


__init__(func: Callable[[bokeh.document.document.Document], None], *, trap_exceptions: bool = False) None[source]
Parameters
  • func (callable) –

    a function to modify and return a Bokeh Document. The function should have the form:

    def func(doc: Document):
        # modify doc
        return doc
    

    and it should return the passed-in document after making any modifications in-place.

  • trap_exceptions (bool) – should exceptions in func be caught and logged or allowed to propagate

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

Execute the configured func to modify the document.

After this method is first executed, safe_to_fork will return False.

property safe_to_fork: bool

Whether it is still safe for the Bokeh server to fork new workers.

False if modify_doc has already been called.