在Flask中调制Bokeh服务器 [英] Modulating Bokeh Servers within Flask

查看:382
本文介绍了在Flask中调制Bokeh服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录中有很多Bokeh Server文件./dir/bokeh/,假设bokeh服务器被称为bokeh1.py, bokeh2.py, bokeh3.py

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

文件结构如下:

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

然后对于每个bokeh服务器,我在flask.py中:

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)

我需要部署的bokeh服务器数量可能会迅速增长.如何基于当前设置编写一些可以为每个Bokes bokeh1.py, bokeh2.py, bokeh3.py有效生成@app.route的内容?该服务器正在Ubuntu上运行

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

推荐答案

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

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]

这篇关于在Flask中调制Bokeh服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆