将多个Bokeh服务器集成到烧瓶

时间:2019-03-20 16:54:02

标签: python flask tornado bokeh

我正在尝试在flask应用程序中运行多个Bokeh服务器,这些绘图可以使用以下方法自行正常运行:

  def getTimePlot():
   script = server_document('http://localhost:5006/timeseries')
   return render_template("displaytimeseries.html", script=script, template="Flask")    
def startPlotserver():
    server.start()
    server = Server({'/timeseries': modifyTimeSeries}, io_loop=IOLoop(), allow_websocket_origin=["localhost:8000"])
    server.io_loop.start() 
if __name__ == '__main__':
    print('Opening single process Flask app with embedded Bokeh application on http://localhost:8000/')
    print()
    print('Multiple connections may block the Bokeh app in this configuration!')
    print('See "flask_gunicorn_embed.py" for one way to run multi-process')
    app.run(port=5000, debug=True)

但是当我尝试使用这种方法将两台服务器一起嵌入烧瓶时,这就是我遇到的问题:

文件结构:

|--app4
    |---webapp2.py
    |---bokeh
          |--timeseries.py
          |--map.py

我认为我在这里Link To Question找到了解决方法 我现在尝试使用提到的类似方法将地图服务器导入flak,最终结果如下:

1。文件生成器(不确定为什么不选择它)

def build_single_handler_applications(paths, argvs=None):
applications = {}
argvs = {} or argvs
for path in paths:
    application = build_single_handler_application(path, argvs.get(path, []))
    route = application.handlers[0].url_path()
    if not route:
        if '/' in applications:
            raise RuntimeError("Don't know the URL path to use for %s" % (path))
    route = '/'
    applications[route] = application
return applications

2。查找文件并建立连接的代码

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

argvs = {}
urls = []
for i in files:
    argvs[i] = None
    urls.append(i.split('\\')[-1].split('.')[0])
host = 'http://localhost:5006/map'

apps = build_single_handler_applications(files, argvs)

bokeh_tornado = BokehTornado(apps, extra_websocket_origins=["localhost:8000"])
bokeh_http = HTTPServer(bokeh_tornado)
sockets, port = bind_sockets("localhost:8000", 5000)
bokeh_http.add_sockets(sockets)

3。调用服务器并呈现模板的代码

    @app.route('/crimeMap', methods=['GET'])
def getCrimeMap():
    bokeh_script = server_document("http://localhost:5006:%d/map" % port) 
    return render_template("displaymap1.html", bokeh_script=bokeh_script)

我正在像这样的单个命令中运行两个Bokeh服务器

bokeh serve timeseries.py map.py --allow-websocket-origin=127.0.0.1:5000

但是当我运行webapp2.py时,出现此错误:

    (env1) C:\Users\Dell1525\Desktop\flaskapp\env1\app4>webapp2.py
Traceback (most recent call last):
  File "C:\Users\Dell1525\Desktop\flaskapp\env1\app4\webapp2.py", line 113, in <module>
    apps = build_single_handler_applications(files, argvs)
  File "C:\Users\Dell1525\Desktop\flaskapp\env1\app4\webapp2.py", line 29, in build_single_handler_applications
    application = build_single_handler_application(path, argvs.get(path, []))
NameError: name 'build_single_handler_application' is not defined

仅由于此错误,我从Bokeh文档中找到并添加了build_single_handler_application函数,因此我不确定它是否是必需的或正确的。我想知道如果要定位错误或缺少导入,我会缺少什么来使这项工作有效,我在这里附加完整的flask webapp2.py代码:

Full Code

非常感谢您

1 个答案:

答案 0 :(得分:0)

通过稍微调整一下示例,我发现了一个更简单的解决方案:Link To Original Post请注意,这要求您安装龙卷风4.4.1,因为它不适用于较新的版本

技巧是像这样

分别在所有端口和具有相同套接字访问权限的不同端口上运行所有服务器
  bokeh serve timeseries.py --port 5100 --allow-websocket-origin=localhost:5567

bokeh serve map.py --port 5200 --allow-websocket-origin=localhost:5567

对于那些可能会觉得有用的人,我提供了完整的解决方案Link To Working Code

相关问题