Modulating Bokeh Servers within Flask

两盒软妹~` 提交于 2019-12-10 18:08:03

问题


I have numerous Bokeh Server files in a directory say.. /dir/bokeh/, assume the bokeh servers are called bokeh1.py, bokeh2.py, bokeh3.py

The file structure is like so:

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

I am deploying them all on flask like so:

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)

Then for each bokeh server, I have within 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)

The number of bokeh servers I need to deploy could grow quickly. How can I write something that will generate the @app.route for each of the bokehs bokeh1.py, bokeh2.py, bokeh3.py efficiently based on my current setup? The server is being run on Ubuntu


回答1:


You can create all the functions in a loop:

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]


来源:https://stackoverflow.com/questions/50256853/modulating-bokeh-servers-within-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!