我想使用 Bokeh 服务器来保存我的绘图和数据,这样我就可以将我的 Bokeh 应用程序嵌入到网站中。我正在尝试重新创建 example在 Bokeh 0.12.6 文档中给出:
from bokeh.client import push_session
from bokeh.embed import autoload_server
from bokeh.plotting import figure, curdoc
# figure() function auto-adds the figure to curdoc()
plot = figure()
plot.circle([1,2], [3,4])
session = push_session(curdoc())
script = autoload_server(plot, session_id=session.id)
所以我启动了一个 Bokeh 服务器并运行这个 python 程序:
bokeh serve --show animated.py
我得到的错误看起来像这样:
File "session.py", line 298, in push:
raise IOError("Cannot push session document because we failed to connect to the server (to start the server, try the 'bokeh serve' command)") Traceback (most recent call last):
File "/Users/.../anaconda/lib/python3.5/site-packages/bokeh/application/handlers/code_runner.py", line 81, in run
exec(self._code, module.__dict__)
File "/Users/.../Documents/.../.../animated.py", line 9, in <module>
session = push_session(curdoc())
File "/Users/.../anaconda/lib/python3.5/site-packages/bokeh/client/session.py", line 86, in push_session
session.push(document)
File "/Users/.../anaconda/lib/python3.5/site-packages/bokeh/client/session.py", line 298, in push
raise IOError("Cannot push session document because we failed to connect to the server (to start the server, try the 'bokeh serve' command)")
OSError: Cannot push session document because we failed to connect to the server (to start the server, try the 'bokeh serve' command)
我应该如何解决这个问题?如果 autoload_server() 是完全错误的方法,那么部署 Bokeh 应用程序的其他方法是什么?
最佳答案
您希望您的 Bokeh 应用看起来像:
### contents of app.py
from bokeh.client import push_session
from bokeh.embed import server_document
from bokeh.plotting import figure, curdoc
plot = figure()
plot.circle([1,2], [3,4])
doc = curdoc()
doc.add_root(plot)
您将通过以下方式提供服务:(您可能不需要 origin kwarg,YMMV)
bokeh serve app.py --allow-websocket-origin="*"
知道服务器应用程序正在运行 http://localhost:5006/ss (或者无论运行应用程序的终端说什么),您创建一个脚本以从那里加载
script = autoload_server(url='http://localhost:5006/ss') # or whatever the location of the server process is.
你将该脚本以某种方式嵌入到你的网页中(可能是将脚本加载到神社模板中),在这里它被复制粘贴到一个基本的 html 模板中:
<!doctype html>
<html lang="en">
<head> </head>
<body>
<script
src="http://localhost:5006/ss/autoload.js?bokeh-autoload-element=435ac063-5288-41b9-8375-31907dd5f124&bokeh-app-path=/ss&bokeh-absolute-url=http://localhost:5006/ss"
id="435ac063-5288-41b9-8375-31907dd5f124"
data-bokeh-model-id=""
data-bokeh-doc-id=""></script>
</body>
</html>
打开上面的 html 文档应该会打开一个包含绘图的页面。
关于python - 如何正确部署需要服务器回调的 Bokeh 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45381998/