Source code for bokeh.application.handlers.server_lifecycle
#-----------------------------------------------------------------------------# Copyright (c) 2012 - 2022, Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------''' Bokeh Application Handler to look for Bokeh server lifecycle callbacksin a specified Python module.'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsimportosfromtypesimportModuleTypefromtypingimportList# Bokeh importsfrom...core.typesimportPathLikefrom...util.callback_managerimport_check_callbackfrom.code_runnerimportCodeRunnerfrom.lifecycleimportLifecycleHandler#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('ServerLifecycleHandler',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------
[docs]classServerLifecycleHandler(LifecycleHandler):''' Load a script which contains server lifecycle callbacks. .. autoclasstoc:: '''
[docs]def__init__(self,*,filename:PathLike,argv:List[str]=[],package:ModuleType|None=None)->None:''' Keyword Args: filename (str) : path to a module to load lifecycle callbacks from argv (list[str], optional) : a list of string arguments to use as ``sys.argv`` when the callback code is executed. (default: []) '''super().__init__()withopen(filename,'r',encoding='utf-8')asf:source=f.read()self._runner=CodeRunner(source,filename,argv,package=package)ifnotself._runner.failed:# unlike ScriptHandler, we only load the module one timeself._module=self._runner.new_module()defextract_callbacks()->None:contents=self._module.__dict__if'on_server_loaded'incontents:self._on_server_loaded=contents['on_server_loaded']if'on_server_unloaded'incontents:self._on_server_unloaded=contents['on_server_unloaded']if'on_session_created'incontents:self._on_session_created=contents['on_session_created']if'on_session_destroyed'incontents:self._on_session_destroyed=contents['on_session_destroyed']_check_callback(self._on_server_loaded,('server_context',),what="on_server_loaded")_check_callback(self._on_server_unloaded,('server_context',),what="on_server_unloaded")_check_callback(self._on_session_created,('session_context',),what="on_session_created")_check_callback(self._on_session_destroyed,('session_context',),what="on_session_destroyed")self._runner.run(self._module,extract_callbacks)
# Properties --------------------------------------------------------------@propertydeferror(self)->str|None:''' If the handler fails, may contain a related error message. '''returnself._runner.error@propertydeferror_detail(self)->str|None:''' If the handler fails, may contain a traceback or other details. '''returnself._runner.error_detail@propertydeffailed(self)->bool:''' ``True`` if the lifecycle callbacks failed to execute '''returnself._runner.failed# Public methods ----------------------------------------------------------
[docs]defurl_path(self)->str|None:''' The last path component for the basename of the path to the callback module. '''ifself.failed:returnNoneelse:# TODO should fix invalid URL charactersreturn'/'+os.path.splitext(os.path.basename(self._runner.path))[0]