Provide Python descriptors for delegating to Bokeh properties.
The Python descriptor protocol allows fine-grained control over all attribute access on instances (“You control the dot”). Bokeh uses the descriptor protocol to provide easy-to-use, declarative, type-based class properties that can automatically validate and serialize their values, as well as help provide sophisticated documentation.
A Bokeh property really consist of two parts: a familiar “property” portion, such as Int, String, etc., as well as an associated Python descriptor that delegates attribute access to the property instance.
Int
String
For example, a very simplified definition of a range-like object might be:
from bokeh.model import Model from bokeh.core.properties import Float class Range(Model): start = Float(help="start point") end = Float(help="end point")
When this class is created, the MetaHasProps metaclass wires up both the start and end attributes to a Float property. Then, when a user accesses those attributes, the descriptor delegates all get and set operations to the Float property.
MetaHasProps
start
end
Float
rng = Range() # The descriptor __set__ method delegates to Float, which can validate # the value 10.3 as a valid floating point value rng.start = 10.3 # But can raise a validation exception if an attempt to set to a list # is made rng.end = [1,2,3] # ValueError !
More sophisticated properties such as DataSpec and its subclasses can exert control over how values are serialized. Consider this example with the Circle glyph and its x attribute that is a NumberSpec:
DataSpec
Circle
x
NumberSpec
from bokeh.models import Circle c = Circle() c.x = 10 # serializes to {'value': 10} c.x = 'foo' # serializes to {'field': 'foo'}
There are many other examples like this throughout Bokeh. In this way users may operate simply and naturally, and not be concerned with the low-level details around validation, serialization, and documentation.
This module provides the class PropertyDescriptor and various subclasses that can be used to attach Bokeh properties to Bokeh models.
PropertyDescriptor
Note
These classes form part of the very low-level machinery that implements the Bokeh model and property system. It is unlikely that any of these classes or their methods will be applicable to any standard usage or to anyone who is not directly developing on Bokeh’s own infrastructure.
Base class for a python descriptor that delegates access for a named attribute to a Bokeh Property instance.
Property
__init__
Create a descriptor for a hooking up a named Bokeh property as an attribute on a HasProps class.
HasProps
name (str) – the attribute name that this descriptor is for
__str__
Basic string representation of PropertyDescriptor.
Subclasses must implement this to serve their specific needs.
__get__
Implement the getter for the Python descriptor protocol.
obj (HasProps or None) – The instance to set a new property value on (for instance attribute access), or None (for class attribute access)
owner (obj) – The new value to set the property to
None
NotImplementedError –
__set__
Implement the setter for the Python descriptor protocol.
An optional argument setter has been added to the standard setter arguments. When needed, this value should be provided by explicitly invoking __set__. See below for more information.
setter
obj (HasProps) – The instance to set a new property value on
value (obj) – The new value to set the property to
setter (ClientSession or ServerSession or None, optional) –
This is used to prevent “boomerang” updates to Bokeh apps. (default: None)
In the context of a Bokeh server application, incoming updates to properties will be annotated with the session that is doing the updating. This value is propagated through any subsequent change notifications that the update triggers. The session can compare the event setter to itself, and suppress any updates that originate from itself.
__delete__
Implement the deleter for the Python descriptor protocol.
obj (HasProps) – An instance to delete this property from
add_prop_descriptor_to_class
MetaHasProps calls this during class creation as it iterates over properties to add, to update its registry of new properties.
The parameters passed in are mutable and this function is expected to update them accordingly.
class_name (str) – name of the class this descriptor is added to
new_class_attrs (dict[str, PropertyDescriptor]) – mapping of attribute names to PropertyDescriptor that this function will update
names_with_refs (set[str]) – set of all property names for properties that also have references, that this function will update
container_names (set[str]) – set of all property names for properties that are container props, that this function will update
dataspecs (dict[str, PropertyDescriptor]) – mapping of attribute names to PropertyDescriptor for DataSpec properties that this function will update
class_default
The default as computed for a certain class, ignoring any per-instance theming.
serializable_value
Produce the value as it should be serialized.
Sometimes it is desirable for the serialized value to differ from the __get__ in order for the __get__ value to appear simpler for user or developer convenience.
obj (HasProps) – the object to get the serialized attribute for
JSON-like
set_from_json
Sets the value of this property from a JSON value.
obj – (HasProps) : instance to set the property value on
json – (JSON-value) : value to set to the attribute to
models (dict or None, optional) –
Mapping of model ids to models (default: None)
This is needed in cases where the attributes to update also have values that have references.
trigger_if_changed
Send a change event notification if the property is set to a value is not equal to old.
old
obj (HasProps) – The object the property is being set on.
old (obj) – The previous value of the property to compare
has_ref
Whether the property can refer to another HasProps instance.
This is typically True for container properties, Instance, etc.
Instance
readonly
Whether this property is read-only.
Read-only properties may only be modified by the client (i.e., by BokehJS, in the browser). Read only properties are useful for quantities that originate or that can only be computed in the browser, for instance the “inner” plot dimension of a plot area, which depend on the current layout state. It is useful for Python callbacks to be able to know these values, but they can only be computed in the actual browser.
serialized
Whether the property should be serialized when serializing an object.
This would be False for a “virtual” or “convenience” property that duplicates information already available in other properties, for example.
BasicPropertyDescriptor
A PropertyDescriptor for basic Bokeh properties (e.g, Int, String, Float, etc.) with simple get/set and serialization behavior.
Create a PropertyDescriptor for basic Bokeh properties.
name (str) – The attribute name that this property is for
property (Property) – A basic property to create a descriptor for
Basic string representation of BasicPropertyDescriptor.
Delegates to self.property.__str__
self.property.__str__
For instance attribute access, we delegate to the Property. For class attribute access, we return ourself.
Examples
>>> from bokeh.models import Range1d >>> r = Range1d(start=10, end=20) # instance attribute access, returns the property value >>> r.start 10 # class attribute access, returns the property descriptor >>> Range1d.start <bokeh.core.property.descriptors.BasicPropertyDescriptor at 0x1148b3390>
Get the default value for a specific subtype of HasProps, which may not be used for an individual instance.
cls (class) – The class to get the default value for.
object
instance_default
Get the default value that will be used for a specific instance.
obj (HasProps) – The instance to get the default value for.
This method first
obj (HasProps) –
json (JSON-dict) –
models (seq[Model], optional) –
For basic properties, delegate to the has_ref attribute on the Property.
Read-only properties may only be modified by the client (i.e., by BokehJS in the browser).
ColumnDataPropertyDescriptor
A PropertyDescriptor specialized to handling ColumnData properties.
ColumnData
This method first separately extracts and removes any units field in the JSON, and sets the associated units property directly. The remaining value is then passed to the superclass __set__ to be handled.
units
DataSpecPropertyDescriptor
A PropertyDescriptor for Bokeh DataSpec properties that serialize to field/value dictionaries.
UnitsSpecPropertyDescriptor
A PropertyDecscriptor for Bokeh UnitsSpec properties that contribute associated _units properties automatically as a side effect.
PropertyDecscriptor
UnitsSpec
_units
units_property (Property) – An associated property to hold units information
This method first separately extracts and removes any units field in the JSON, and sets the associated units property directly. The remaining JSON is then passed to the superclass set_from_json to be handled.