指定组合框的位置

Specify position of combo box

我有以下生成组合框 (Select) 和绘图的脚本:

import bokeh.plotting as bk
from bokeh.models import ColumnDataSource, Plot
from bokeh.models.widgets import Select
from bokeh.simpleapp import simpleapp

data = {"a": {"x": [1,2,3], "y": [1,2,3]},
        "b": {"x": [3,2,1], "y": [1,2,3]},
        "c": {"x": [2,2,2], "y": [1,2,3]},}


options = ["a", "b", "c"]
select1 = Select(name = 'ticker1', value = options[0], options = options)

@simpleapp(select1)
def test_layout(ticker1):
    p = bk.figure(title = "layout test")

    chart_data = data[ticker1]

    df = ColumnDataSource(data = chart_data)
    p.circle(x = chart_data["x"], y = chart_data["y"])

    return p

test_layout.route("/bokeh/layout/")

这很好用。但是,我发现无法指定组合框的位置。

我的目标是,组合框显示在绘图上方(而不是左侧)。这怎么可能?

您可以通过实现一个实际构建您的布局的函数来自定义您的 simpleapp 布局,然后用 layout 装饰器包装它。在您的情况下,它将是 test_layout.layout 装饰器。为此,您需要稍微更改代码。这是一个可以满足您需要的版本:

import bokeh.plotting as bk
from bokeh.models import ColumnDataSource, Plot
from bokeh.models.widgets import Select, AppVBox
from bokeh.simpleapp import simpleapp

data = {"a": {"x": [1,2,3], "y": [1,2,3]},
        "b": {"x": [3,2,1], "y": [1,2,3]},
        "c": {"x": [2,2,2], "y": [1,2,3]},}


options = ["a", "b", "c"]
select1 = Select(name = 'ticker1', value = options[0], options =     options)

@simpleapp(select1)
def test_layout(ticker1):
    p = bk.figure(title = "layout test")

    chart_data = data[ticker1]

    df = ColumnDataSource(data = chart_data)
    p.circle(x = chart_data["x"], y = chart_data["y"])

    return {'plot': p}

@test_layout.layout
def layout(app):
    return AppVBox(app=app, children=['ticker1', 'plot'])

test_layout.route("/bokeh/layout/")