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

2024-10-02 12:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

按照复杂度的顺序,使用金字塔,我可以创建静态bokeh图,然后用div标记(如outlinedhere)对其进行编码。在

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

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

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

有些事情我不清楚:

  • 我不确定用于选择和过滤数据的“小部件”应该在哪里呈现。为了方便与图的其余部分交互,它们应该是bokeh服务器的一部分。在
  • 我不知道如何将bokeh服务器页面集成到金字塔视图中。在
  • 我也不确定如何从金字塔网络应用程序启动bokeh服务器。在

有一个one paragraph提到了如何将bokeh服务器嵌入到Flask或Tornado应用程序中。但这段话太简短了,我现在不太明白。所以我问我在金字塔里怎么做?在


Tags: 数据对象标记模型div服务器视图应用程序
2条回答

嵌入另一个(烧瓶、django、tornado等)过程的运行公式在所有情况下基本相同。在这个“独立”的示例中介绍了一些基本要素,其中仅显示了在您自己管理的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)
    
  • 使用此函数创建一个BokehApplication,并用它启动Bokeh服务器:

    ^{2美元
  • 最后,将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应用程序(示例见Flask嵌入脚本)

正如bigreddot所说,工作流程与代码中微小的更改非常相似。我实际上是根据他的回答来回答的。谢谢你,比格瑞多!在

以下是我的解决方案,集成博克服务器和金字塔。在

  1. 创建生成Bokeh文档(plot)的函数
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. 将应用程序的路由位置添加到金字塔服务器配置对象。 主要在__init__.py或配置路由的任何其他文件中。在
^{pr2}$
  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. 现在,启动一个bokeh服务器。在
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

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

相关问题 更多 >

    热门问题