我有一个 Pyramid Web 应用程序,客户希望能够交互式地绘制大型数据集。
该应用程序当前使用 D3 通过缩放、平移、悬停等功能显示客户所选数据的子集。但是,如果用户需要查看具有相同功能的全套内容,我想使用 Bokeh 服务器并使用下采样。</p>
我遇到的麻烦是下采样函数只能用于使用 ServerDataSource 的绘图。
理想情况下,Bokeh 服务器会持续运行,我可以将客户选择的数据推送给它,然后将其用作下采样图的源。但是,据我所知,Blaze 不允许我将数据推送到现有服务器。
相反,我认为当用户请求绘图时,我可以使用 Pyramid 的 View 之一来修改 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()
一旦数据存储在服务器上,另一个 View 将以 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 发送请求,该请求将停止服务器。
@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 服务器?
预先感谢您的帮助!
最佳答案
以下是应用程序如何动态配置服务器数据源、启动 Bokeh 服务器并在 Bokeh 服务器上绘制请求的数据以供客户导航。
要绘制的数据被传递到 startBokehServer View ,并在其中写入临时文本文件。然后使用 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 中,关闭该框架或主窗口将启动另一个 View ,该 View 终止 Bokeh 服务器并删除临时文本文件。
关于python - 从 Pyramid Web 应用程序配置 Blaze 并启动 Bokeh 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29547387/