4

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

machump
  • 1,207
  • 2
  • 20
  • 41

1 Answers1

2

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]
HYRY
  • 94,853
  • 25
  • 187
  • 187
  • for some reason, it is returning, ` View function mapping is overwriting an existing endpoint function: func`. What might be giving rise to this? – machump May 10 '18 at 15:47
  • New finding. You must set the `endpoint` parameter on the `app.route` line. To be safe, ensure `name` does not include the extension `.py` – machump May 10 '18 at 18:48