Bokeh-Server

The architecture of Bokeh is such that higher-level model objects (that is, representations such as plots, areas, axes, glyphs, etc.) are created in Python and then converted into a JSON format that is used by the BokehJS client library. With the help of the Bokeh server, the model objects in Python and in the browser can be synchronised with each other, creating powerful functions:

  • Browser events lead to server-side Python calculations or queries

  • Automatic push update of the browser UI (for example, widgets or plots)

  • Periodic, timeout and asynchronous callbacks for streaming updates

This function for synchronisation between server-side Python and the browser is the main purpose of the Bokeh server.

It is also possible to define Bokeh applications by creating a standard Python script. In this case, it is not necessary to create a function like modify_doc. Normally, the script simply creates all bokeh quotas and adds it to the document with one line:

curdoc().add_root(layout)

To try out the example below, copy the code into a file hello.py and then execute the following:

$ pipenv run python -m bokeh serve --show hello.py
[1]:
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models.widgets import Button, Paragraph, TextInput


# create some widgets
button = Button(label="Say Hi")
input = TextInput(value="Pythonistas")
output = Paragraph()


# add a callback to a widget
def update():
    output.text = "Hello, " + input.value + "!"


button.on_click(update)

# create a layout for everything
layout = column(button, input, output)

# add the layout to curdoc
curdoc().add_root(layout)