从Flask应用程序中运行bokeh服务器应用程序?

时间:2019-09-25 14:13:51

标签: flask websocket bokeh

我希望我的Bokeh滑块应用程序位于烧瓶应用程序内。

这是我的Bokeh应用程序的代码,直接从Bokeh的sliders.py示例中获取:

Scrub the sliders to change the properties of the ``sin`` curve, or
type into the title text box to update the title of the plot.
Use the ``bokeh serve`` command to run the example by executing:
    bokeh serve sliders.py
at your command prompt. Then navigate to the URL
    http://localhost:5006/sliders
in your browser.
'''
import numpy as np

from bokeh.io import curdoc
from bokeh.layouts import row, column
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.plotting import figure

# Set up data
N = 200
x = np.linspace(0, 4*np.pi, N)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))


# Set up plot
plot = figure(plot_height=400, plot_width=400, title="my sine wave",
              tools="crosshair,pan,reset,save,wheel_zoom",
              x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])

plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)


# Set up widgets
text = TextInput(title="title", value='my sine wave')
offset = Slider(title="offset", value=0.0, start=-5.0, end=5.0, step=0.1)
amplitude = Slider(title="amplitude", value=1.0, start=-5.0, end=5.0, step=0.1)
phase = Slider(title="phase", value=0.0, start=0.0, end=2*np.pi)
freq = Slider(title="frequency", value=1.0, start=0.1, end=5.1, step=0.1)


# Set up callbacks
def update_title(attrname, old, new):
    plot.title.text = text.value

text.on_change('value', update_title)

def update_data(attrname, old, new):

    # Get the current slider values
    a = amplitude.value
    b = offset.value
    w = phase.value
    k = freq.value

    # Generate the new curve
    x = np.linspace(0, 4*np.pi, N)
    y = a*np.sin(k*x + w) + b

    source.data = dict(x=x, y=y)

for w in [offset, amplitude, phase, freq]:
    w.on_change('value', update_data)


# Set up layouts and add to document
inputs = column(text, offset, amplitude, phase, freq)

curdoc().add_root(row(inputs, plot, width=800))
curdoc().title = "Sliders"

这是我的main.py代码,它将启动Flask应用。

from flask import Flask, render_template

from bokeh.client import pull_session
from bokeh.embed import server_session

app = Flask(__name__)

@app.route('/', methods=['GET'])
def bkapp_page():

    # pull a new session from a running Bokeh server
    with pull_session(url="http://localhost:5006/sliders") as session:

        # update or customize that session
        session.document.roots[0].children[1].title.text = "Special Sliders For A Specific User!"

        # generate a script to load the customized session
        script = server_session(session_id=session.id, url='http://localhost:5006/sliders')

        # use the script in the rendered page
        return render_template("embed.html", script=script)

if __name__ == '__main__':
    app.run(port=8080)

因此,我使用bokeh serve启动了滑块应用程序,请访问它,它可以正常工作。然后,我启动我的Flask应用程序,看到的只是一个空白页面,出现控制台错误:

Firefox can’t establish a connection to the server at ws://localhost:5006/sliders/ws?bokeh-protocol-version=1.0&bokeh-session-id=Z2BPtTLxYcfINlynDozIjp5goLx6UR8RFqPFG5O8QH3R. connection.js:55

[bokeh] Failed to connect to Bokeh server Error: Could not open websocket connection.js:287

[bokeh] Lost websocket 0 connection, 1006 () connection.js:208

[bokeh] Websocket connection 0 disconnected, will not attempt to reconnect

是什么阻止了websocket连接?

这也是我的embed.html模板的样子:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <script src="https://cdn.bokeh.data.org/bokeh/release/bokeh-1.3.4.min.js"></script>
        <script src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-1.3.4.min.js"></script>
        <script src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-1.3.4.min.js"></script>
    </head>

    <body>
        {{ script|safe }}

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

也尝试过滑块示例。当您在运行bokeh服务slides.py时,您还必须明确允许第二个应用程序使用websocket-我在适用于我的powershell中运行了以下内容

docker-compose down