调整代码以通过烧瓶部署多个散景服务器图

时间:2018-02-27 20:17:00

标签: flask websocket port bokeh

以下内容来自:

https://github.com/bokeh/bokeh/blob/0.12.14/examples/howto/server_embed/flask_embed.py

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")
###my bokeh plot ###
def grid():    
    #import libraries
    from bokeh.io import curdoc
    from bokeh.models import ColumnDataSource
    from bokeh.plotting import figure
    from random import randrange

    #create figure
    f=figure(x_range=(0,11),y_range=(0,11))

    #create columndatasource
    source=ColumnDataSource(data=dict(x=[],y=[]))

    #create glyphs
    f.circle(x='x',y='y',size=8,fill_color='olive',line_color='yellow',source=source)

    #create periodic function
    def update():
        new_data=dict(x=[randrange(1,10)],y=[randrange(1,10)])
        source.stream(new_data,rollover=15)
        print(source.data)

    #add figure to curdoc and configure callback
    curdoc().add_root(f)
    curdoc().add_periodic_callback(update,1000)
######
@app.route('/', methods=['GET'])
def bkapp_page():
    script = server_document('hostname:5006/bkapp')
    return render_template("embed.html", script=script, template="Flask")
###NEW ROUTE###
@app.route('/plot')
def grid():
    bokeh_script=server_document('hostname:5006/plot') 
    return render_template("embed.html",script=script, template="Flask")
######  
def bk_worker():
    server = Server({'/bkapp': modify_doc}, allow_websocket_origin=["hostname:8000"])
    server.start()
    server.io_loop.start()

from threading import Thread
Thread(target=bk_worker).start()

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

我试图将其修改为不仅在modify_doc处提供散景图app.route("/"),还在grid处提供新的散景图@app.route('/plot')。添加新的绘图和路线后,它完全破坏,甚至modify_doc不再渲染。如何修改它以在不同的路线上渲染多个图?提前致谢。我们自己使用bokeh serve --show (plotname).py工作图我尝试了将grid中的端口更改为5007并传递包含route:bokeh的{​​{1}}字典的内容。和modify_docgrid,但都不起作用。

1 个答案:

答案 0 :(得分:0)

没有时间验证,但这是一个解决方案,从单个python脚本服务两个散景服务器

https://groups.google.com/a/continuum.io/forum/#!msg/bokeh/LYmjTXzX43E/_0dNPR1_CQAJ

我明天会检查

编辑:它有效。请注意,它使用的是Bokeh 0.12.1