以下是借鉴:
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
和modify_doc
的grid
字典传递给Server
,但两者都没有效果。
没有时间验证,但这是一个解决方案,从单个python脚本服务两个散景服务器
https://groups.google.com/a/continuum.io/forum/#!msg/bokeh/LYmjTXzX43E/_0dNPR1_CQAJ
我明天会检查
编辑:它的工作原理。请注意,它使用的是Bokeh 0.12.1