Bokeh 简明教程

Bokeh - Server

Bokeh 架构采用分离设计,其中使用 Python 创建诸如绘图和符号之类的对象,并将其转换为 JSON 供 BokehJS client library 使用。

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 client library.

但是,可以在 Python 和浏览器中保持对象彼此同步,借助 Bokeh Server 。它允许响应浏览器中生成的带有 Python 全部功能的用户界面 (UI) 事件。它还有助于自动将服务器端更新推送到浏览器中的小部件或绘图。

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.

Bokeh 服务器使用用 Python 编写的应用程序代码来创建 Bokeh 文档。来自客户端浏览器的每次新连接都会导致 Bokeh 服务器创建新文档,仅适用于该会话。

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

server

首先,我们必须开发要在客户端浏览器中提供的应用程序代码。下面的代码呈现正弦波线形符号。除了绘图之外,还呈现了一个滑块控件来控制正弦波的频率。回调函数 update_data() 更新 ColumnDataSource 数据,将滑块的瞬时值作为当前频率。

First, we have to develop an application code to be served to client browser. Following code renders a sine wave line glyph. Along with the plot, a slider control is also rendered to control the frequency of sine wave. The callback function update_data() updates ColumnDataSource data taking the instantaneous value of slider 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 Slider, TextInput
from bokeh.plotting import figure
N = 200
x = np.linspace(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.line('x', 'y', source = source, line_width = 3, line_alpha = 0.6)
freq = Slider(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.linspace(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 = "Sliders"

接下来,按以下命令行启动 Bokeh 服务器:

Next, start Bokeh server by following command line −

Bokeh serve –show sliders.py

Bokeh 服务器开始在 localhost:5006/sliders 处运行并为应用程序提供服务。控制台日志显示以下显示:

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

C:\Users\User>bokeh serve --show scripts\sliders.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/sliders
2019-09-29 00:21:35,875 Starting Bokeh server with process id: 3776
2019-09-29 00:21:37,330 200 GET /sliders (::1) 699.99ms
2019-09-29 00:21:38,033 101 GET /sliders/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

你可以滚动滑块将频率尝试更改为 2。

You can try and change the frequency to 2 by rolling the slider.

frequency