I am using the following code to push a plot (actually, multiple plots in a single doc) to the user’s browser:
class SingleShotServer:
def __init__(self, log, plot, template=None, title=None, template_vars=None):
self.__log = log
self.__plot = plot
self.__template = template
self.__title = title
self.__template_vars = {} if template_vars is None else template_vars
self.__server = Server(self.__modify_doc)
self.__server.start()
self.__log.info('Opening Bokeh application on http://localhost:5006/')
self.__server.io_loop.add_callback(self.__server.show, "/")
self.__server.io_loop.call_later(5, self.__stop)
self.__server.io_loop.start()
def __modify_doc(self, doc):
doc.add_root(self.__plot)
doc.title = self.__title
# this extends bokeh.core.templates.FILE - see code in bokeh.embed.elements
doc.template = self.__template
doc.template_variables.update(self.__template_vars)
def __stop(self):
self.__log.info('Stopping server')
self.__server.io_loop.stop()
self.__server.stop()
self.__server.unlisten()
``
and it works nicely. Everything is good in the world - templating, variables, display. Both log messages are displayed.
BUT:
1 - It reconfigures Python’s logging so that I start to see a lot of output on stdout (or stderr). This is a disaster as my calling code is running inside an curses-like environment; the logging destroys my TUI.
2 - If I call the code a second time it fails with an error “Models must be owned by a single document XXX is already in a doc” where XXX seems to vary, but is what seems to be some kind of tool like zoom, etc. Sorry for the lack of detail here - I can’t cut + paste because the error message is appearing only in a terminal expecting curses (see above) - there is nothing in the logs I actually configured for the app (this is all related to point 1 above). The plots are “freshly” generated each time - no explicit state is re-used, that I can see.
So I guess I’d like to know how to stop Bokeh / Tornado taking over Python’s logs, and what I need to do in addition to the above to release resources on server shutdown?
Thanks,
Andrew

