我正在尝试在个人网站中静态嵌入散景图,并且遇到了一些我不理解的行为。基本上,我正在使用散景生成一个情节,如下所示:
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
目录中的内容。解决方法是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
.