按照复杂性的顺序,我可以使用金字塔创建静态散景图,然后将它们与div标签(如here概述)合并。

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

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


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


有些事情我不清楚:


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


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

最佳答案

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

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


创建一个函数以生成Bokeh文档(图解)


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)



将应用程序的路由位置添加到Pyramid服务器配置对象。
通常在__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}



现在,启动一个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套接字连接。在这种情况下,我们需要提供金字塔服务器的网址


最后,启动Pyramid服务器


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

关于python - 如何将Bokeh服务器集成到 Pyramid 应用程序中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44400581/

10-12 17:45
查看更多