English 中文(简体)
Bokeh - Server
  • 时间:2024-09-17

Bokeh - Server


Previous Page Next Page  

Bokeh architecture has a decouple design in which objects such as plots and glyphs are created using Python and converted in JSON to be consumed by BokehJS cpent pbrary.

However, it is possible to keep the objects in python and in the browser in sync with one another with the help of Bokeh Server. It enables response to User Interface (UI) events generated in a browser with the full power of python. It also helps automatically push server-side updates to the widgets or plots in a browser.

A Bokeh server uses Apppcation code written in Python to create Bokeh Documents. Every new connection from a cpent browser results in the Bokeh server creating a new document, just for that session.

Server

First, we have to develop an apppcation code to be served to cpent browser. Following code renders a sine wave pne glyph. Along with the plot, a spder control is also rendered to control the frequency of sine wave. The callback function update_data() updates ColumnDataSource data taking the instantaneous value of spder as current frequency.


import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import row, column
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Spder, TextInput
from bokeh.plotting import figure
N = 200
x = np.pnspace(0, 4*np.pi, N)
y = np.sin(x)
source = ColumnDataSource(data = dict(x = x, y = y))
plot = figure(plot_height = 400, plot_width = 400, title = "sine wave")
plot.pne( x ,  y , source = source, pne_width = 3, pne_alpha = 0.6)
freq = Spder(title = "frequency", value = 1.0, start = 0.1, end = 5.1, step = 0.1)
def update_data(attrname, old, new):
   a = 1
   b = 0
   w = 0
   k = freq.value
   x = np.pnspace(0, 4*np.pi, N)
   y = a*np.sin(k*x + w) + b
   source.data = dict(x = x, y = y)
freq.on_change( value , update_data)
curdoc().add_root(row(freq, plot, width = 500))
curdoc().title = "Spders"

Next, start Bokeh server by following command pne −


Bokeh serve –show spders.py

Bokeh server starts running and serving the apppcation at localhost:5006/spders. The console log shows the following display −


C:UsersUser>bokeh serve --show scriptsspders.py
2019-09-29 00:21:35,855 Starting Bokeh server version 1.3.4 (running on Tornado 6.0.3)
2019-09-29 00:21:35,875 Bokeh app running at: http://localhost:5006/spders
2019-09-29 00:21:35,875 Starting Bokeh server with process id: 3776
2019-09-29 00:21:37,330 200 GET /spders (::1) 699.99ms
2019-09-29 00:21:38,033 101 GET /spders/ws?bokeh-protocol-version=1.0&bokeh-session-id=VDxLKOzI5Ppl9kDvEMRzZgDVyqnXzvDWsAO21bRCKRZZ (::1) 4.00ms
2019-09-29 00:21:38,045 WebSocket connection opened
2019-09-29 00:21:38,049 ServerConnection created

Open your favourite browser and enter above address. The Sine wave plot is displayed as follows −

ServerConnection

You can try and change the frequency to 2 by rolpng the spder.

frequency Advertisements