【发布时间】:2014-04-16 05:12:58
【问题描述】:
我正在尝试在个人网站中静态嵌入散景图,但遇到了一些我不理解的行为。基本上,我正在使用散景生成一个情节,如下所示:
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 语法的打印字符串,我手动将其复制到我调用 testembed.html 的 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 sn-p函数的代码:
https://github.com/ContinuumIO/bokeh/blob/master/bokeh/objects.py#L309
我注意到有一个选项我没有传入create_html_snippet,即embed_base_url,可能与此有关。
提前致谢! 迈克
编辑
我接受了bigreddot 的建议,解决了这个问题。我遇到的实际问题是,出于安全目的,我使用的网络服务器只能访问我的 public_html 目录中的内容。解决方法是将rsync bokeh/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中。
【问题讨论】: