按照金字塔的复杂性顺序,我可以创建静态散景图,然后用像此处概述的DIV标签不正确。
散景文档清楚地说明了如何为交互式数据探索设置散景服务器,我已经成功创建了这样的应用程序。
我想做的是在金字塔查看页面中拥有一个交互式图。此页面的要求如下:
- 加载视图后,将以某种方式启动散景服务器,并将数据加载到服务器的对象模型中。
- 以某种方式,金字塔视图还将在服务器对象模型中接收数据并渲染数据。
我不清楚的事情:
- 我不确定应该渲染用于选择和过滤数据的"小部件"的位置。看来,要轻松与图形的其余部分进行交互,它们应该是散景服务器的一部分。
- 我不确定如何将散景服务器页面集成到金字塔视图中。
- 我也不确定如何从金字塔Web应用程序中接近散景服务器的启动。
有一个段落提到如何将散布服务器嵌入烧瓶或龙卷风应用程序中。但是,这段太简短了,现在我无法有意义。所以我问我该如何在金字塔中做到这一点?
正如BigReddot所说,工作流与代码的微小更改非常相似。我实际上是根据他的安格(Anwer(建立了答案。谢谢BigRedDot!
以下是我将散景与金字塔整合的解决方案。
- 创建一个函数以生成散景文档(图(
def bokeh_doc(doc):
# create data source
# define all elements that are necessary
# ex:
p = line(x, y, source)
# now add 'p' to the doc object
doc.add_root(p)
# define a callback if necessary
# and register that callback
doc.add_periodic_callback(_cb, delay)
- 将应用程序的路由位置添加到金字塔服务器配置对象。主要在
__init__.py
或您配置路由的任何其他文件中。
conf.add_route('bokeh_app', '/bokeh-app')
- 添加一个视图必须呈现
bokeh_app
。此功能可以写在views.py
或您认为适当的地方。
from pyramid.view import view_config
from bokeh.embed import server_document
@view_config(route_name='bokeh_app', renderer='static/plot.jinja2')
def bokeh_view(request):
# this '/app' route to the plot is configured in step. 4
# using default host and port of bokeh server.
# But, the host and port can be configured (step. 4)
script = server_document('localhost:5006/app')
# assuming your jinja2 file has
# {{ script|safe }}
# embedded somewhere in the <body> tag
return {'script': script}
- 现在,开火散景服务器。
from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler
from bokeh.server.server import Server
# bokeh_doc is the function which defines the plot layout (step. 1)
chart_app = Application(FunctionHandler(bokeh_doc))
# the '/app' path is configured to display the 'chart_app' application
# here, a different host and port for Bokeh-server could be defined
# ex: {"<host2:9898>/app_bokeh": chart_app}
bokeh_server = Server({"/app": chart_app}, allow_websocket_origin=["localhost:6543"])
# start the bokeh server and put it in a loop
server.start()
server.io_loop.start()
allow_websocket_origin
列出了必须升级的字符串列表,以支持Bokeh所需的Web插座连接。在这种情况下,我们需要提供金字塔服务器的URL
- 最后,启动金字塔服务器
from wsgiref.simple_server import make_server
pyramid_app = conf.make_wsgi_app()
pyramid_server = make_server('localhost', 6543, pyramid_app)
pyramid_server.serve_forever()
在所有情况下,嵌入在另一个烧瓶,django,tornado等(的公式在所有情况下都是相同的。这个"独立"示例中介绍了Bare Essentials,该示例仅显示在您管理自己的龙卷风IOloop
上启动散景服务器所需的步骤:
https://github.com/bokeh/bokeh/bokeh/blob/master/examples/howto/server_embed/standalone_embed.py
基本步骤是:
-
发挥生成散景文档的函数:
def modify_doc(doc): # setup up plots and widgets in a layout, then doc.add_root(some_layout)
-
使用此功能创建一个散景
Application
,然后使用它启动散景服务器:from bokeh.application.handlers import FunctionHandler from bokeh.application import Application from bokeh.server.server import Server bokeh_app = Application(FunctionHandler(modify_doc)) server = Server({'/': bokeh_app}, io_loop=io_loop) server.start()
-
最后,将散布
Server
添加到创建和管理的龙卷风IOloop
中:from tornado.ioloop import IOLoop io_loop = IOLoop.current() io_loop.add_callback(server.show, "/") io_loop.start()
然后您的(烧瓶,django,金字塔等(视图可以以标准方式使用 <iframes>
或 bokeh.embed.autoload_server
嵌入该服务器的散布应用程序(例如,请参见示例的烧瓶嵌入脚本(