Python 烧瓶应用程序中的Bokeh问题渲染绘图
我的烧瓶驱动的应用程序与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
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修复了它。愚蠢的错误…把这句话写在回答里,我会接受的。非常感谢。