首页 > 解决方案 > 如何在独立设备中嵌入散景服务器

问题描述

我正在尝试将散景服务器嵌入到此处提供的独立文档中。我很迷茫,无法理解提供的示例。我尝试在下面的示例中实现它,但是当我运行它时并没有显示应用程序。有人可以告诉我如何创建这个独立的吗?

谢谢

from bokeh.io import show, curdoc
from bokeh.models import  ColumnDataSource, Legend, CustomJS, Select
from bokeh.plotting import figure
from bokeh.palettes import Category10
from bokeh.layouts import row
import pandas as pd
from bokeh.server.server import Server

def test(doc):
    df0 = pd.DataFrame({'x': [1, 2, 3], 'Ay' : [1, 5, 3], 'A': [0.2, 0.1, 0.2], 'By' : [2, 4, 3], 'B':[0.1, 0.3, 0.2]})

    columns = ['A', 'B']

    tools_to_show = 'box_zoom,save,hover,reset'
    p = figure(plot_height =300, plot_width = 1200, 
               toolbar_location='above',
               tools=tools_to_show)

    legend_it = []
    color = Category10[10]
    columns = ['A', 'B']
    source = ColumnDataSource(df0)
    c = []
    for i, col in enumerate(columns):
        c.append(p.line('x', col, source=source, name=col, color=color[i]))
        legend_it.append((col, [c[i]]))


    legend = Legend(items=legend_it, location=(5,114))#(0, -60))

    p.add_layout(legend, 'right')

    select = Select(title="color", value=color[0],
                    options = color)
    callbacks = CustomJS(args=dict(renderer=c[0], select=select), code ="""
        renderer.glyph.line_color = select.value;
        renderer.trigger('change')
    """)

    select.callback = callbacks

    layout = row(select, p)

    curdoc().add_root(layout)

server = Server(test)
server.start()

根据接受的答案,我修改了代码并且它可以工作:

from bokeh.models import  ColumnDataSource, Legend, CustomJS, Select
from bokeh.plotting import figure
from bokeh.palettes import Category10
from bokeh.layouts import row
import pandas as pd
from bokeh.server.server import Server


def test(doc):
    df0 = pd.DataFrame({'x': [1, 2, 3], 'Ay' : [1, 5, 3], 'A': [0.2, 0.1, 0.2], 'By' : [2, 4, 3], 'B':[0.1, 0.3, 0.2]})

    columns = ['A', 'B']

    tools_to_show = 'box_zoom,save,hover,reset'
    p = figure(plot_height =300, plot_width = 1200, 
               toolbar_location='above',
               tools=tools_to_show)

    legend_it = []
    color = Category10[10]
    columns = ['A', 'B']
    source = ColumnDataSource(df0)
    c = []
    for i, col in enumerate(columns):
        c.append(p.line('x', col, source=source, name=col, color=color[i]))
        legend_it.append((col, [c[i]]))


    legend = Legend(items=legend_it, location=(5,114))#(0, -60))

    p.add_layout(legend, 'right')

    select = Select(title="color", value=color[0],
                    options = color)
    callbacks = CustomJS(args=dict(renderer=c[0], select=select), code ="""
        renderer.glyph.line_color = select.value;
        renderer.trigger('change')
    """)

    select.callback = callbacks

    layout = row(select, p)

    doc.add_root(layout)


server = Server({'/': test}, num_procs=1)
server.start()

server.io_loop.add_callback(server.show, "/")
server.io_loop.start()

标签: pythonpython-3.xbokeh

解决方案


在 bokeh github 站点上,有一个完整的工作示例,可以在不使用“bokeh serve ...”的情况下以编程方式运行 bokeh 服务器:

https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/standalone_embed.py

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

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")

# Setting num_procs here means we can't touch the IOLoop before now, we must
# let Server handle that. If you need to explicitly handle IOLoops then you
# will need to use the lower level BaseServer class.
server = Server({'/': modify_doc}, num_procs=4)
server.start()

if __name__ == '__main__':
    print('Opening Bokeh application on http://localhost:5006/')

    server.io_loop.add_callback(server.show, "/")
    server.io_loop.start()

如果我们将此作为模板并集成您的代码,它看起来像这样:

from bokeh.models import  ColumnDataSource, Legend, CustomJS, Select
from bokeh.plotting import figure
from bokeh.palettes import Category10
from bokeh.layouts import row
import pandas as pd
from bokeh.server.server import Server

from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature

def test(doc):
    df0 = pd.DataFrame({'x': [1, 2, 3], 'Ay' : [1, 5, 3], 'A': [0.2, 0.1, 0.2], 'By' : [2, 4, 3], 'B':[0.1, 0.3, 0.2]})

    columns = ['A', 'B']

    tools_to_show = 'box_zoom,save,hover,reset'
    p = figure(plot_height =300, plot_width = 1200, 
               toolbar_location='above',
               tools=tools_to_show)

    legend_it = []
    color = Category10[10]
    columns = ['A', 'B']
    source = ColumnDataSource(df0)
    c = []
    for i, col in enumerate(columns):
        c.append(p.line('x', col, source=source, name=col, color=color[i]))
        legend_it.append((col, [c[i]]))


    legend = Legend(items=legend_it, location=(5,114))#(0, -60))

    p.add_layout(legend, 'right')

    select = Select(title="color", value=color[0],
                    options = color)
    callbacks = CustomJS(args=dict(renderer=c[0], select=select), code ="""
        renderer.glyph.line_color = select.value;
        renderer.trigger('change')
    """)

    select.callback = callbacks

    layout = row(select, p)

    doc.add_root(layout)


server = Server({'/': test}, num_procs=1)
server.start()

if __name__ == '__main__':
    print('Opening Bokeh application on http://localhost:5006/')

    server.io_loop.add_callback(server.show, "/")
    server.io_loop.start()

推荐阅读