首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在瓶中嵌入bokeh图

在瓶中嵌入bokeh图
EN

Stack Overflow用户
提问于 2015-10-31 10:58:00
回答 2查看 3.6K关注 0票数 4

我希望得到一个由Flask返回的简单行bokeh图,但是当我浏览localhost时得到的是:5002/simpleline:

(“,”)

我有两个文件。Python文件:

代码语言:javascript
运行
复制
from bokeh.plotting import figure, show, output_file
from bokeh.embed import components
from flask import Flask, render_template
app=Flask(__name__)

@app.route('/simpleline/')
def simpleLine():
    fig=figure(title="Sensor data")
    fig.line([1,2,3,4],[2,4,6,8])
    div=components(fig)
    return render_template('simpleline.html',div=div)
    show(fig)

if __name__ == "__main__":
    app.run(debug=True,port=5002)

以及HTML模板:

代码语言:javascript
运行
复制
<!doctype html>
<html>
<head>
 <title>Figure examples</title>
 <link rel="stylesheet" href="http://cdn.bokeh.org/bokeh-0.7.1.min.css" type="text/css" />
 <script type="text/javascript"src="http://cdn.bokeh.org/bokeh-0.7.1.min.js"></script>
</head>
<body>
<div class='bokeh'>
 {{ div|safe }}
</div>
</body>
</html>

我肯定我在这里错过了一些重要的东西。

在mn的回答之后,我们发现components()生成两个元素,一个Javascript字符串和一个html div。因此,我更新了我的脚本如下,但这一次网页显示为空白。

代码语言:javascript
运行
复制
from bokeh.plotting import figure, show, output_file
from bokeh.embed import components
from flask import Flask, render_template
app=Flask(__name__)

@app.route('/simpleline/')
def simpleLine():
    fig=figure(title="Sensor data")
    fig.line([1,2,3,4],[2,4,6,8])
    global script
    global div
    script,div=components(fig)
    return render_template('simpleline.html',div=div,script=script)
    output_file("simpleline.html")
    show(fig)

fig=figure(title="Sensor data")
fig.line([1,2,3,4],[2,4,6,8])
script,div=components(fig)
if __name__ == "__main__":
    app.run(debug=True,port=5002)

以及HTML模板:

代码语言:javascript
运行
复制
<!doctype html>
<html>
 <head>
  <title>Figure examples</title>
  <link rel="stylesheet" href="http://cdn.bokeh.org/bokeh-0.9.0.min.css" type="text/css" />
  <script type="text/javascript" src="http://cdn.bokeh.org/bokeh-0.9.0.min.js"></script>
  {{ script|safe }}
 </head>
 <body>
  <div class='bokeh'>
   {{ div|safe }}
  </div>
 </body>
</html>

我尝试了所有bokeh-0.7.1.min.js、0.9和0.10,但仍然得到了相同的空白页。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-31 11:57:29

components()返回带有<script>(script, div)元组,其中包含绘图的数据和加载到绘图视图中的伴随的<div>标记:

guide/embed.html#components

代码语言:javascript
运行
复制
script, div = components(fig)
return render_template('simpleline.html',div=div, script=script)

模板

代码语言:javascript
运行
复制
<!doctype html>
<html>
 <head>
  <title>Figure examples</title>
  <link rel="stylesheet" href="http://cdn.bokeh.org/bokeh/release/bokeh-0.10.0.min.css" type="text/css" />
  <script type="text/javascript" src="http://cdn.bokeh.org/bokeh/release/bokeh-0.10.0.min.js"></script>
  {{ script|safe }}
 </head>
 <body>
  <div class='bokeh'>
   {{ div|safe }}
  </div>
 </body>
</html>
票数 7
EN

Stack Overflow用户

发布于 2015-11-05 05:30:40

或者,如果希望bokeh和css自动加载,也可以使用autoload_static而不是components。此外,您还可以将js保存到文件路径中,并仅使用html中的div来访问它。

下面是我使用过的示例代码:

代码语言:javascript
运行
复制
from bokeh.embed import autoload_static
from bokeh.resources import CDN
.............
.............
js, div = autoload_static(bar, CDN, '/static/bokeh/plot.js')
with open('static/bokeh/plot.js', 'w') as f:
        f.write(js)

然后在html文件中只包含div标记(包括js脚本的路径)。

代码语言:javascript
运行
复制
<!doctype html>
 <html>
   <head>
     <title>Figure examples</title>
   </head>
   <body>
    <div class='bokeh'>
    {{ div|safe }}
    </div>
   </body>
 </html>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33450773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档