首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在单机版中嵌入bokeh服务器

如何在单机版中嵌入bokeh服务器
EN

Stack Overflow用户
提问于 2018-08-12 01:32:59
回答 1查看 4.3K关注 0票数 4

我正在尝试将bokeh服务器嵌入到一个独立的文档中,就像呈现的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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-12 04:12:51

有一个完整的工作示例,可以在不使用"bokeh serv...“的情况下以编程方式运行bokeh服务器。在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()
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51802159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档