在bokeh中嵌入散景应用程序

lak*_*erz 19 python applet flask bokeh

我正在拼命地将一个正在运行的散景小程序嵌入到烧瓶中,并且找不到合适的方法来执行此操作.我查看了所有示例,但我找不到一个包含更新数据的能力(最好的例子:sliders_applet).

如果我没弄错的话,我确实需要散景服务器才能更改数据(使用滑块等).以这种方式启动applet,例如:

bokeh-server --script sliders_app.py
Run Code Online (Sandbox Code Playgroud)

但我找不到合适的,或者至少是一种将sliders_app嵌入烧瓶的工作方式.由于应该可以使用多个applet,因此在散景服务器启动时指定一个小程序似乎并不干净.

我很乐意感谢任何帮助 - 散景看起来对我来说是一个很棒的工具.

hal*_*ngs 10

由Bokeh项目的核心开发人员之一编辑以下信息不能回答上述问题.通过使用如下所述来嵌入Bokeh 应用程序是绝对不可能的bokeh.embed.components.components只能嵌入独立的文档(即不在Bokeh服务器上运行)


景github repo上存在一个用烧瓶嵌入散景例子.

import flask

from bokeh.embed import components
from bokeh.plotting import figure
from bokeh.resources import INLINE
from bokeh.templates import RESOURCES
from bokeh.util.string import encode_utf8

app = flask.Flask(__name__)

colors = {
    'Black': '#000000',
    'Red':   '#FF0000',
    'Green': '#00FF00',
    'Blue':  '#0000FF',
}


def getitem(obj, item, default):
    if item not in obj:
        return default
    else:
        return obj[item]


@app.route("/")
def polynomial():
    """ Very simple embedding of a polynomial chart"""
    # Grab the inputs arguments from the URL
    # This is automated by the button
    args = flask.request.args

    # Get all the form arguments in the url with defaults
    color = colors[getitem(args, 'color', 'Black')]
    _from = int(getitem(args, '_from', 0))
    to = int(getitem(args, 'to', 10))

    # Create a polynomial line graph
    x = list(range(_from, to + 1))
    fig = figure(title="Polynomial")
    fig.line(x, [i ** 2 for i in x], color=color, line_width=2)

    # Configure resources to include BokehJS inline in the document.
    # For more details see:
    #   http://docs.bokeh.org/en/latest/docs/reference/resources_embedding.html#module-bokeh.resources
    plot_resources = RESOURCES.render(
        js_raw=INLINE.js_raw,
        css_raw=INLINE.css_raw,
        js_files=INLINE.js_files,
        css_files=INLINE.css_files,
    )

    # For more details see:
    #   http://docs.bokeh.org/en/latest/docs/user_guide/embedding.html#components
    script, div = components(fig, INLINE)
    html = flask.render_template(
        'embed.html',
        plot_script=script, plot_div=div, plot_resources=plot_resources,
        color=color, _from=_from, to=to
    )
    return encode_utf8(html)


def main():
    app.debug = True
    app.run()

if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

另一个想法是并排运行bokeh-server和你的flask网络应用程序,并以这种方式(服务器端或通过JS或iframe)加载散景代码,但这可能很麻烦.

  • 嘿伙计们,有没有人设法实现散景服务器与烧瓶的整洁整合?如果是,将代码作为答案发布将非常有帮助. (5认同)

big*_*dot 10

另一个答案没有描述如何嵌入Bokeh服务器应用程序(它用于components嵌入独立的Bokeh文档).

首先,您可以在以下网址看到许多实时示例:https://demo.bokehplots.com/

对于嵌入应用程序,有两种常用选项:

后者通常使用如下:

from bokeh.embed import server_document
script = server_document("https://demo.bokeh.org/sliders")
Run Code Online (Sandbox Code Playgroud)

这将返回一个<script>类似于下面的标记,您可以将其放入烧瓶HTML响应中,无论您希望应用出现在何处:

<script
    src="https://demo.bokeh.org/sliders/autoload.js?bokeh-autoload-element=1000&bokeh-app-path=/sliders&bokeh-absolute-url=https://demo.bokeh.org/sliders"
    id="1000">
</script>
Run Code Online (Sandbox Code Playgroud)

最后,值得注意的是,默认情况下,Bokeh服务器选择相当保守的网络配置.您需要启动Bokeh服务器,并将--allow-websocket-origin命令行选项设置为您将散景应用程序嵌入到的任何主机.

  • 你有两个完整的例子来说明iframe和`autoload_server`吗? (3认同)