使用散景在python烧瓶上运行实时绘图

时间:2016-09-15 15:55:31

标签: python heroku flask ibm-cloud bokeh

我正在尝试使用python flask(heroku样板)和散景作为绘图库来建立一个简单的实时绘图场景。在绘制初始图形(数据来自批处理场景)之后,图形应该将新数据点更新为图形。在本地,代码完全遵循official documentation。但是当我尝试将代码合并到我的Bluemix应用程序中时,它会抛出一个IOError,表明没有要连接的散景服务器:

IOError: Cannot push session document because we failed to connect to the server (to start the server, try the 'bokeh serve' command)

所以我尝试在运行Bluemix上启动散景服务器

call(["bokeh", "serve"])

,但它不起作用。

附上他们是小用例的完整代码:

    @app.route('/testBokeh', methods=['GET', 'POST'])
    def PlotWithBokeh():
    sumSessionCounter()
    global X
    global Y
    global n
    alpha = request.form['alpha']
    beta = request.form['beta']
    n = 100
    X = np.random.uniform(0, 100, n)
    e = np.random.normal(0, 1, n)
    Y = int(alpha) + int(beta) * X + e
    x = X.reshape(n, 1)
    y = Y.reshape(n, 1)
    regr = linear_model.LinearRegression()
    regr.fit(x, y)
    call(["bokeh", "serve"])

    myfigure = figure(plot_width=800, plot_height=400)
    myfigure.line(x.flatten(), regr.predict(x).flatten(), line_width=20, color="red")
    datacoords = ColumnDataSource(data=dict(x=X, y=Y))
    linea = myfigure.scatter("x", "y", source=datacoords)

    @linear(m=0.05, b=0) #step will increment by 0.05 every time
    def update(step): 
        global X 
        global Y
        new_x = np.random.uniform(0, 5, 1)
        new_y = 10 + 20 * new_x + np.random.normal(0, 1, 1)

        X = np.append(X, new_x)
        Y = np.append(Y, new_y)
        linea.data_source.data["x"] = X
        linea.data_source.data["y"] = Y

    # Configure resources to include BokehJS inline in the document.
    # For more details see:
    #   http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed
    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()

    # For more details see:
    #   http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components
    script, div = components(myfigure, INLINE)
    html = render_template(
        'plotData.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources
    )
    set_curdoc(html)
    #open a session to keep our local document in sync with server
    session = push_session(curdoc())
    curdoc().add_periodic_callback(update, 10000) #period in ms
    session.show(myfigure)
    session.loop_until_closed()

编辑:

我指向的方向是,我在散景服务器中运行整个应用程序。为此,我将以下更改合并到我的Procfile中

web: python -m bokeh serve /welcome.py

现在它根本没有启动应用程序,但是查看日志文件让我假设它可能是一个好方法:

2016-09-16 09:59:15,087 Starting Bokeh server version 0.12.2
2016-09-16 09:59:15,111 Starting Bokeh server on port 5006 with applications at paths ['/welcome']
2016-09-16 09:59:15,111 Starting Bokeh server with process id: 29

ERROR:

Instance (index 0) failed to start accepting connections

App instance exited with guid 618110f2-f477-4427-bd79-6599b4fc2c90 payload: {"cc_partition"=>"default", "droplet"=>"618110f2-f477-4427-bd79-6599b4fc2c90", "version"=>"f5212f2d-ec88-40bc-9855-b25213cfc55c", "instance"=>"13d1c8b9ba754088ae368519b098cc20", "index"=>0, "reason"=>"CRASHED", "exit_status"=>-1, "exit_description"=>"failed to accept connections within health check timeout", "crash_timestamp"=>1474019881}

我打算这是端口解析或其他一些内部Bluemix问题的问题。

由于Bluemix正在使用Cloud Foundry,我尝试联系本地端口80,CF根据我在此域中的(有限)知识使用该端口。不会抛出权限错误:

2016-09-16 10:14:53,226 Starting Bokeh server version 0.12.2
ERROR:[Errno 13] Permission denied
Instance (index 0) failed to start accepting connections

0 个答案:

没有答案