2017-07-19 49 views
2

我正在使用bokeh0.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) 

回答

2

看起來我有幾個問題。我最終找到並修改了我在郵件組here上找到的一些代碼,以供我使用。

我設法讓一切工作,通過使用單獨的過程1)啓動服務器,2)啓動應用程序的URL與webbrowser,和3)檢查關閉連接並關閉。

我想我可能會廢除啓動tornado服務器實例,就像在我改編的燒瓶示例中所做的那樣,但我很高興在這裏。

說明:該示例使用單個文件應用程序,但您也可以傳遞目錄格式化應用程序的路徑。

def create_bokeh_server(io_loop, files, argvs, host, port): 
    '''Start bokeh server with applications paths''' 
    from bokeh.server.server import Server 
    from bokeh.command.util import build_single_handler_applications 

    # Turn file paths into bokeh apps 
    apps = build_single_handler_applications(files, argvs) 

    # kwargs lifted from bokeh serve call to Server, with created io_loop 
    kwargs = { 
     'io_loop':io_loop, 
     'generate_session_ids':True, 
     'redirect_root':True, 
     'use_x_headers':False, 
     'secret_key':None, 
     'num_procs':1, 
     'host': host, 
     'sign_sessions':False, 
     'develop':False, 
     'port':port, 
     'use_index':True 
    } 
    server = Server(apps,**kwargs) 

    return server 


def run_single_app(files, port=5000, new='tab'): 

    def start_bokeh(io_loop): 
     '''Start the `io_loop`''' 
     io_loop.start() 
     return None 

    def launch_app(host, app_name, new): 
     '''Lauch app in browser 

     Ideally this would `bokeh.util.browser.view()`, but it doesn't work 
     ''' 
     import webbrowser 

     # Map method strings to webbrowser method 
     options = {'current':0, 'window':1, 'tab':2} 

     # Concatenate url and open in browser, creating a session 
     app_url = 'http://{}/{}'.format(host, app_name) 
     print('Opening `{}` in browser'.format(app_url)) 
     webbrowser.open(app_url, new=options[new]) 

     return None 

    def server_loop(server, io_loop): 
     '''Check connections once session created and close on disconnect''' 
     import time 

     connected = [True,] 
     session_loaded = False 
     while any(connected): 

      # Check if no session started on server 
      sessions = server.get_sessions() 
      if not session_loaded: 
       if sessions: 
        session_loaded = True 
      # Once 1+ sessions started, check for no connections 
      else: 
       # List of bools for each session 
       connected = [True,]*len(sessions) 
       # Set `connected` item false no connections on session 
       for i in range(len(sessions)): 
        if sessions[i].connection_count == 0: 
         connected[i] = False 
      # Keep the pace down 
      time.sleep(2) 

     # Stop server once opened session connections closed 
     io_loop.stop() 

     return None 

    import os 
    import threading 
    import tornado.ioloop 
    import tornado.autoreload 
    import time 

    # Initialize some values, sanatize the paths to the bokeh plots 
    argvs = {} 
    app_names = [] 
    for path in files: 
     argvs[path] = None 
     app_names.append(os.path.splitext(os.path.split(path)[1])[0]) 

    # Concate hostname/port for creating handlers, launching apps 
    host = 'localhost:{}'.format(port) 

    # Initialize the tornado server 
    io_loop = tornado.ioloop.IOLoop.instance() 
    tornado.autoreload.start(io_loop) 

    # Add the io_loop to the bokeh server 
    server = run_bokeh_server(io_loop, files, argvs, host, port) 

    print('Starting the server on {}'.format(host)) 
    args = (io_loop,) 
    th_startup = threading.Thread(target=start_bokeh, args=args) 
    th_startup.start() 

    # Launch each application in own tab or window 
    th_launch = [None,]*len(app_names) 
    for i in range(len(app_names)): 
     args = (host, app_names[i], new) 
     th_launch[i] = threading.Thread(target=launch_app, args=args) 
     th_launch[i].start() 
     # Delay to allow tabs to open in same browser window 
     time.sleep(2) 

    # Run session connection test, then stop `io_loop` 
    args = (server, io_loop) 
    th_shutdown = threading.Thread(target=server_loop, args=args) 
    th_shutdown.start() 

    return None 

if __name__ == "__main__": 

import os 
files = [os.path.join('bokeh', fname) for fname in ['ex1.py','ex2.py']] 
run_single_app(files, port=5006) 
相關問題