bokeh.model

Provide a base class for all objects (called Bokeh Models) that can go in a Bokeh Document.

class Model(**kwargs)[source]

Bases: bokeh.core.has_props.HasProps, bokeh.util.callback_manager.PropertyCallbackManager, bokeh.util.callback_manager.EventCallbackManager

Base class for all objects stored in Bokeh Document instances.

js_event_callbacks

property type: Dict ( String , List ( Instance ( CustomJS ) ) )

A mapping of event names to lists of CustomJS callbacks.

Typically, rather then modifying this property directly, callbacks should be added using the Model.js_on_event method:

callback = CustomJS(code="console.log('tap event occurred')")
plot.js_on_event('tap', callback)
js_property_callbacks

property type: Dict ( String , List ( Instance ( CustomJS ) ) )

A mapping of attribute names to lists of CustomJS callbacks, to be set up on BokehJS side when the document is created.

Typically, rather then modifying this property directly, callbacks should be added using the Model.js_on_change method:

callback = CustomJS(code="console.log('stuff')")
plot.x_range.js_on_change('start', callback)
name

property type: String

An arbitrary, user-supplied name for this model.

This name can be useful when querying the document to retrieve specific Bokeh models.

>>> plot.circle([1,2,3], [4,5,6], name="temp")
>>> plot.select(name="temp")
[GlyphRenderer(id='399d53f5-73e9-44d9-9527-544b761c7705', ...)]

Note

No uniqueness guarantees or other conditions are enforced on any names that are provided, nor is the name used directly by Bokeh for any reason.

subscribed_events

property type: List ( String )

List of events that are subscribed to by Python callbacks. This is the set of events that will be communicated from BokehJS back to Python for this model.

tags

property type: List ( Any )

An optional list of arbitrary, user-supplied values to attach to this model.

This data can be useful when querying the document to retrieve specific Bokeh models:

>>> r = plot.circle([1,2,3], [4,5,6])
>>> r.tags = ["foo", 10]
>>> plot.select(tags=['foo', 10])
[GlyphRenderer(id='1de4c3df-a83d-480a-899b-fb263d3d5dd9', ...)]

Or simply a convenient way to attach any necessary metadata to a model that can be accessed by CustomJS callbacks, etc.

Note

No uniqueness guarantees or other conditions are enforced on any tags that are provided, nor are the tags used directly by Bokeh for any reason.

js_on_change(event, *callbacks)[source]

Attach a CustomJS callback to an arbitrary BokehJS model event.

On the BokehJS side, change events for model properties have the form "change:property_name". As a convenience, if the event name passed to this method is also the name of a property on the model, then it will be prefixed with "change:" automatically:

# these two are equivalent
source.js_on_change('data', callback)
source.js_on_change('change:data', callback)

However, there are other kinds of events that can be useful to respond to, in addition to property change events. For example to run a callback whenever data is streamed to a ColumnDataSource, use the "stream" event on the source:

source.js_on_change('streaming', callback)
js_on_event(event, *callbacks)[source]
layout(side, plot)[source]
on_change(attr, *callbacks)[source]

Add a callback on this object to trigger when attr changes.

Parameters:
  • attr (str) – an attribute name on this object
  • *callbacks (callable) – callback functions to register
Returns:

None

Example:

widget.on_change('value', callback1, callback2, ..., callback_n)
references()[source]

Returns all Models that this object has references to.

select(selector)[source]

Query this object and all of its references for objects that match the given selector.

Parameters:selector (JSON-like) –
Returns:seq[Model]
select_one(selector)[source]

Query this object and all of its references for objects that match the given selector. Raises an error if more than one object is found. Returns single matching object, or None if nothing is found :param selector: :type selector: JSON-like

Returns:Model
set_select(selector, updates)[source]

Update objects that match a given selector with the specified attribute/value updates.

Parameters:
  • selector (JSON-like) –
  • updates (dict) –
Returns:

None

to_json(include_defaults)[source]

Returns a dictionary of the attributes of this object, containing only “JSON types” (string, number, boolean, none, dict, list).

References to other objects are serialized as “refs” (just the object ID and type info), so the deserializer will need to separately have the full attributes of those other objects.

There’s no corresponding from_json() because to deserialize an object is normally done in the context of a Document (since the Document can resolve references).

For most purposes it’s best to serialize and deserialize entire documents.

Parameters:include_defaults (bool) – whether to include attributes that haven’t been changed from the default
to_json_string(include_defaults)[source]

Returns a JSON string encoding the attributes of this object.

References to other objects are serialized as references (just the object ID and type info), so the deserializer will need to separately have the full attributes of those other objects.

There’s no corresponding from_json_string() because to deserialize an object is normally done in the context of a Document (since the Document can resolve references).

For most purposes it’s best to serialize and deserialize entire documents.

Parameters:include_defaults (bool) – whether to include attributes that haven’t been changed from the default
trigger(attr, old, new, hint=None, setter=None)[source]
document

The Document this model is attached to (can be None)

id
ref

A Bokeh protocol “reference” to this model, i.e. a dict of the form:

{
    'type' : << view model name >>
    'id'   : << unique model id >>
}

Additionally there may be a subtype field if this model is a subtype.

JSON Prototype
{
  "id": "27121",
  "js_event_callbacks": {},
  "js_property_callbacks": {},
  "name": null,
  "subscribed_events": [],
  "tags": []
}
collect_models(*input_values)[source]

Collect a duplicate-free list of all other Bokeh models referred to by this model, or by any of its references, etc.

Iterate over input_values and descend through their structure collecting all nested Models on the go. The resulting list is duplicate-free based on objects’ identifiers.

Parameters:*input_values (Model) – Bokeh models to collect other models from
Returns:all models reachable from this one.
Return type:list[Model]
get_class(view_model_name)[source]

Look up a Bokeh model class, given its view model name.

Parameters:view_model_name (str) – A view model name for a Bokeh model to look up
Returns:the model class corresponding to view_model_name
Return type:Model
Raises:KeyError, if the model cannot be found

Example

>>> from bokeh.model import get_class
>>> get_class("Range1d")
<class 'bokeh.models.ranges.Range1d'>