我正在使用bokeh
(0.12.6
)實用程序進行交互式數據處理,我將在包中進行部署。這個想法是,用戶可以運行一些例程module.utility()
來啓動散景服務器,在瀏覽器中啓動應用程序,當標籤或瀏覽器關閉時,服務器將停止。以編程方式運行散景服務器以在本地瀏覽器中顯示
如果我運行bokeh serve --show myapp
,我的應用程序啓動正常,但在使用下面介紹的方法連接到本地主機時掛起。我已經檢查過處理程序,看起來應該如此。
這是一個合理的事情要做,我是否正確地去做?
目錄格式
<installed module path>/myapp
└── main.py
凡./myapp
將駐留在venv/lib/python3.5/site-packages/mymodule
等
main.py
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.plotting import figure
from bokeh.models.sources import ColumnDataSource
source = ColumnDataSource(dict(x=list(range(5)), y=list(range(5))))
p = figure(width=300, height=300, tools=[], toolbar_location=None)
p.line(x='x', y='y', source=source)
curdoc().add_root(column(p, sizing_mode='scale_width'))
運行腳本
def run_single_server(abs_app_path, port=5000):
'''Run bokeh application for single session from server`'''
from bokeh.application import Application
from bokeh.application.handlers import DirectoryHandler
from bokeh.server.server import Server
import os
app_name = os.path.split(abs_app_path)[1]
url = '/{}'.format(app_name)
# Start a bokeh server
apps = {url:Application(DirectoryHandler(filename=abs_app_path))}
server = Server(apps, port=port)
server.start()
server.show(url)
# somehow wait for session to end, perhaps using `server_lifecycle.py`
server.stop()
return
def utility():
import mymodule
module_path = os.path.split(mymodule.__file__)[0]
abs_app_path = os.path.join(module_path, 'myapp')
run_single_server(abs_app_path, port=5000)
return
也許有一個常規的主__init__.py
,並像這樣的工作:
import mymodule
mymodule.utility()
# 1. Browser launches
# 2. user does stuff
# 3. user closes window
# 4. bokeh server is shutdown
更新 我發現build_single_handler_application
常規,嘗試過,但它也似乎掛起。
from bokeh.command.util import build_single_handler_application
import os
app_name = os.path.split(abs_app_path)[1]
url = '/{}'.format(app_name)
# Start a bokeh server
apps = build_single_handler_application(abs_app_path)