1

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

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

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

理想情况下,Bokeh 服务器将持续运行,我可以将客户选择的数据推送到它,然后将其用作下采样图的源。但是,据我所知,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 服务器窗口已关闭的信号将请求发送到另一个视图,该视图将停止服务器。

@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 脚本创建和提供它们时,这些图按我的意图工作。

所以我的问题如下:我可以将数据推送到已经运行的 Bokeh 服务器,然后将其用作数据源进行下采样吗?如果没有,我如何根据 Pyramid 应用程序中的请求启动/停止 Bokeh 服务器?

提前感谢您的帮助!

4

1 回答 1

1

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

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

视图.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 中,关闭框架或主窗口会启动另一个视图,该视图终止 Bokeh 服务器并删除临时文本文件。

于 2015-04-14T16:00:50.850 回答