配置 Blaze 并从 Pyramid 网络应用程序启动 Bokeh 服务器

Configure Blaze and start Bokeh server from Pyramid web-application

我有一个 Pyramid 网络应用程序,客户希望能够在该应用程序上以交互方式绘制大型数据集。

该应用程序当前使用 D3 显示具有缩放、平移、悬停等功能的客户所选数据的子集。但是,如果用户需要查看具有相同功能的完整集,我想使用 Bokeh 服务器并使用下采样。

我 运行 遇到麻烦的地方是下采样功能只能用于使用 ServerDataSource 的绘图。

理想情况下,Bokeh 服务器会不断 运行ning,我可以将客户选择的数据推送给它,然后将其用作下采样图的来源。但是,据我所知,Blaze 不允许我将数据推送到现有服务器。

相反,我想当用户请求绘图时,我可以使用 Pyramid 的视图之一修改 Blaze 配置文件,然后启动 Bokeh 服务器。

@view_config(route_name='startBokehServer', renderer = 'json',permission='view')
def startBokehServer_view(request):

    r"""

    Configures blaze_config.py file to create a dict containing customer's select reversal data
    Starts Bokeh server using modified blaze_config.py file at http://localhost:5006

    """
    bokeh_server.run()

一旦数据存储在服务器上,另一个视图将以 Bokeh 服务器作为数据源绘制曲线。

@view_config(route_name='fetchPlotDataRev', renderer = 'json',permission='view')
def fetchPlotDataRev_view(request):

  r"""

  Plots full reversal data using Bokeh's down-sampling method.

  """
    from bokeh.plotting import output_server, figure, show
    from bokeh.models import HoverTool, Range1d
    from bokeh.transforms import line_downsample      

    output_server("Full Reversal Curve(s)")
    c=bokeh_client('http://localhost:5006')
    d=bokeh_data(c)

    rev=d.rev

    source=line_downsample.source()
    source.from_blaze(rev,local=True)

    TOOLS="pan,wheel_zoom,box_zoom,reset,hover"
    p = figure(title="Reversal with tooltip", tools=TOOLS)

    p1=p.scatter(x='time',y='aa_cd',color='#0000ff',source=source)
    p1.select(dict(type=HoverTool)).tooltips=[("(x,y)", "($x, $y)"),]

    p2=p.scatter(x='time',y='aa_cv',color='#ff0000',source=source)
    p2.select(dict(type=HoverTool)).tooltips=[("(x,y)", "($x, $y)"),]

    xmin=float(rev.time.min())
    xmax=float(rev.time.max())
    ymin=float(rev.aa_cv.min())
    ymax=float(rev.aa_cd.max())

    p.x_range=Range1d(start=xmin,end=xmax)
    p.y_range=Range1d(start=ymin,end=ymax)        

    show(p)

最后,来自 JavaScript 的信号表明 Bokeh 服务器 window 已关闭,向另一个将停止服务器的视图发出请求。

@view_config(route_name='stopBokehServer', renderer = 'json',permission='view')
def stopBokehServer_view(request):

    r"""

    Stops Bokeh server.

    """
    bokeh_server.start.stop()

但是,应用程序在尝试服务 /startBokehServer 时以状态 2 退出。追溯包含在下面。

usage: pserve [-h] [--ip IP] [--port PORT] [--url-prefix URL_PREFIX]
      [-D BLAZE_CONFIG] [-m] [--script SCRIPT] [--backend BACKEND]
      [--redis-port REDIS_PORT] [--start-redis] [--no-start-redis]
      [--ws-conn-string WS_CONN_STRING] [-d] [--dev] [--filter-logs]
      [-j] [-s] [--robust-reload] [-v]
pserve: error: unrecognized arguments: development.ini
/home/katmeg/mat-cruncher/PyramidAnalysis
2015-04-09 12:20:53,730 ERROR [waitress][Dummy-11] Exception when serving /startBokehServer
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/waitress-0.8.9-py2.7.egg/waitress/channel.py", line 337, in service
task.service()
  File "/usr/local/lib/python2.7/dist-packages/waitress-0.8.9-py2.7.egg/waitress/task.py", line 173, in service
self.execute()
  File "/usr/local/lib/python2.7/dist-packages/waitress-0.8.9-py2.7.egg/waitress/task.py", line 392, in execute
app_iter = self.channel.server.application(env, start_response)
  File "/usr/local/lib/python2.7/dist-packages/pyramid-1.5.1-py2.7.egg/pyramid/router.py", line 242, in __call__
response = self.invoke_subrequest(request, use_tweens=True)
  File "/usr/local/lib/python2.7/dist-packages/pyramid-1.5.1-py2.7.egg/pyramid/router.py", line 217, in invoke_subrequest
response = handle_request(request)
  File "/usr/local/lib/python2.7/dist-packages/pyramid_debugtoolbar-2.3-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 178, in toolbar_tween
response = _handler(request)
  File "/usr/local/lib/python2.7/dist-packages/pyramid_debugtoolbar-2.3-py2.7.egg/pyramid_debugtoolbar/panels/performance.py", line 57, in resource_timer_handler
result = handler(request)
  File "/usr/local/lib/python2.7/dist-packages/pyramid-1.5.1-py2.7.egg/pyramid/tweens.py", line 21, in excview_tween
response = handler(request)
  File "/usr/local/lib/python2.7/dist-packages/pyramid-1.5.1-py2.7.egg/pyramid/router.py", line 163, in handle_request
response = view_callable(context, request)
  File "/usr/local/lib/python2.7/dist-packages/pyramid-1.5.1-py2.7.egg/pyramid/config/views.py", line 245, in _secured_view
return view(context, request)
  File "/usr/local/lib/python2.7/dist-packages/pyramid-1.5.1-py2.7.egg/pyramid/config/views.py", line 355, in rendered_view
result = view(context, request)
  File "/usr/local/lib/python2.7/dist-packages/pyramid-1.5.1-py2.7.egg/pyramid/config/views.py", line 501, in _requestonly_view
response = view(request)
  File "/home/katmeg/mat-cruncher/PyramidAnalysis/pyramidanalysis/views.py", line 649, in startBokehServer_view
bokeh_server.run()
  File "/home/katmeg/pyrEnv/local/lib/python2.7/site-packages/bokeh/server/__init__.py", line 134, in run
args = parser.parse_args(sys.argv[1:])
  File "/usr/lib/python2.7/argparse.py", line 1691, in parse_args
self.error(msg % ' '.join(argv))
  File "/usr/lib/python2.7/argparse.py", line 2347, in error
self.exit(2, _('%s: error: %s\n') % (self.prog, message))
  File "/usr/lib/python2.7/argparse.py", line 2335, in exit
_sys.exit(status)
SystemExit: 2

注意:当我从命令行 运行 bokeh-server 可执行文件然后从一个单独的创建和服务它们时,这些图按照我的预期工作Python 脚本。

所以我的问题如下: 我可以将数据推送到已经 运行ning 的 Bokeh 服务器,然后将其用作下采样的数据源吗? 如果没有,我怎样才能 start/stop 在 Pyramid 应用程序中请求 Bokeh 服务器?

在此先感谢您的帮助!

下面是应用程序如何动态配置服务器数据源,启动 Bokeh 服务器,并将请求的数据绘制在 Bokeh 服务器上以供客户导航。

要绘制的数据被传递到 startBokehServer 视图,在那里它被写入一个临时文本文件。然后使用 Python 的 subprocess 和一个配置文件启动 Bokeh 服务器,该文件读取文本文件并配置数据以便 Blaze 可以读取。

views.py

@view_config(route_name='startBokehServer',renderer = 'json',permission='view')
def startBokehServer_view(request):

    # plotDataRev is dictionary containing data requested from web page
    with open('pyramidanalysis/temp/reversal_data.txt','wb') as handle:
      pickle.dump(plotDataRev,handle)  
    bokeh_pid=str(subprocess.Popen(['/bokeh-server','-D','blaze_config.py'],stdout=subprocess.PIPE).pid)
    request.session['bokehPID']=bokeh_pid

blaze_config.py

import pandas as pd
import pickle

with open('pyramidanalysis/temp/reversal_data.txt','rb') as handle:
  b=pickle.loads(handle.read())

data=dict()

keys=b.keys()
for key in keys:
  data[key]=pd.DataFrame(b[key])

然后服务器页面显示在网页的 iFrame 中,关闭框架或主 window 启动另一个视图,终止 Bokeh 服务器并删除临时文本文件。