如何将 Bokeh 服务器集成到 Pyramids 应用程序中?

How do I integrate a Bokeh Server into a Pyramids Application?

按照复杂程度的顺序,使用金字塔,我可以创建静态散景图,然后将它们与 div 标记结合起来,如概述 here

Bokeh documentations 清楚地解释了如何设置 bokeh 服务器进行交互式数据探索,我已经成功创建了这样一个应用程序。

我想做的是在 Pyramids 视图页面中有一个交互式图表。此页面的要求如下:

还有不明白的地方:

one paragraph 提到如何将散景服务器嵌入到 Flask 或 Tornado 应用程序中。但是这段太简短了,我现在无法理解。所以我问我如何在金字塔中做到这一点?

嵌入另一个(flask、django、tornado 等)进程的运行公式在所有情况下基本相同。他在这个 "standalone" 示例中介绍了基本要素,该示例仅显示了在您自己管理的 Tornado IOloop 上启动 Bokeh 服务器所需的步骤:

https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/standalone_embed.py

基本步骤是:

  • 制作一个生成Bokeh文档的函数:

    def modify_doc(doc):
    
        # setup up plots and widgets in a layout, then
        doc.add_root(some_layout)
    
  • 用这个函数创建一个 Bokeh Application,并用它启动一个 Bokeh 服务器:

    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()
    

然后您的(Flask、Django、Pyramid 等)视图可以使用 <iframes>bokeh.embed.autoload_server 以标准方式从此服务器嵌入 Bokeh 应用程序(参见例如 Flask 嵌入脚本示例)

正如 bigreddot 所说,工作流程非常相似,只是代码有细微的变化。我实际上根据他的回答建立了我的答案。感谢 bigreddot!

以下是我将 bokeh-server 与 Pyramid 集成的解决方案。

  1. 创建一个函数来生成散景文档(绘图)
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)
  1. 将应用程序的路由位置添加到 Pyramid 服务器配置对象。 主要在 __init__.py 或您配置路由的任何其他文件中。
    conf.add_route('bokeh_app', '/bokeh-app')
  1. 添加一个必须呈现 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}
  1. 现在,启动散景服务器。
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 所需的网络套接字连接的字符串列表。在这种情况下,我们需要给金字塔服务器url

  1. 最后,启动金字塔服务器
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()