使用flask_embed.py在bokeh中嵌入散景服务时出错

时间:2018-02-01 19:27:43

标签: javascript python flask bokeh

我想将bokeh服务嵌入烧瓶框架中。因此,我使用了一段名为" flask_embed.py"的在线代码,代码和相应的HTML附件如下。但是,运行后我遇到了错误。以前有人遇到过这个问题吗?

错误是:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "flaskBokehHighchart.py", line 44, in bk_worker
    server = Server({'/bkapp': modify_doc}, allow_websocket_origin=["localhost:5000"])
  File "C:\ProgramData\Anaconda3\lib\site-packages\bokeh\server\server.py", line 390, in __init__
    sockets, self._port = bind_sockets(self.address, self.port)
  File "C:\ProgramData\Anaconda3\lib\site-packages\bokeh\server\util.py", line 32, in bind_sockets
    ss = netutil.bind_sockets(port=port or 0, address=address)
  File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\netutil.py", line 197, in bind_sockets
    sock.bind(sockaddr)
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted

''' 带烧瓶的Python代码:

from flask import Flask, render_template

from bokeh.embed import server_document
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure
from bokeh.server.server import Server
from bokeh.themes import Theme

from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature

app = Flask(__name__)

def modify_doc(doc):
    df = sea_surface_temperature.copy()
    source = ColumnDataSource(data=df)

    plot = figure(x_axis_type='datetime', y_range=(0, 25), y_axis_label='Temperature (Celsius)',
                  title="Sea Surface Temperature at 43.18, -70.43")
    plot.line('time', 'temperature', source=source)

    def callback(attr, old, new):
        if new == 0:
            data = df
        else:
            data = df.rolling('{0}D'.format(new)).mean()
        source.data = ColumnDataSource(data=data).data

    slider = Slider(start=0, end=30, value=0, step=1, title="Smoothing by N Days")
    slider.on_change('value', callback)

    doc.add_root(column(slider, plot))

    doc.theme = Theme(filename="theme.yaml")

@app.route('/', methods=['GET'])
def bkapp_page():
    script = server_document('http://localhost:5006/bkapp')
    return render_template("embed.html", script=script, template="Flask")

def bk_worker():
    # Can't pass num_procs > 1 in this configuration. If you need to run multiple
    # processes, see e.g. flask_gunicorn_embed.py
    server = Server({'/bkapp': modify_doc}, allow_websocket_origin=["localhost:8000"])
    server.start()
    server.io_loop.start()

from threading import Thread
Thread(target=bk_worker).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=8000)

'''

HTML模板名为" embed.html":

<!doctype html>

<html lang="en">

  <head>
    <meta charset="utf-8">
    <title>App</title>
  </head>

  <body>
    <div>HERE IS SOME CUSTOM STUFF</div>
    {{ script|safe }}
  </body>

</html>

1 个答案:

答案 0 :(得分:0)

您系统上的其他一些进程已经在使用端口5006.最可能的解释是您已经在另一个窗口中运行了另一个Bokeh服务器。您可以确保关闭其他Bokeh服务器实例,或者您可以更改上面的示例代码以使用其他端口。您想要更改server_document行,例如:

script = server_document('http://localhost:9090/bkapp')

然后还要更改Server行以使用相同的新端口:

server = Server({'/bkapp': modify_doc}, allow_websocket_origin=["localhost:8000"],
                # add this argument:
                port=9090)