gpt4 book ai didi

python - 无法将 Bokeh 图嵌入到 Flask 应用程序中

转载 作者:行者123 更新时间:2023-11-28 22:14:15 24 4
gpt4 key购买 nike

我是 Bokeh 和 Flask 的新手。我浏览了相关问题、教程并查看了 Bokeh 文档,但无法弄清楚我做错了什么。

话虽这么说,我想创建一个简单的网络应用程序,我可以在其中“组合”各种数据报告和图表。

根据我的阅读,我得出以下结论:

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 的链接。当我点击它时,我看到的只是标题“bokeh sample”,但没有情节。

如果我取消对 show(plot) 的注释,则会打开一个新选项卡并正确显示该图,因此问题似乎不在于图本身,而是在于我尝试嵌入它的方式在 Bokeh 测试中。

我是这一切的新手,所以也许我在做一些愚蠢的事情,但我一直无法弄清楚,我会很感激一些帮助。

附言。不确定是否可能相关,但为此我从 Anaconda 2 创建了一个 python 3.6 环境,我使用这个环境作为项目解释器。

最佳答案

您正在模板中从 CDN 加载 BokehJS 版本 0.12.13。那里的版本需要与您系统中安装的 Bokeh 版本完全匹配。

关于python - 无法将 Bokeh 图嵌入到 Flask 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53474808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com