在Flask中调制Bokeh服务器

问题描述 投票:2回答:1

我在目录中有许多Bokeh服务器文件说.. /dir/bokeh/,假设散景服务器被称为bokeh1.py, bokeh2.py, bokeh3.py

文件结构如下:

|--dir
    |---flask.py
    |---bokeh
          |--bokeh1.py
          |--bokeh2.py

我正在将它们全部部署在烧瓶上,如下所示:

files=[]
for file in os.listdir("/dir/bokeh/"):
    if file.endswith('.py'):
        file="bokeh/"+file
        files.append(file)

argvs = {}
urls = []
for i in files:
    argvs[i] = None
    urls.append(i.split('\\')[-1].split('.')[0])
host = 'myhost.com'

apps = build_single_handler_applications(files, argvs)

bokeh_tornado = BokehTornado(apps, extra_websocket_origins=["myhost.com"])
bokeh_http = HTTPServer(bokeh_tornado)
sockets, port = bind_sockets("myhost.com", 0)
bokeh_http.add_sockets(sockets)

然后对于每个散景服务器,我在flask.py内:

@app.route("/bokeh1")
    def bokeh1():
    bokeh_script = server_document("http://11.111.11.111:%d/bokeh1" % port) 
    return render_template("bokserv.html", bokeh_script=bokeh_script)

我需要部署的散景服务器数量可能会快速增长。我怎样才能根据我当前的设置有效地为每个散景@app.route生成一些bokeh1.py, bokeh2.py, bokeh3.py?服务器正在Ubuntu上运行

python-3.x loops flask bokeh web-development-server
1个回答
2
投票

您可以在循环中创建所有函数:

def serve(name):
    @app.route("/{}".format(name))
    def func():
        bokeh_script = server_document("http://11.111.11.111:%d/%s" % (port, name))
        return render_template("bokserv.html", bokeh_script=bokeh_script)

    func.__name__ = name
    return func

all_serve_functions = [serve(name) for name in all_names]
© www.soinside.com 2019 - 2024. All rights reserved.