bokeh: Custom Bokeh Model from documentation raises exception
ALL software version info (bokeh, python, notebook, OS, browser, any other relevant packages)
Bokeh: 1.4.0 Python: 3.7.4 Jupyter: 1.0.0 OS: Windows 8.1 Browser: Chrome
Description of expected behavior and the observed behavior
I’m trying to learn how to create custom bokeh models. I started out trying the Custom model in https://docs.bokeh.org/en/latest/docs/user_guide/extensions.html. But it raises the below exception.

Expected: It works without exception Actual: Exception
Complete, minimal, self-contained example code that reproduces the issue
I have the file custom.ipynb
from bokeh.core.properties import Instance, String
from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.models import HTMLBox, Slider
class Custom(HTMLBox):
__implementation__ = "custom.ts"
text = String(default="Custom text")
slider = Instance(Slider)
slider = Slider(start=0, end=10, step=0.1, value=0, title="value")
custom = Custom(text="Special Slider Display", slider=slider)
layout = column(slider, custom)
show(layout)
and the file custom.ts in the same folder
import { HTMLBox, HTMLBoxView } from "models/layouts/html_box"
import { div } from "core/dom"
import * as p from "core/properties"
export class CustomView extends HTMLBoxView {
connect_signals(): void {
super.connect_signals()
// Set BokehJS listener so that when the Bokeh slider has a change
// event, we can process the new data.
this.connect(this.model.slider.change, () => {
this.render()
this.invalidate_layout()
})
}
render(): void {
// BokehjS Views create <div> elements by default, accessible as
// ``this.el``. Many Bokeh views ignore this default <div>, and instead
// do things like draw to the HTML canvas. In this case though, we change
// the contents of the <div>, based on the current slider value.
super.render()
this.el.appendChild(div({
style: {
padding: '2px',
color: '#b88d8e',
backgroundColor: '#2a3153',
},
}, `${this.model.text}: ${this.model.slider.value}`))
}
}
export class Custom extends HTMLBox {
slider: { value: string }
// The ``__name__`` class attribute should generally match exactly the name
// of the corresponding Python class. Note that if using TypeScript, this
// will be automatically filled in during compilation, so except in some
// special cases, this shouldn't be generally included manually, to avoid
// typos, which would prohibit serialization/deserialization of this model.
static __name__ = "Custom"
static init_Custom(): void {
// If there is an associated view, this is typically boilerplate.
this.prototype.default_view = CustomView
// The this.define() block adds corresponding "properties" to the JS model.
// These should normally line up 1-1 with the Python model class. Most property
// types have counterparts, e.g. bokeh.core.properties.String will be
// ``p.String`` in the JS implementation. Any time the JS type system is not
// yet as complete, you can use ``p.Any`` as a "wildcard" property type.
this.define<Custom.Props>({
text: [p.String],
slider: [p.Any],
})
}
}
Stack traceback and/or browser JavaScript console output
Compilation failed:
custom.ts:13:33 - error TS2339: Property 'slider' does not exist on type 'HTMLBox'.
13 this.connect(this.model.slider.change, () => {
~~~~~~
custom.ts:32:26 - error TS2339: Property 'text' does not exist on type 'HTMLBox'.
32 }, `${this.model.text}: ${this.model.slider.value}`))
~~~~
custom.ts:32:46 - error TS2339: Property 'slider' does not exist on type 'HTMLBox'.
32 }, `${this.model.text}: ${this.model.slider.value}`))
~~~~~~
custom.ts:55:21 - error TS2702: 'Custom' only refers to a type, but is being used as a namespace here.
55 this.define<Custom.Props>({
~~~~~~
An exception has occurred, use %tb to see the full traceback.
SystemExit: 1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15 (12 by maintainers)
If this is in the notebook you will need to call
output_notebookagain after the custom extension is defined. That is what causes the extension definition to exist on the JS side in the notebook.On Sunday, January 19, 2020, Marc Skov Madsen notifications@github.com wrote: