gpt4 book ai didi

python - 以编程方式运行 Bokeh 服务器以在本地浏览器中显示

转载 作者:太空狗 更新时间:2023-10-30 02:54:37 25 4
gpt4 key购买 nike

我正在使用 bokeh 进行交互式数据操作( 0.12.6 ) 我将在一个包中部署的实用程序。这个想法是用户可以运行一些例程 module.utility()这将启动 bokeh 服务器,在浏览器中启动应用程序,当标签页或浏览器关闭时,服务器将停止。

如果我运行 bokeh serve --show myapp,我的应用程序可以正常启动,但是当使用我下面描述的方法连接到本地主机时它会挂起。我已经检查了处理程序,一切看起来都应该如此。

Is this a reasonable thing to do, and am I going about it correctly?

目录格式

<installed module path>/myapp
└── main.py

在哪里./myapp将居住在 venv/lib/python3.5/site-packages/mymodule

主.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)

最佳答案

看来我遇到了一些问题。我最终找到并改编了我在邮件组 here 上找到的一些代码对于我的用例。

我设法通过使用单独的进程使一切正常工作:1) 启动服务器,2) 使用 webbrowser 启动应用程序 url,以及 3) 检查关闭的连接并关闭。

我想我也许可以取消启动 tornado 服务器实例,就像在我改编的 flask 示例中所做的那样,但我很高兴在这里。

注意:此示例使用单文件应用,但您也可以传递目录格式应用的路径。

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)

关于python - 以编程方式运行 Bokeh 服务器以在本地浏览器中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45192698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com