Using html template when starting bokeh server through python

Hi!

I have an app that makes use of an html template, index.html, which is stored in the app directory my_app/templates. If I start my app with bokeh serve /path/to/my_app (or whatever relative path is appropriate) it works fine; the template is used automatically. If I start the app by creating an instance of bokeh.server.server.Server in python and then running it, the template is not detected. In my app:

doc = Application(FunctionHandler(self.make_document))
apps = {'/': doc}
server = Server(apps, port=port)
server.show('/')

That is, the app starts up just fine and works, but the functionality that relies on some custom stuff in the template doesn’t work. Is there some way to pass to Server that the html template should be used or is there something else to make this work?

bokeh serve uses DirectoryHandler which, in turn, uses the templates directory: https://github.com/bokeh/bokeh/blob/be6326f1293f94a61110c8487af19458a2ce1667/bokeh/application/handlers/directory.py#L79

You can take a look at the build_single_handler_application to see how it uses DirectoryHandler: https://github.com/bokeh/bokeh/blob/996e57f8491f25b4f51b0a93b73f1145bb91f599/bokeh/command/util.py#L80

Alternatively, you can just write an arbitrary handler for the '/' route that serves whatever HTML you want and pass that handler with the route to the extra_patterns argument of the Server class.

1 Like

That pointed me in the right direction; all I needed to do was set doc.template to the jinja template of index.html

Thanks!

ps: I realize now the naming scheme in the code in my initial piece of code is confusing since doc is not an instance of Document. In de method self.make_document in my piece of code, an instance of Document (i.e. doc) is modified, so there I could set doc.template = template with template being the jinja template.