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

我正在尝试在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)

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

文件结构:

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

非常感谢您的帮助

python参考方案

通过稍微调整一下此示例,我找到了一个更简单的解决方案: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

Python:同时在for循环中添加到列表列表 - python

我想用for循环外的0索引值创建一个新列表,然后使用for循环添加到相同的列表。我的玩具示例是:import random data = ['t1', 't2', 't3'] masterlist = [['col1', 'animal1', 'an…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

查找字符串中的行数 - python

我正在创建一个python电影播放器​​/制作器,我想在多行字符串中找到行数。我想知道是否有任何内置函数或可以编写代码的函数来做到这一点:x = """ line1 line2 """ getLines(x) python大神给出的解决方案 如果换行符是'\n',则nlines …

字符串文字中的正斜杠表现异常 - python

为什么S1和S2在撇号位置方面表现不同?S1="1/282/03/10" S2="4/107/03/10" R1="".join({"N\'" ,S1,"\'" }) R2="".join({"N\'…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…