bokeh 4th: server

how to write simple bokeh program that runs on a server?

bokeh server

  • from bokeh.io import curdoc
    • Create plots and widgets
    • add callbacks
    • arrange plots and widges
  • curdoc().add_root(layout)

how to run server

  • at the shell
    • bokeh serve --show myappname.py
    • if project is big that has many files
      • bokeh serve --show myappnamedir/
In [1]:
from bokeh.io import curdoc

import numpy as np
from bokeh.layouts import gridplot
from bokeh.plotting import figure, show, output_file, output_notebook

# output_notebook()

x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)

TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select"

p1 = figure(title="Legend Example", tools=TOOLS)

p1.circle(x,   y, legend="sin(x)")
p1.circle(x, 2*y, legend="2*sin(x)", color="orange")
p1.circle(x, 3*y, legend="3*sin(x)", color="green")

p2 = figure(title="Another Legend Example", tools=TOOLS)

p2.circle(x, y, legend="sin(x)")
p2.line(x, y, legend="sin(x)")

p2.line(x, 2*y, legend="2*sin(x)", line_dash=(4, 4), line_color="orange", line_width=2)

p2.square(x, 3*y, legend="3*sin(x)", fill_color=None, line_color="green")
p2.line(x, 3*y, legend="3*sin(x)", line_color="green")

# output_file("legend.html", title="legend.py example")

# show(gridplot(p1, p2, ncols=2, plot_width=400, plot_height=400))  # open a browser

photos=gridplot(p1, p2, ncols=2, plot_width=400, plot_height=400)

#show be deployed 
curdoc().add_root(photos)
output_notebook()
show(photos)
Loading BokehJS ...

copy all the code into a .py file, name it as test.py

in the command line type

bokeh serve --show test.py

In [2]:
# !bokeh serve --show test.py

add a single Slider

  • interactive
  • deploy sidebar on widgebox
In [3]:
from bokeh.models import Slider
from bokeh.layouts import widgetbox 
In [4]:
slider0 = Slider(title='my slider', start=0, end=10, step=0.1, value=2)
layout = widgetbox(slider0)
In [5]:
curdoc().add_root(layout)
show(layout)

connecting Sliders to Plots

callback function

callback(attr, old, new):

  • add callback to slider
    • slider1.on_change('value, callback)
In [6]:
from bokeh.io import curdoc
from bokeh.models import Slider


from numpy.random import random
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.layouts import column, row

try embed html to notebook -> interactive

In [7]:
# from IPython.display import display, HTML
# from bokeh.embed import file_html
# from bokeh.resources import JSResources
In [8]:
N=300
data_column = ColumnDataSource(data={'x':random(N),'y':random(N)})

plot=figure(plot_width=400, plot_height=400)
plot.circle(x='x', y='y',source=data_column)

slider1=Slider(start=100,end=1000,value=N,step=10, title='No. of points')

# add callback to widgets
def callback(attr, old, new):
    N=slider1.value
    data_column.data={'x':random(N), 'y':random(N)}
    
slider1.on_change('value',callback)


# layout=column(widgetbox(slider1),plot)

# Arrange plots and widgets in layouts
layout=column(slider1, plot)


# js_resources = JSResources(mode='inline')   
# trying=file_html(layout, js_resources, 'trying interactive')



curdoc().add_root(layout)
show(layout)

# only on server can be interactive

updating plots from dropdown menus

from bokeh.models import Select

callback func can be named any, paras can be named any as well

In [10]:
from bokeh.models import Select, ColumnDataSource
from numpy.random import random, normal, lognormal

from bokeh.io import curdoc    
from bokeh.layouts import column
from bokeh.plotting import figure
In [11]:
N=1000
data_source=ColumnDataSource(data={'x':random(N),'y':random(N)})

plot=figure(plot_width=400, plot_height=400)
plot.circle('x','y',source=data_source,color='green')

menu=Select(options=['uniform','normal','lognormal'],
           value='uniform',title='Distribution')

def wtf(a_ttribute, b_old, c_new):
    if menu.value == 'uniform': f = random
    elif menu.value == 'normal': f=normal
    else: f=lognormal
    data_source.data={'x':f(size=N),'y':f(size=N)}

menu.on_change('value',wtf)

layout=column(menu,plot)


curdoc().add_root(layout)
show(layout)
In [12]:
# !bokeh serve --show menu.py

how to use a dropdown callback to update another dropdown's options.

In [13]:
from bokeh.models import Select

from bokeh.io import curdoc
from bokeh.layouts import column, widgetbox

# Create two dropdown Select widgets: select1, select2
select1 = Select(title='First', options=['A', 'B'], value='A')
select2 = Select(title='Second', options=['1', '2', '3'], value='1')

# Define a callback function: callback
def callback(attr, old, new):
    # If select1 is 'A' 
    if select1.value == 'A':
        # Set select2 options to ['1', '2', '3']
        select2.options =  ['1', '2', '3']

        # Set select2 value to '1'
        select2.value = '1'
    else:
        # Set select2 options to ['100', '200', '300']
        select2.options = ['100', '200', '300']

        # Set select2 value to '100'
        select2.value = '100'

# Attach the callback to the 'value' property of select1
select1.on_change('value', callback)

# Create layout and add to current document
layout = widgetbox(select1, select2)

curdoc().add_root(layout)
show(layout)

Bottons

  • Botton callbacks
    • library winsound
      • winsound.PlaySound
PlaySound() name Corresponding Control Panel Sound name
'SystemAsterisk' Asterisk
'SystemExclamation' Exclaation
'SystemExit' Exit Windows
'SystemHand' Critical Stop
'SystemQuestion' Question
In [14]:
from bokeh.models import Button
from bokeh.io import curdoc
In [15]:
butt=Button(label='press! u dare')

def update0():
    import winsound
    winsound.PlaySound('SystemExit',winsound.SND_ALIAS)
    winsound.PlaySound('SystemExit',winsound.SND_ALIAS)
    winsound.PlaySound('SystemExit',winsound.SND_ALIAS)
    winsound.PlaySound('SystemExit',winsound.SND_ALIAS)

butt.on_click(update0)
In [16]:
curdoc().add_root(butt)
show(butt)
In [17]:
# !bokeh serve --show fuck.py

Button types

In [18]:
from bokeh.models import CheckboxGroup, RadioGroup, Toggle
In [19]:
from bokeh.models import CheckboxGroup, RadioGroup, Toggle
toggle = Toggle(label='Some on/off', button_type='success')
checkbox = CheckboxGroup(labels=['foo', 'bar', 'baz'])
radio = RadioGroup(labels=['2000', '2010', '2020'])
def callback(active):
    pass
#  Active tells which button is active

show(row(toggle, checkbox, radio))
In [ ]:
 

2 thoughts on “bokeh 4th: server

Leave a Reply

Your email address will not be published.

Notice: Undefined index: cookies in /var/www/html/wp-content/plugins/live-composer-page-builder/modules/tp-comments-form/module.php on line 1638