2017-07-28 130 views
1

我想使用Bokeh服務器來保存我的情節和數據,所以我可以將我的Bokeh應用程序嵌入到網站中。我試圖重新在散景0.12.6文檔中給出的example如何正確部署需要服務器回調的Bokeh應用程序?

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) 

所以我開始一個背景虛化服務器和運行這個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應用程序的其他方法是什麼?

回答

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,因人而異)

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文檔應該打開一個網頁與情節。

+1

我使用了錯誤的autoload_server kwargs。我從此編輯過正確性。 –

+0

快速跟進:如果我想要在個人網站上動態運行此應用程序,我會添加到Bokeh CDN的鏈接嗎?或者我會怎麼去做呢? – swetharevanur

+1

不,Bokeh服務器應用程序始終會自動加載散景服務器本身的資源。 – bigreddot

相關問題