Source code for bokeh.server.views.autoload_js_handler
#-----------------------------------------------------------------------------# 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 request handler that returns a page displaying a document.'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsfromurllib.parseimporturlparse# Bokeh importsfrombokeh.core.templatesimportAUTOLOAD_JSfrombokeh.embed.bundleimportScript,bundle_for_objs_and_resourcesfrombokeh.embed.elementsimportscript_for_render_itemsfrombokeh.embed.utilimportRenderItem# Bokeh importsfrom.session_handlerimportSessionHandler#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('AutoloadJsHandler',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------
[docs]classAutoloadJsHandler(SessionHandler):''' Implements a custom Tornado handler for the autoload JS chunk '''
asyncdefget(self,*args,**kwargs):ifself.request.cookiesand"Origin"inself.request.headers:# If credentials, i.e. cookies, are sent with the request,# we cannot leave the allowed origin as wildcard "*",# but have to make it explicit.self.set_header("Access-Control-Allow-Origin",self.request.headers["Origin"])session=awaitself.get_session()element_id=self.get_argument("bokeh-autoload-element",default=None)ifnotelement_id:self.send_error(status_code=400,reason='No bokeh-autoload-element query parameter')returnapp_path=self.get_argument("bokeh-app-path",default="/")absolute_url=self.get_argument("bokeh-absolute-url",default=None)ifabsolute_url:uri=urlparse(absolute_url)server_url=f"{uri.scheme}://{uri.netloc}"else:server_url=Noneresources_param=self.get_argument("resources","default")resources=self.application.resources(server_url)ifresources_param!="none"elseNonebundle=bundle_for_objs_and_resources(None,resources)render_items=[RenderItem(token=session.token,elementid=element_id,use_for_title=False)]bundle.add(Script(script_for_render_items({},render_items,app_path=app_path,absolute_url=absolute_url)))js=AUTOLOAD_JS.render(bundle=bundle,elementid=element_id)self.set_header("Content-Type",'application/javascript')self.write(js)
[docs]asyncdefoptions(self,*args,**kwargs):'''Browsers make OPTIONS requests under the hood before a GET request'''self.set_header('Access-Control-Allow-Methods','PUT, GET, OPTIONS')self.set_header("Access-Control-Allow-Origin",self.request.headers["Origin"])