15

我正在尝试在个人网站中静态嵌入散景图,并且遇到了一些我不理解的行为。基本上,我正在使用散景生成一个情节,如下所示:

import bokeh.plotting as bplt
import numpy as np

x=np.random.random(100)
y=np.random.random(100)

bplt.output_file("t.html")
plot=bplt.line(x,y)

##the following line refers to the bokeh installed on my home computer
print plot.create_html_snippet(
           static_path='/usr/local/lib/python2.7/site-packages/bokeh/server/static/')

##the following line refers to the bokeh installed on my remote computer
#print plot.create_html_snippet(
#           static_path='/opt/anaconda/lib/python2.7/site-packages/bokeh/server/static/')

到现在为止还挺好。这会生成一个看起来像 的文件(random garbage).embed.js,以及一个包含我手动复制到我正在调用的 html 文件中的 html 语法的打印字符串,testembed.html我在下面复制了该文件:

<html>
<body>

<h2>Simple Embed Example</h2>
<p>This is where my plot should be:</p>
<p>
<!--The next 4 lines are the output of the print statement from the python code-->
<script src="ccbd451a-6995-4dd2-b99c-e4140b362997.embed.js"
        bokeh_plottype="embeddata"
        bokeh_modelid="ccbd451a-6995-4dd2-b99c-e4140b362997"
        bokeh_modeltype="Plot" async="true"></script>
</p>

</body>
</html>

如果我有 python 代码引用我的本地python 安装并将生成的文件(.html 和 .embed.js)复制到我的本地计算机,我可以在 html 文件中看到该图。

但是,我真正想做的是让它在远程计算机上运行,​​并通过我的个人网站上的网络访问 html 文件。

当我static_path参考我的远程计算机的python安装(如上图,注释掉)时,当我通过网络访问它时,我看不到html页面中的情节(即,去http://mywebsite.com/ testembed.html)。我不知道为什么会这样。

作为参考,这里是定义 html 片段函数的代码: https://github.com/ContinuumIO/bokeh/blob/master/bokeh/objects.py#L309 我注意到有一个选项我没有传入create_html_snippet,即 , embed_base_url可能与此有关。

提前致谢!麦克风

编辑 我接受了bigreddot的建议,解决了这个问题。我遇到的实际问题是,出于安全目的,我使用的网络服务器只能访问我public_html目录中的内容。解决方法是rsyncbokeh/static目录放入我的目录public_html并指向:

rsync -ax /opt/anaconda/lib/python2.7/site-packages/bokeh/server/static/ /home/myusername/public_html/bokeh-static/

然后修改我的代码如下:

import bokeh.plotting as bplt
import numpy as np

x=np.random.random(100)
y=np.random.random(100)

bplt.output_file("t.html")
plot=bplt.line(x,y)


#the following line refers to the bokeh rsynced to my directory
print plot.create_html_snippet(
           static_path='http://www.my_server_website/~myusername/bokeh-static/', 
           embed_base_url = 'http://www.my_server_website/~myusername/where_.js_file_is_located')

然后显然将生成的 html 复制到testembed.html.

4

2 回答 2

20

更新:create_html_snippet原始问题中提到的功能在几年前已被弃用和删除。现在有各种更新的方法可以在bokeh.embed模块中嵌入 Bokeh 内容。这个答案将总结其中的一些。

独立内容

独立的 Bokeh 内容是纯 HTML/JS/CSS,不受正在运行的 Bokeh 服务器的支持。然而,独立的 Bokeh 内容仍然可以是高度交互的,具有绘图工具(例如平移、缩放、选择)、链接画笔和触发 CustomJS 操作的小部件。有几种方法可以嵌入独立内容:

json_item

如果您想创建可由 JS 函数加载的内容的纯 JSON 表示,您可以使用该json_item函数。例如,您可以从 Flask 端点提供 JSON 服务:

@app.route('/plot')
def plot():
    p = make_plot('petal_width', 'petal_length')
    return json.dumps(json_item(p, "myplot"))

然后页面可以使用 JavaScript 代码加载和呈现内容,如下所示:

<div id="myplot"></div>

<script>
fetch('/plot')
    .then(function(response) { return response.json(); })
    .then(function(item) { Bokeh.embed.embed_item(item); })
</script>

这假设您已经在页面上加载了 BokehJS 库,例如通过CDN.render()在页面中进行模板化<head>。在此处查看完整的最小示例

components

如果您想生成一个简单的<script>标签并且<div>可以模板化到页面中,您可以使用以下components功能:

from bokeh.plotting import figure
from bokeh.embed import components

plot = figure()
plot.circle([1,2], [3,4])

script, div = components(plot)

返回的scriptdiv(或传递多个项目的 div)可以插入到页面中:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Bokeh Scatter Plots</title>

        <!-- COPY/PASTE BOKEHJS RESOURCES HERE -->

        <!-- COPY/PASTE SCRIPT HERE -->

    </head>
    <body>
        <!-- INSERT DIVS HERE -->
    </body>
</html>

如上所述,您需要在页头中对 BokehJS JS 和 CSS 资源进行硬编码或模板化,例如使用CDN.render()

file_html

如果要生成整个完整的 HTML 页面(即包括<head></head><body></body>),可以使用该file_html函数:

from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import file_html

plot = figure()
plot.circle([1,2], [3,4])

html = file_html(plot, CDN, "my plot")

这会生成一个可以保存或提供服务等的基本页面。如果需要,您还可以提供自己的 Jinja 模板(有关详细信息,请参阅文档)。

散景服务器应用程序

Bokeh 服务器应用程序可以将 Bokeh 绘图和小部件连接到实时运行的 Python 进程,以便 UI 交互、选择或小部件操作等事件可以触发真正的 Python 代码(例如 Pandas 或 scikit-learn)。

要在页面模板中嵌入基本的 Bokeh 应用程序,最常用的方法是使用server_document

from bokeh.embed import server_document
script = server_document("https://demo.bokeh.org/slider")

返回的script模板可以在 HTML 页面的任何地方进行模板化,Bokeh 应用程序将出现在那里。还有许多其他可能性,例如单独嵌入应用程序组件、为用户自定义会话或在代理/负载平衡器后面运行。Bokeh 服务器可能还需要配置为允许访问嵌入页面。有关完整详细信息,请参阅用户指南的运行散景服务器一章。

“嵌入” Bokeh 服务器应用程序的另一种可能更简单的方法是使用指向正在运行的 Bokeh 应用程序的公共 URL 的 IFrame。

于 2014-07-17T12:38:18.263 回答
3

编辑:此答案中的信息与 Bokeh 的旧版本有关,不再与任何用法相关


embed_base_url控制 javascript 将在其中搜索嵌入文件的 url 路径(可以是绝对的或相对的)。

embed_save_loc控制 python 将在其中写出嵌入文件的目录。当 server=True 时,不需要 embed_save_loc

static_path控制 javascript 将用于为 bokeh.js 和 bokeh.css 构造 URL 的 url 路径(它可以是绝对的或相对的)。它默认为http://localhost:5006/static/,但也可以很容易地指向 CDN

运行散景服务器时,导航到http://localhost:5006/bokeh/generate_embed/static. 我认为这需要您因为错误而在 master 上运行。

编辑:CDN 是“内容交付网络”,它只是文件服务器的一个花哨术语。例如,我们在http://cdn.bokeh.org/bokeh-0.4.2.js(或http://cdn.bokeh.org/bokeh-0.4.2.min.js)为任何人托管 bokeh.js使用。

于 2014-03-13T22:27:28.260 回答