在bokeh中动态添加小部件 [英] Adding widgets dynamically in bokeh

查看:110
本文介绍了在bokeh中动态添加小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在bokeh中动态添加过滤器,即每次按下按钮时,都会添加一个新的过滤器.但是,在添加新的小部件后,布局会被破坏:新的小部件将被覆盖在旧的小部件上,而不是重新计算布局.代码示例

I want to add filters dynamically in bokeh, i.e. every time a button is pressed, a new filter is appended. However, the layout gets broken after a new widgets are added: new ones get written over old ones instead of the layout being recomputed. Code example

from bokeh.layouts import row, column
from bokeh.models.widgets import Button, Select
from bokeh.io import curdoc

def add_select():
    feature = Select(value='feat', options=["a"])
    dynamic_col.children.append(feature)

b1 = Button(label="Add condition", button_type="success")
b1.on_click(add_select)

b2 = Button(label="Apply", button_type="success")

dynamic_col = column()
curdoc().add_root(column(b1, dynamic_col, b2))

点击添加"按钮之前的布局

Layout before clicking "Add" button

添加选择小部件后的布局

Layout after Select widget gets added

推荐答案

为什么不使用单个列表来处理所有小部件?

Why don't you use a single list to handle all your widgets ?

from bokeh.layouts import column
from bokeh.models.widgets import Button, Select
from bokeh.io import curdoc

def add_select():
    feature = Select(value='feat', options=["a"])
    dynamic_col.children.insert(-1, feature)

b1 = Button(label="Add condition", button_type="success")
b1.on_click(add_select)

b2 = Button(label="Apply", button_type="success")

dynamic_col = column(b1, b2)
curdoc().add_root(dynamic_col)

我插入"而不是附加"窗口小部件,以使第二个按钮位于列表的末尾

I "insert" instead of "append" the widget to let the 2nd button at the end of the list

我得到了这个结果:

这篇关于在bokeh中动态添加小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆