Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 烧瓶应用程序中的Bokeh问题渲染绘图_Python_Flask_Bokeh - Fatal编程技术网

Python 烧瓶应用程序中的Bokeh问题渲染绘图

Python 烧瓶应用程序中的Bokeh问题渲染绘图,python,flask,bokeh,Python,Flask,Bokeh,我的烧瓶驱动的应用程序与Bokeh的高水平条形图一样工作 我现在想把这个图改成水平条形图,然后找到答案 为简洁起见,我的代码减去格式: from flask import Flask, render_template from bokeh.embed import components from bokeh.util.string import encode_utf8 from bokeh.plotting import figure import pandas as pd app = Fla

我的烧瓶驱动的应用程序与Bokeh的高水平条形图一样工作

我现在想把这个图改成水平条形图,然后找到答案

为简洁起见,我的代码减去格式:

from flask import Flask, render_template
from bokeh.embed import components
from bokeh.util.string import encode_utf8
from bokeh.plotting import figure
import pandas as pd

app = Flask(__name__)

@app.route('/')
def test():
    kws = ["one", "two", "cat", "dog"]
    count = [23, 45, 11, 87]
    df = pd.DataFrame({"kw": kws,
                       "count": count
                       })

    df.sort("count", inplace=True)
    df.set_index("kw", inplace=True)
    series = df['count']

    p = figure(width=1000, height=1000, y_range=series.index.tolist())

    j = 1
    for k, v in series.iteritems():
        w = v / 2 * 2
        p.rect(x=v/2,
                y=j,
                width=w,
                height=0.4,
                color=(76, 114, 176),
                width_units="screen",
                height_units="screen"
                )
        j += 1

    #### get components ####
    script, div = components(p)

    page = render_template('test.html', div=div, script=script)
    return encode_utf8(page)


if __name__ == "__main__":
    app.run(debug=True,
            threaded=False
            )
位于
模板/test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="http://cdn.bokeh.org/bokeh/release/bokeh-0.9.0.min.css" rel="stylesheet" type="text/css">
    <script src="http://cdn.bokeh.org/bokeh/release/bokeh-0.9.0.min.js"></script>
</head>

<body>

{{ div | safe }}
{{ script | safe }}

</body>
</html>

{{div | safe}}
{{script | safe}}
此答案适用于测试
show(p)
。但是,实际应用程序使用
p
对象,获取组件
div
script
,并将它们嵌入到html中

当我用
debug=True
运行应用程序时,我不会得到错误,只是得到一个挂起的页面


编辑:“挂起”不准确。我得到了一张空白页。

按照Bigreddot的建议,我检查了我的Bokeh版本,并调整了BokehJS版本以匹配

conda list bokeh

bokeh                     0.10.0                   py27_0  
然后我改变了我的html,最小的示例如预期的那样工作

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="http://cdn.bokeh.org/bokeh/release/bokeh-0.10.0.min.css" rel="stylesheet" type="text/css">
    <script src="http://cdn.bokeh.org/bokeh/release/bokeh-0.10.0.min.js"></script>
</head>

<body>

{{ div | safe }}
{{ script | safe }}

</body>
</html>

{{div | safe}}
{{script | safe}}

如果从现在起有人遇到此问题,请注意,
bokeh.util.string import encode\u utf8
已从bokeh==2.0.0删除


在我的例子中,使用flask应用程序(使用flask==1.1.2和bokeh==2.0.2),我可以简单地从代码和html呈现模板中删除这一行,只需
return html
而不是
return encode\u utf8(html)

您正在从模板中的CDN加载一个非常旧版本的BokehJS。您实际使用的是Bokeh版本
0.9
?如果不是,CDN版本应该与您安装的版本相匹配。好的一点…忘了这一点。我正在使用Bokeh 0.10.0,将更改并重试。@bigreddot修复了它。愚蠢的错误…把这句话写在回答里,我会接受的。非常感谢。