简体   繁体   English

在烧瓶中嵌入散景应用程序

[英]Embedding a bokeh app in flask

I am trying desperately to embed a working bokeh applet into flask, and can't find a proper way to do this.我拼命地尝试将一个工作散景小程序嵌入烧瓶中,但找不到合适的方法来做到这一点。 I looked through all the examples, but I can't find one which includes the ability to update the data (best example: the sliders_applet).我查看了所有示例,但找不到包含更新数据功能的示例(最佳示例:slider_applet)。

If I'm not mistaken, I do need the bokeh-server to be able to change the data (with sliders etc.).如果我没记错的话,我确实需要散景服务器才能更改数据(使用滑块等)。 Starting the applet this way works, eg:以这种方式启动小程序有效,例如:

bokeh-server --script sliders_app.py

But I can't find the proper, or at least a working way to embed the sliders_app into flask.但我找不到合适的,或者至少是一种将 sliders_app 嵌入到烧瓶中的工作方法。 And since it should be possible to use multiple applets, it doesn't seem clean to me to specify one single applet at the startup of the bokeh server too..而且由于应该可以使用多个小程序,因此在散景服务器启动时指定一个小程序对我来说似乎也不干净。

I would gladly appreciate any help - bokeh looks like a great tool for me.我很乐意提供任何帮助 - 散景对我来说是一个很好的工具。

The other answer does not describe how to embed a Bokeh server app (it uses components to embed a standalone Bokeh document).另一个答案没有描述如何嵌入 Bokeh 服务器应用程序(它使用components嵌入独立的 Bokeh 文档)。

First, you can see lots of live examples hosted at: https://demo.bokeh.org/首先,您可以在以下位置看到许多现场示例: https : //demo.bokeh.org/

For embedding apps there are two usual options:对于嵌入应用程序,有两个常用选项:

The latter is usually used like this:后者通常是这样使用的:

from bokeh.embed import server_document
script = server_document("https://demo.bokeh.org/sliders")

This will return a <script> tag similar to the one below, that you can put in your flask HTML response, wherever you'd like the app to appear:这将返回一个类似于下面的<script>标签,您可以将其放入您的 Flask 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>

Lastly, it's important to note that by default the Bokeh server opts for a fairly conservative network configuration.最后,需要注意的是,默认情况下 Bokeh 服务器选择相当保守的网络配置。 You'll need to start the Bokeh server with --allow-websocket-origin command line option set to be whatever host you are embedding the bokeh app into.您需要将--allow-websocket-origin命令行选项设置为您要将散景应用程序嵌入到的任何主机来启动散景服务器。

EDIT by one one of the core developers of the Bokeh project The information below does not answer the question above.由 Bokeh 项目的核心开发人员之一编辑下面的信息没有回答上面的问题。 It is categorically impossibly to embed a Bokeh Application by using bokeh.embed.components as described below.如下所述,绝对不可能使用bokeh.embed.components来嵌入 Bokeh应用程序 components is only capable of embedding standalone documenents (ie that do NOT run on a Bokeh server) components只能嵌入独立文档(即不在 Bokeh 服务器上运行的文档)


An example of embedding bokeh with flask is present on the bokeh github repo . 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()

Another idea would be to run bokeh-server and your flask web app side-by-side, and load the bokeh-code that way (server-side or via JS or an iframe), but that could be troublesome.另一个想法是并排运行bokeh-server和您的flask Web 应用程序,并以这种方式(服务器端或通过 JS 或 iframe)加载 bokeh 代码,但这可能会很麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM