Bokeh Apps

In the Introduction , we showed how to use parambokeh, using the Jupyter notebook to host our example. However, parambokeh widgets can also be used in other contexts. Here we show how parambokeh can be used in a bokeh server app.

We'll create a simple bokeh app that displays a log of every time a button has been pressed:

screenshot of simple app

In a python script, examples/app/bokeh/simple.py , we first declare a sample Parameterized class to use as a demonstration object:

# existing parameterized class

import param
import datetime as dt

class Example(param.Parameterized):
    """Example Parameterized class"""
    log = []
    x            = param.Number(default=1.0,bounds=(0,100),precedence=0,doc="X position")
    write_to_log = param.Action(lambda obj: obj.log.append((dt.datetime.now(),obj.x)), 
                                doc="""Record value of x and timestamp.""",precedence=1)

Whenever Example.write_to_log is called, the current time and value of x are stored in Example.log .

We now create a properties frame, just as in the Introduction , but this time specifying mode='server' :

# create a properties frame for Example

import parambokeh
w = parambokeh.Widgets(Example, mode='server')

Finally, we write a simple bokeh app that periodically updates a display showing the value of Example.log :

# display value of Example.log in bokeh app

from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import Div

log = Div()

def update_log():
    log.text = "<br />".join(["%s -- %s"%(t[0].strftime('%H:%M:%S.%f'),t[1]) for t in Example.log])

curdoc().add_periodic_callback(update_log, 200)

layout = layout([log])
curdoc().add_root(layout)
curdoc().title = "simple parambokeh + bokeh server example"

The app can be launched using bokeh serve simple.py .


The example code used here is in examples/app/bokeh/simple.py . For reference, the entire file is reproduced below:

##### existing parameterized class

import param
import datetime as dt

class Example(param.Parameterized):
    """Example Parameterized class"""
    log = []
    x            = param.Number(default=1.0,bounds=(0,100),precedence=0,doc="X position")
    write_to_log = param.Action(lambda obj: obj.log.append((dt.datetime.now(),obj.x)), 
                                doc="""Record value of x and timestamp.""",precedence=1)

##### create a properties frame for Example

import parambokeh
w = parambokeh.Widgets(Example, mode='server')


##### display value of Example.log in bokeh app

from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import Div

log = Div()

def update_log():
    log.text = "<br />".join(["%s -- %s"%(t[0].strftime('%H:%M:%S.%f'),t[1]) for t in Example.log])

curdoc().add_periodic_callback(update_log, 200)

layout = layout([log])
curdoc().add_root(layout)
curdoc().title = "simple parambokeh + bokeh server example"