#-----------------------------------------------------------------------------# 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 runningthe code from Python script (``.py``) files.This handler is configured with the filename of a Python module. When aBokeh application calls ``modify_doc``, the contents of the module are run toprocess a new Document for a session. When the script code is executed, theDocument being modified will be available as ``curdoc``, and any optionallyprovided ``args`` will be available as ``sys.argv``.As an example, consider the following Python module ``myapp.py``.. code-block:: python # myapp.py import sys from bokeh.io import cudoc from bokeh.plotting import figure p = figure(x_range=(10, 10), y_range=(10, 10), title=sys.argv[1]) curdoc().add_root(p)The a ``ScriptHandler`` configured with this script will modify new BokehDocuments by adding an empty plot with a title taken from ``args``.'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsfromtypesimportModuleType# Bokeh importsfrom...core.typesimportPathLikefrom.codeimportCodeHandler#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('ScriptHandler',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------
[docs]classScriptHandler(CodeHandler):''' Modify Bokeh documents by executing code from Python scripts. '''_logger_text="%s: call to %s() ignored when running scripts with the 'bokeh' command."_origin="Script"
[docs]def__init__(self,*,filename:PathLike,argv:list[str]=[],package:ModuleType|None=None)->None:''' Keywords: filename (str) : a path to a Python source (".py") file '''withopen(filename,encoding='utf-8')asf:source=f.read()super().__init__(source=source,filename=filename,argv=argv,package=package)