将散景图嵌入Flask应用程序时遇到问题 [英] Trouble embedding Bokeh plot into Flask app
问题描述
我对Bokeh和Flask都是陌生的.我浏览了相关的问题,教程,并查看了Bokeh文档,但无法弄清楚我在做什么错.
I'm new both to Bokeh and Flask. I browsed related questions, tutorials, and looked at Bokeh docs but could not figure out what I'm doing wrong.
话虽如此,我想创建一个简单的网络应用程序,在其中将各种数据报告和图表分组".
That being said, I want to create a simple web-app in which I "group together" various data reports and plots.
根据我的读物,我想到了以下内容:
According to what I read, I came up with the following:
app.py:
... # imports
app = Flask(__name__, static_url_path='/static')
@app.route("/")
def index():
return render_template("index.html")
@app.route("/bokeh_test")
def bokeh_test():
script, div = components(sample_plot())
return render_template("bokeh_test.html", script=script, div=div)
def sample_plot():
"""
A random plot just for testing purposes.
:return: plot
"""
PLOT_OPTIONS = dict(plot_width=600, plot_height=400)
SCATTER_OPTIONS = dict(size=12, alpha=0.5)
data = lambda: [random.choice([i for i in range(100)]) for r in range(10)]
plot = figure(sizing_mode='scale_both', tools='pan', **PLOT_OPTIONS)
plot.scatter(data(), data(), color="red", **SCATTER_OPTIONS)
# show(plot)
return plot
bokeh_test.html:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Bokeh includes-->
<script type="text/javascript" src="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.13.min.js"></script>
<link rel="stylesheet" href="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.13.min.css" type="text/css" />
{{ script|safe }}
</head>
<body>
<div>
<h1>Bokeh sample</h1>
<div class='bokeh'>
{{ div|safe }}
</div>
</div>
</body>
</html>
想象一下我的 index.html 文件中的边栏,其中有一个指向 bokeh_test.html 的链接.当我单击它时,我看到的只是标题散景样本",但没有情节.
Imagine in my index.html file I have a side bar with a link to bokeh_test.html. When I click it, all I see is the header "bokeh sample" but no plot.
如果取消注释 show(plot)的操作,则会打开一个新选项卡并正确显示该图,因此问题似乎不在于图本身,而是我尝试嵌入的方式在bokeh_test中.
If I uncomment the show(plot), a new tab is opened and the plot correctly displayed, so the problem seems not to be in the plot itself but in the way I try to embed it in bokeh_test.
我对所有这些都是新手,所以也许我正在做一些愚蠢的事情,但是我一直无法弄清楚,我将很乐意为您提供帮助.
I'm new to all this so maybe I'm doing something stupid but I haven't been able to figure it out and I'd appreciate some help.
PS.不知道是否可能相关,但是为此,我从Anaconda 2创建了一个python 3.6环境,并将该环境用作项目解释器.
PS. Not sure if might be related, but for this I created a python 3.6 environment from Anaconda 2, and I use this environment as the project interpreter.
推荐答案
您正在从模板中的CDN加载BokehJS版本0.12.13.那里的版本需要与您系统中安装的Bokeh的版本完全匹配.
You are loading BokehJS version 0.12.13 from CDN in your template. The version there needs to match the version of Bokeh installed in your system exactly.
这篇关于将散景图嵌入Flask应用程序时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!