我正在尝试学习实现bokeh自定义扩展以创建一些我希望用于数据项目的小部件。我试图按照示例here进行操作,但是在运行bokeh提供的示例扩展时遇到了麻烦。我目前正在返回
的错误ValueError:应该是HasProps的子类,得到了类'bokeh.models.sources.ColumnDataSource'
这是当DrawTool类在第80行上调用source = Instance(ColumnDataSource)时引起的。我不确定现在我到底在做什么错,但我首先想到的是与我正在使用的IDE有关使用?我目前正在使用spyder(python 3.6),并具有最新的bokeh更新2.1.0。
这是我第一次使用bokeh扩展,因此我一无所知,我一直在网上寻找帮助,但实际上找不到很多。我当前的代码与示例网站上的代码完全相同,但为了方便起见,我会将其发布在这里,
from bokeh.core.properties import Instance
from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, Tool
from bokeh.plotting import figure
from bokeh.util.compiler import TypeScript
output_file('tool.html')
TS_CODE = """
import {GestureTool, GestureToolView} from "models/tools/gestures/gesture_tool"
import {ColumnDataSource} from "models/sources/column_data_source"
import {PanEvent} from "core/ui_events"
import * as p from "core/properties"
export class DrawToolView extends GestureToolView {
model: DrawTool
//this is executed when the pan/drag event starts
_pan_start(_ev: PanEvent): void {
this.model.source.data = {x: [], y: []}
}
//this is executed on subsequent mouse/touch moves
_pan(ev: PanEvent): void {
const {frame} = this.plot_view
const {sx, sy} = ev
if (!frame.bbox.contains(sx, sy))
return
const x = frame.xscales.default.invert(sx)
const y = frame.yscales.default.invert(sy)
const {source} = this.model
source.get_array("x").push(x)
source.get_array("y").push(y)
source.change.emit()
}
// this is executed then the pan/drag ends
_pan_end(_ev: PanEvent): void {}
}
export namespace DrawTool {
export type Attrs = p.AttrsOf<Props>
export type Props = GestureTool.Props & {
source: p.Property<ColumnDataSource>
}
}
export interface DrawTool extends DrawTool.Attrs {}
export class DrawTool extends GestureTool {
properties: DrawTool.Props
__view_type__: DrawToolView
constructor(attrs?: Partial<DrawTool.Attrs>) {
super(attrs)
}
tool_name = "Drag Span"
icon = "bk-tool-icon-lasso-select"
event_type = "pan" as "pan"
default_order = 12
static init_DrawTool(): void {
this.prototype.default_view = DrawToolView
this.define<DrawTool.Props>({
source: [ p.Instance ],
})
}
}
"""
class DrawTool(Tool):
__implementation__ = TypeScript(TS_CODE)
source = Instance(ColumnDataSource)
source = ColumnDataSource(data=dict(x=[], y=[]))
plot = figure(x_range=(0, 10), y_range=(0, 10), tools=[DrawTool(source=source)])
plot.title.text = "Drag to draw on the plot"
plot.line('x', 'y', source=source)
show(plot)
答案 0 :(得分:0)
对于Bokeh 2.1.0,您的代码对我来说很好用。并且与IDE不应该有任何关系,因为它不会影响代码的运行时属性。当然,您可以手动使用Python运行脚本。
如果那仍然给您错误,那么您的Bokeh安装可能已损坏。毕竟bokeh.models.sources.ColumnDataSource
是的一个子类HasProps
。尝试从头开始创建虚拟环境。
这是我运行代码并与绘图进行交互时看到的内容:
答案 1 :(得分:0)
以防万一将来有人遇到这个问题,我想我会把我所做的事情发布给创建自定义扩展的新手,并且找不到解决方案的人。
事实证明,正如Eugene指出的那样,我的bokeh装置已损坏。我继续按照Getting Started中的Bokehs文档对bokeh进行了完全重建。分叉最新的bokeh存储库之后,我遇到了anaconda环境的问题,试图运行this step,在那里我发现一些Openssl损坏的文件阻止了我创建环境。
我发现this fix on GitHub可以为我提供一系列其他可能的解决方案。完成其余步骤后,我成功地在本地运行了自定义扩展。
TLDR:问题是Bokeh安装损坏。如果您是新手,并打算进行自定义扩展或使用bokeh模型,请分叉并克隆bokeh的本地版本,而不要依赖快速安装方法。