运行 带有交互式小部件的散景
Running Bokeh with interactive Widgets
我想 运行 一个带有交互式小部件的 Bokeh 应用程序,但无法使其完全正常工作。
我的代码demo.py
:
# imports
import pandas as pd
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Dropdown
from bokeh.plotting import figure
from bokeh.sampledata.iris import flowers
# Data
df = pd.DataFrame({'x': flowers['sepal_length'], 'y': flowers['sepal_width'], 'species': flowers['species']})
# Source
SPECIES = 'versicolor'
source = ColumnDataSource(df.loc[df.species == SPECIES])
# Create plots and widgets
plot = figure()
plot.circle(x= 'x', y='y', source=source)
menu = [("setosa", "setosa"), ("versicolor", "versicolor"), None, ("virginica", "virginica")]
dropdown = Dropdown(label="Dropdown species", button_type="warning", menu=menu)
# Add callback to widgets
def callback(attr, old, new):
SPECIES = dropdown.value
source.data=ColumnDataSource(df.loc[df.species == SPECIES])
dropdown.on_change('value', callback)
# Arrange plots and widgets in layouts
layout = column(dropdown, plot)
curdoc().add_root(layout)
当我使用 bokeh serve --show demo.py
从命令行界面 运行 这个应用程序时,它 returns 一个带有情节的 HTML 页面。下拉菜单似乎有效,但当从下拉菜单中选择一个值时,绘图不会改变。
有什么解决方法的建议吗?
您没有为 source.data
分配正确的值。该值需要是将列名映射到数据的 lists/arrays 的常规 Python 字典。在文档和示例中有多种方法可以做到这一点,但一个好的方法是使用 CDS 的 from_df
class 方法来生成正确类型的字典:
source.data = ColumnDataSource.from_df(df.loc[df.species == SPECIES])
该行使您的代码按预期工作。
仅供参考,您的代码在服务器控制台输出中生成错误(正如预期的那样):
error handling message Message 'PATCH-DOC' (revision 1): ValueError("expected an element of ColumnData(String, Seq(Any)), got ColumnDataSource(id='44e09b5e-133b-4c1b-987b-cbf80b803401', ...)",)
作为一个温和的建议,在 SO 问题中包含此类错误总是一个好主意。
我想 运行 一个带有交互式小部件的 Bokeh 应用程序,但无法使其完全正常工作。
我的代码demo.py
:
# imports
import pandas as pd
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Dropdown
from bokeh.plotting import figure
from bokeh.sampledata.iris import flowers
# Data
df = pd.DataFrame({'x': flowers['sepal_length'], 'y': flowers['sepal_width'], 'species': flowers['species']})
# Source
SPECIES = 'versicolor'
source = ColumnDataSource(df.loc[df.species == SPECIES])
# Create plots and widgets
plot = figure()
plot.circle(x= 'x', y='y', source=source)
menu = [("setosa", "setosa"), ("versicolor", "versicolor"), None, ("virginica", "virginica")]
dropdown = Dropdown(label="Dropdown species", button_type="warning", menu=menu)
# Add callback to widgets
def callback(attr, old, new):
SPECIES = dropdown.value
source.data=ColumnDataSource(df.loc[df.species == SPECIES])
dropdown.on_change('value', callback)
# Arrange plots and widgets in layouts
layout = column(dropdown, plot)
curdoc().add_root(layout)
当我使用 bokeh serve --show demo.py
从命令行界面 运行 这个应用程序时,它 returns 一个带有情节的 HTML 页面。下拉菜单似乎有效,但当从下拉菜单中选择一个值时,绘图不会改变。
有什么解决方法的建议吗?
您没有为 source.data
分配正确的值。该值需要是将列名映射到数据的 lists/arrays 的常规 Python 字典。在文档和示例中有多种方法可以做到这一点,但一个好的方法是使用 CDS 的 from_df
class 方法来生成正确类型的字典:
source.data = ColumnDataSource.from_df(df.loc[df.species == SPECIES])
该行使您的代码按预期工作。
仅供参考,您的代码在服务器控制台输出中生成错误(正如预期的那样):
error handling message Message 'PATCH-DOC' (revision 1): ValueError("expected an element of ColumnData(String, Seq(Any)), got ColumnDataSource(id='44e09b5e-133b-4c1b-987b-cbf80b803401', ...)",)
作为一个温和的建议,在 SO 问题中包含此类错误总是一个好主意。