如何正确部署需要服务器回调的Bokeh应用程序?

时间:2017-07-28 20:34:44

标签: python session bokeh deploying

我想使用Bokeh服务器来保存我的情节和数据,因此我可以将我的Bokeh应用程序嵌入到网站中。我试图重新创建Bokeh 0.12.6文档中给出的example

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应用程序的其他方法是什么?

1 个答案:

答案 0 :(得分:4)

您希望您的散景应用看起来像:

### 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)

您将通过以下方式提供此服务:(您可能不需要原始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.

您以某种方式将该脚本嵌入您的网页(可能将脚本加载到jinja模板中),将其复制粘贴到基本的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文档应打开带有图表的页面。