在散热器中嵌入散景应用程序 [英] Embedding a bokeh app in flask

查看:206
本文介绍了在散热器中嵌入散景应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在拼命地将一个工作的散景小程序嵌入到烧瓶中,并且找不到一个正确的方法来做到这一点。我查看了所有的例子,但是我找不到包含更新数据的能力(最好的例子:sliders_applet)。

如果我不是错误,我确实需要散景服务器才能够更改数据(滑块等)。以这种方式启动applet,例如:

  bokeh-server --script sliders_app.py 

但是我找不到合适的,或者至少是一个将sliders_app嵌入烧瓶的工作方式。而且由于应该可以使用多个applet,所以在启动散景服务器的时候指定一个applet也不是一件容易的事情。

我很高兴感谢任何帮助 - 散景看起来像是一个伟大的工具。

解决方案

Bokeh项目的开发人员下面的信息没有回答上面的问题。如下所述,通过使用 bokeh.embed.components 嵌入一个Bokeh应用程序是绝对不可能的。 组件只能嵌入独立的文档(即不能在Bokeh服务器上运行)

$ hr

嵌入式示例有烧瓶的散景出现在散景github回购中。

 导入烧瓶

from bokeh.embed导入组件
from bokeh.plotting导入图形
from bokeh .resources从bokeh.templates导入INLINE
从bokeh.util.string中导入RESOURCES
encode_utf8

app = flask.Flask(__ name__)

颜色= {
'黑色':'#000000',
'红色':'#FF0000',
'绿色':'#00FF00',
'蓝色': '#0000FF',
}


def getitem(obj,item,default):
如果item不在obj中:
return default
else:
return obj [item]


@ app.route(/)
def polynomial():
非常简单嵌入一​​个多项式图表
#从URL获取输入参数
#通过按钮自动执行
args = flask.request.args

getitem(args,'_from',0)#获取网址中的所有表单参数,默认为
color = colors [getitem(args,'color','Black')]
_from = int )
to = int(getitem(args,'to',10))

#创建一个多项式线图
x = list(范围(_from,to + 1))
fig = figure(title =Polynomial)
fig.line(x,[i ** 2 for i in x],color = color,line_width = 2)

#配置资源以在文档中内嵌BokehJS。
#详情请参阅:
#http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#module-bokeh.resources
plot_resources = RESOURCES。渲染(
js_raw = INLINE.js_raw,
css_raw = INLINE.css_raw,
js_files = INLINE.js_files,
css_files = INLINE.css_files,


#详情请参阅:
#http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components
脚本,div =组件(无花果,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)

$ b def main():
app.debug = True
app。 run()

if __name__ ==__main__:
main()






另一个想法是运行 bokeh-server 和你的 flask web应用程序,并加载散景代码(服务器端或通过JS或iframe),但这可能会很麻烦。 / p>

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

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, e.g.:

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. 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.

解决方案

EDIT by one one of the core developers of the Bokeh project The information below does not answer the question above. It is categorically impossibly to embed a Bokeh Application by using bokeh.embed.components as described below. components is only capable of embedding standalone documenents (i.e. that do NOT run on a Bokeh server)


An example of embedding bokeh with flask is present on the 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://bokeh.pydata.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://bokeh.pydata.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.

这篇关于在散热器中嵌入散景应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆