bokeh.model

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

The Document class is the basic unit of serialization for Bokeh visualizations and applications. Documents contain collections of related Bokeh Models (e.g. Plot, Range1d, etc. ) that can be all serialized together.

The Model class is a base class for all objects that can be added to a Document.

class Model(**kwargs)[source]

Bases: bokeh.core.has_props.HasProps, bokeh.util.callback_manager.CallbackManager

Base class for all objects stored in Bokeh Document instances.

js_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.

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.

classmethod collect_models(*input_values)[source]

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.

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('stream', callback)
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
  • callback (callable) – a callback function to register
Returns:

None

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
ref
JSON Prototype
{
  "id": "ca214b1b-b6e5-4f1e-90d7-2588bcb83099",
  "js_callbacks": {},
  "name": null,
  "tags": []
}
class Viewable(class_name, bases, nmspc)[source]

Any Bokeh Model which has its own View Model in the persistence layer.

classmethod get_class(view_model_name)[source]

Given a __view_model__ name, returns the corresponding class object