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

时间:2017-06-06 22:16:45

标签: python pyramid bokeh

为了复杂,使用金字塔,我可以创建静态散景图,然后使用div here等div标签对它们进行合并。

Bokeh documentations清楚地解释了如何为交互式数据探索设置散景服务器,并且我已成功创建了这样的应用程序。

我想做的是在金字塔视图页面中有一个交互式图形。本页面的要求如下:

  • 加载视图后,会以某种方式启动散景服务器,并将数据加载到服务器的对象模型中。
  • 金字塔视图还会以某种方式接收服务器对象模型中的数据并呈现数据。

有些事我不清楚:

  • 我不确定应该在何处呈现用于选择和过滤数据的“小部件”。看来,为了便于与图表的其余部分进行交互,它们应该成为散景服务器的一部分。
  • 我不确定如何将散景服务器页面集成到金字塔视图中。
  • 我也不确定如何在Pyramids网络应用程序中启动散景服务器。

one paragraph提及散景服务器如何嵌入Flask或Tornado应用程序中。但这段话对我来说太简短了,现在没有意义。所以我在问金字塔怎么做?

2 个答案:

答案 0 :(得分:3)

正如bigreddot所说,工作流程与代码中的微小变化非常相似。我实际上根据他的答案建立了我的答案。谢谢bigreddot!

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

  1. 创建一个生成散景文档(绘图)的函数
  2.     General
          1) "before each" hook
    
    
      0 passing (10s)
      1 failing
    
      1) "before each" hook:
         Uncaught NetworkingError: Cannot read property 'replace' of undefined
          at findTargetPort (node_modules/zombie/lib/reroute.js:50:28)
          at Socket.Net.Socket.connect (node_modules/zombie/lib/reroute.js:69:18)
          at Agent.connect [as createConnection] (net.js:106:35)
          at Agent.createSocket (_http_agent.js:217:26)
          at Agent.addRequest (_http_agent.js:187:10)
          at new ClientRequest (_http_client.js:272:16)
          at Object.request (http.js:39:10)
          at features.constructor.handleRequest (node_modules/aws-sdk/lib/http/node.js:42:23)
          at executeSend (node_modules/aws-sdk/lib/event_listeners.js:304:29)
          at Request.SEND (node_modules/aws-sdk/lib/event_listeners.js:318:9)
          at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
          at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
          at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
          at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
          at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
          at node_modules/aws-sdk/lib/state_machine.js:26:10
          at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
          at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:685:12)
          at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:115:18)
          at callNextListener (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
          at node_modules/aws-sdk/lib/event_listeners.js:220:9
          at finish (node_modules/aws-sdk/lib/config.js:315:7)
          at node_modules/aws-sdk/lib/config.js:333:9
          at Credentials.get (node_modules/aws-sdk/lib/credentials.js:126:7)
          at getAsyncCredentials (node_modules/aws-sdk/lib/config.js:327:24)
          at Config.getCredentials (node_modules/aws-sdk/lib/config.js:347:9)
          at Request.SIGN (node_modules/aws-sdk/lib/event_listeners.js:192:22)
          at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
          at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
          at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
          at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
          at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
          at node_modules/aws-sdk/lib/state_machine.js:26:10
          at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
          at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:685:12)
          at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:115:18)
          at Timeout.callNextListener [as _onTimeout] (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
    
    1. 将应用程序的路径位置添加到Pyramid服务器配置对象。 主要在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) 或您配置路由的任何其他文件中。
    2. __init__.py
      1. 添加必须呈现 conf.add_route('bokeh_app', '/bokeh-app') 的视图。此功能可以用bokeh_app或您认为合适的地方编写。
      2. views.py
        1. 现在,启动一个散景服务器。
        2. 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() 接收必须升级的字符串列表,以支持散景所需的Web套接字连接。在这种情况下,我们需要提供金字塔服务器的URL

          1. 最后,启动Pyramid服务器
          2. allow_websocket_origin

答案 1 :(得分:2)

在所有情况下,嵌入另一个(烧瓶,django,龙卷风等)过程的运行公式基本相同。他在这个“独立”示例中展示了基本要素,该示例仅显示了您自己管理的龙卷风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)
    
  • 使用此功能创建一个散景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()
    
  • 最后,将Bokeh 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嵌入脚本)