bokeh Local bokeh server with console entry point


To allow a bokeh application to be executed like a normal .py file, you need to handle the tornado IOloop in your application, as described here. A standalone bokeh application like this can be used to implement a console script entry point in However, this requires bokeh version >= 0.12.4.

The bokeh application

Consider the file

from tornado.ioloop import IOLoop

from bokeh.application.handlers import FunctionHandler
from bokeh.application import Application
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.server.server import Server

def modify_doc(doc):
    """Add a plotted function to the document.

        doc: A bokeh document to which elements can be added.
    x_values = range(10)
    y_values = [x ** 2 for x in x_values]
    data_source = ColumnDataSource(data=dict(x=x_values, y=y_values))
    plot = figure(title="f(x) = x^2",
    plot.line('x', 'y', source=data_source, line_width=3, line_alpha=0.6)
    doc.title = "Test Plot"

def main():
    """Launch the server and connect to it.
    print("Preparing a bokeh application.")
    io_loop = IOLoop.current()
    bokeh_app = Application(FunctionHandler(modify_doc))

    server = Server({"/": bokeh_app}, io_loop=io_loop)
    print("Opening Bokeh application on http://localhost:5006/")

    io_loop.add_callback(, "/")


This file can be executed

$ python

which run the server and automatically launch a browser to show the document.

Entry points and the

In order to provide a script that can be easily installed and called using the Consider the following folder structure:

└── my_package

Content of

from setuptools import setup

    name = "my_package",
        "console_scripts": ["my_script = my_package.local_server:main"],

When installing the package using

$ python install

you can then use the call

$ my_script

to launch the bokeh application and automatically start a browser displaying the document.