Regis' Blog

L'informatique, L'amour, Les vaches

Log into a file with Bokeh
December 09, 2017
in Datascience |
Share this post
| |
Log into a file with Bokeh

Bokeh is a very powerfull project from PyData for displaying datavisualisation.

Bokeh is easy to use and fast to install when you are a datascientist... but not when you are a developper in charge of the project industrialization.

The first requirement when you are the project developper is to log somewhere and the datascientist say:

"OK dude! this is my project, just type bokeh serve document and have fun".

Fun? There's no fun. The script (the bokeh document) is not testable, is not well design as often a negatif score with pylint.

Well but how to log this into a file. By default, Bokeh display its log on the stderr. And there's no obvious way to redirect those log into a file.

The solution is to create an Bokeh application and before, set the redirection.

import logging

from bokeh.application import Application  
from bokeh.application.handlers import FunctionHandler  
from bokeh.server.server import Server  
from bokeh.util import logconfig

# Set you logging here
logconfig.basicConfig(level=logging.INFO, filename='the_log_file.log')  
logger = logging.getLogger('THE_LOGGER')


def make_doc(doc):  
    """
    This is where you create the document. in a document
    'doc' is a function : curdoc()
    """
    logger.info('This is a logging comment')
    doc.title = "Hello world"

    return doc


if __name__ == '__main__':  
    app = Application(FunctionHandler(make_doc))
    server = Server({'/test': app}, port=5000)
    server.start()
    server.run_until_shutdown()

Now bokeh log into a file.

You can download a sample project on my github