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

How to correctly deploy Bokeh app requiring server callbacks?

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

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

### 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(或者带有 运行 应用程序的终端说的任何内容),您创建一个脚本以通过 [=15 从那里加载=]

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 文档应该会打开一个包含情节的页面。