在 Flask 中调制 Bokeh 服务器

Modulating Bokeh Servers within Flask

我在一个目录中有许多散景服务器文件说../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)

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

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

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]