如何在独立版中嵌入散景服务器

问题描述 投票:1回答:1

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

谢谢

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()
python python-3.x bokeh
1个回答
4
投票

有一个完整的工作示例,用于以编程方式运行散景服务器,而不使用bokeh github站点上的“散景服务...”:

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()
© www.soinside.com 2019 - 2024. All rights reserved.