Anaconda Enterprise: Documentation

# bokeh.model

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

class `bokeh.model.``MetaModel`(class_name, bases, nmspc)

  Specialize the construction of |Model| classes.

  This class is a metaclass for |Model| that is responsible for automatically cataloging all Bokeh models that get defined, so that the serialization machinery between Bokeh and BokehJS can function properly.

  Note

  It is worth pointing out explicitly that this relies on the rules for Metaclass inheritance in Python.

  Bokeh works by replicating Python model objects (e.g. plots, ranges, data sources, which are all |HasProps| subclasses) into BokehJS. In the case of using a Bokeh server, the Bokeh model objects can also be synchronized bidirectionally. This is accomplished by serializing the models to and from a JSON format, that includes the name of the model type as part of the payload, as well as a unique ID, and all the attributes:

      {
          type: "Plot",
          id: 100032,
          attributes: { ... }
      }

  Typically the type name is inferred automatically from the Python class name, and is set as the `__view_model__` class attribute on the Model class that is create. But it is also possible to override this value explicitly:

      class Foo(Model): pass

      class Bar(Model):
          __view_model__ == "Quux"

  This metaclass will raise an error if two Bokeh models are created that attempt to have the same view model name. The only exception made is if one of the models has a custom `__implementation__` in its class definition.

  This metaclass also handles subtype relationships between Bokeh models. Occasionally it may be necessary for multiple class types on the Python side to resolve to the same type on the BokehJS side. This is called subtyping, and is expressed through a `__subtype__` class attribute on a model:

      class Foo(Model): pass

      class Bar(Foo):
          __view_model__ = "Foo"
          __subtype__ = "Bar"

  In this case, python instances of `Foo` and `Bar` will both resolve to `Foo` models in BokehJS. In the context of a Bokeh server application, the original python types will be faithfully round-tripped. (Without the `__subtype__` specified, the above code would raise an error due to duplicate view model names.)

class `bokeh.model.``Model`(**kwargs)

  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 occured')")
        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)

    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)
  `layout`(side, plot)
  `on_change`(attr, *callbacks)

    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`()

    Returns all `Models` that this object has references to.

  `select`(selector)

    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)

    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)

    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)

    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)

    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)
  `document`

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

  `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": "bbd65581-8870-43f0-b89e-c8137126894c",
      "js_event_callbacks": {},
      "js_property_callbacks": {},
      "name": null,
      "subscribed_events": [],
      "tags": []
    }

`bokeh.model.``collect_models`(*input_values)

  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]

`bokeh.model.``get_class`(view_model_name)

  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'>