2017-06-06 65 views
1

爲了複雜性,使用金字塔,我可以創建靜態散景圖,然後將它們與div標籤(如概述的here)合併。如何將散景服務器集成到金字塔應用程序中?

Bokeh documentations清楚地解釋瞭如何設置一個用於交互式數據探索的散景服務器,並且我已經成功創建了這樣一個應用程序。

我想要做的是在金字塔視圖頁面中創建一個交互式圖形。該頁面的要求如下:

  • 加載視圖後,散景服務器以某種方式啓動,並將數據加載到服務器的對象模型中。
  • 不知何故金字塔視圖也會接收服務器對象模型中的數據並呈現數據。

有些事情我並不清楚:

  • 我不知道到哪裏用於選擇和過濾數據應該呈現的「小部件」。看起來,爲了便於與圖形的其餘部分進行交互,它們應該是散景服務器的一部分。
  • 我不確定如何將散景服務器頁面集成到金字塔視圖中。
  • 我也不確定如何從金字塔網絡應用程序中啓動散景服務器。

one paragraph提到如何散景服務器可以嵌入到Flask或龍捲風應用程序。但是這段對於我來說太簡短了,現在纔有意義。所以我問我怎麼在金字塔中做到這一點?

回答

2

由於bigreddot表示,工作流程與代碼中的細微變化非常相似。我實際上是根據他的口吻建立了我的答案。謝謝bigreddot!

以下是我的解決方案集成散景服務與金字塔。

  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) 
  • 添加該應用服務器金字塔配置對象的路徑位置。 主要在__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發生在必須升級以支持背景虛化需要網絡套接字連接字符串列表。在這種情況下,我們需要給金字塔服務器的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() 
    
    +0

    我小有一點糊塗 - 也是如此代碼在第4部分中,您啓動Bokeh服務器必須是不同的應用程序並與金字塔應用程序分開運行?我想知道,因爲理想情況下,我會在我的金字塔應用程序中嵌入散景服務器,並讓它在我的金字塔應用程序內啓動Bokeh(即,當我運行'pserve development.ini'之類的東西啓動我的散景服務器時),然後當我進入'散景'頁面時,它已經爲我準備好了。 – hhprogram

    +0

    不好意思 - 我說這就是我想要做的。但是我想向你確認這聽起來像你的解決方案要求你分別運行散景服務器。然後啓動金字塔服務器 - 是否正確? – hhprogram

    +0

    可能找到了解決方案。稍後我會更多地閱讀它 – hhprogram

    2

    嵌入在另一個(燒瓶,django,龍捲風等)過程中的運行公式在所有情況下基本相同。他最基本的要素在這個「獨立」的例子,顯示只是在龍捲風IOloop啓動背景虛化服務器所需的步驟,提出您自己管理:

    https://github.com/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() 
      

    然後你( Flask,Django,Pyramid,whatever)視圖可以使用標準方式使用<iframes>bokeh.embed.autoload_server來嵌入來自此服務器的Bokeh應用程序(請參閱以Flask嵌入腳本爲例)

    相關問題