Source code for bokeh.application.handlers.function
#-----------------------------------------------------------------------------# Copyright (c) Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------''' Provide a Bokeh Application Handler to build up documents by runninga specified Python function.This Handler is not used by the Bokeh server command line tool, but is oftenuseful if users wish to embed the Bokeh server programmatically:.. code-block:: python 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 :bokeh-tree:`examples/server/api`'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsfromtypingimportCallable# Bokeh importsfrom...documentimportDocumentfrom...util.callback_managerimport_check_callbackfrom.handlerimportHandler,handle_exception#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('FunctionHandler',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------ModifyDoc=Callable[[Document],None]
[docs]classFunctionHandler(Handler):''' 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: .. code-block:: python 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. '''_func:ModifyDoc_trap_exceptions:bool_safe_to_fork:bool
[docs]def__init__(self,func:ModifyDoc,*,trap_exceptions:bool=False)->None:''' Args: func (callable) : a function to modify and return a Bokeh Document. The function should have the form: .. code-block:: python 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 '''super().__init__()_check_callback(func,('doc',))self._func=funcself._trap_exceptions=trap_exceptionsself._safe_to_fork=True
# Properties --------------------------------------------------------------@propertydefsafe_to_fork(self)->bool:''' Whether it is still safe for the Bokeh server to fork new workers. ``False`` if ``modify_doc`` has already been called. '''returnself._safe_to_fork# Public methods ----------------------------------------------------------
[docs]defmodify_document(self,doc:Document)->None:''' Execute the configured ``func`` to modify the document. After this method is first executed, ``safe_to_fork`` will return ``False``. '''try:self._func(doc)exceptExceptionase:ifself._trap_exceptions:handle_exception(self,e)else:raisefinally:self._safe_to_fork=False