如何在jupyter笔记本中使用bokeh将多选窗口小部件链接到数据表? [英] How to link a multiselect widget to a datatable using bokeh in a jupyter notebook?

查看:252
本文介绍了如何在jupyter笔记本中使用bokeh将多选窗口小部件链接到数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据表与bokeh中的多选小部件连接.我四处搜寻并收集到我需要开发一个函数来更新数据表的数据源,但是我似乎有两个问题.

I'm attempting to connect a datatable with a multiselect widget in bokeh. I've searched around and gathered that I need to develop a function to update the data source for the data table, but I seem to have two problems.

  1. 单击后,似乎无法访问多选对象的值.
  2. 收到更改后,我似乎无法将更改推送到笔记本上.

这是我的代码示例:

import pandas as pd
from bokeh.io import push_notebook
from bokeh.plotting import show, output_notebook
from bokeh.layouts import row
from bokeh.models.widgets import MultiSelect, DataTable, TableColumn
from bokeh.models import ColumnDataSource

output_notebook()

df=pd.DataFrame({'year':[2000,2001,2000,2001,2000,2001,2000,2001],
              'color':['red','blue','green','red','blue','green','red','blue'],
              'value':[ 0,1,2,3,4,5,6,7]})

columns=[TableColumn(field=x, title=x) for x in df.columns]
source=ColumnDataSource(df)
data_table=DataTable(source=source,columns=columns)

years=[2000,2001,2000,2001,2000,2001,2000,2001]

## MultiSelect won't let me store an integer value, so I convert them to strings

multi=MultiSelect(title="Select a Year", value=['2000','2001'],options=[str(y) for y in set(years)])

def update(attr,old, new):
    yr=multi.value
    yr_vals=[int(y) for y in yr]
    new_data=df[df.year.isin(yr_vals)]
    source.data=new_data
    push_notebook(handle=t)

multi.on_change('value',update)
t=show(row(multi,data_table),notebook_handle=True)

推荐答案

push_notebook是单向的.也就是说,您只能将更改从IPython内核推送到 JavaScript前端.用户界面的任何更改都不会传播回正在运行的Python内核.换句话说,on_change没什么用(无需做更多工作,请参见下文).如果您希望进行这种交互,则有一些选择:

push_notebook is uni-directional. That is, you can only push changes from the IPython kernel, to the JavaScript front end. No changes from the UI are propagated back to the running Python kernel. In other words, on_change is not useful (without more work, see below) If you want that kind of interaction, there are a few options:

  • ipywidgetspush_notebook一起使用.通常,这涉及到interact函数,该函数会自动生成一个简单的UI,该UI带有基于小部件值使用push_notebook来更新绘图等的回调.需要明确的是,这种方法使用了ipywidgets,这不是Bokeh内置小部件.您可以在此处看到完整的示例笔记本:

  • Use ipywidgets with push_notebook. Typically this involved the interact function to automatically generate a simple UI with callbacks that use push_notebook to update the plots, etc. based on the widget values. Just to be clear, this approach uses ipywidgets, which are not Bokeh built-in widgets. You can see a full example notebook here:

https://github.com /bokeh/bokeh/blob/master/examples/howto/notebook_comms/Jupyter%20Interactors.ipynb

嵌入Bokeh服务器应用程序. Bokeh服务器使Bokeh小部件上的on_change回调能够起作用.通常,这涉及创建一个定义应用程序的函数(通过指定如何创建新文档):

Embed a Bokeh server application. The Bokeh server is what makes it possible for on_change callbacks on Bokeh widgets to function. Typically this involves making a function that defines the app (by specifying how a new document is created):

def modify_doc(doc):
    df = sea_surface_temperature.copy()
    source = ColumnDataSource(data=df)

    plot = figure(x_axis_type='datetime', y_range=(0, 25))
    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))

然后在该函数上调用show:

show(modify_doc)

完整的示例笔记本在这里:

A full example notebook is here:

https://github.com/bokeh /bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb

(Hacky选项),有些人将CustomJS回调与Jupyers JS函数kernel.execute结合使用,以将值传播回内核.

(Hacky option) some people have combined CustomJS callbacks with Jupyers JS function kernel.execute to propagate values back to the kernel.

这篇关于如何在jupyter笔记本中使用bokeh将多选窗口小部件链接到数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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