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

Python 如何正确部署需要服务器回调的Bokeh应用程序?,python,session,bokeh,deploying,Python,Session,Bokeh,Deploying,我想使用Bokeh服务器来保存我的绘图和数据,这样我就可以将我的Bokeh应用程序嵌入到网站中。我试图在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()

我想使用Bokeh服务器来保存我的绘图和数据,这样我就可以将我的Bokeh应用程序嵌入到网站中。我试图在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)
文件“session.py”,第298行,在push中:
raise IOError(“无法推送会话文档,因为我们无法连接到服务器(若要启动服务器,请尝试“bokeh serve”命令)”)回溯(最近一次调用):
文件“/Users/../anaconda/lib/python3.5/site packages/bokeh/application/handlers/code_runner.py”,第81行,运行中
exec(自我代码、模块指令)
文件“/Users/../Documents/../…/animated.py”,第9行,在
会话=推送会话(curdoc())
文件“/Users/../anaconda/lib/python3.5/site packages/bokeh/client/session.py”,第86行,在push_会话中
session.push(文档)
文件“/Users/../anaconda/lib/python3.5/site packages/bokeh/client/session.py”,第298行,在push中
raise IOError(“无法推送会话文档,因为我们无法连接到服务器(要启动服务器,请尝试“bokeh serve”命令)”)
OSError:无法推送会话文档,因为无法连接到服务器(若要启动服务器,请尝试“bokeh serve”命令)

我应该如何着手解决这个问题?如果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)
您将通过以下方式提供此服务:(您可能不需要原始kwarg,YMMV)

知道服务器应用程序正在运行(或运行应用程序的终端所说的任何内容),您可以通过

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文档应该会打开一个带有绘图的页面。

您希望您的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)
您将通过以下方式提供此服务:(您可能不需要原始kwarg,YMMV)

知道服务器应用程序正在运行(或运行应用程序的终端所说的任何内容),您可以通过

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文档应该会打开一个带有绘图的页面。

我使用了错误的自动加载服务器kwargs。我已经编辑了正确性。快速跟进:如果我想让这个应用程序在个人网站上动态运行,我会添加到Bokeh CDN的链接吗?或者我该怎么做?不,Bokeh服务器应用程序总是自动加载Bokeh服务器本身的资源。我使用了错误的自动加载服务器kwargs。我已经编辑了正确性。快速跟进:如果我想让这个应用程序在个人网站上动态运行,我会添加到Bokeh CDN的链接吗?或者我该怎么做呢?不,Bokeh服务器应用程序总是自动加载Bokeh服务器本身的资源。