In order to streamline and automate the creation and use of models that can for describing plots and scenes, Bokeh provides a collection of properties and property mixins. Property classes provide automatic validation and serialization for a large collection of useful types. Mixin classes provide for easy bulk addition of properties to model classes.
Below is the full class inheritance diagram for all standard Bokeh property types. Click on any node to be taken to the corresponding documention.
Properties are objects that can be assigned as class level attributes on Bokeh models, to provide automatic serialization and validation.
For example, the following defines a model that has integer, string, and list[float] properties:
class Model(HasProps):
foo = Int
bar = String
baz = List(Float)
The properties of this class can be initialized by specifying keyword arguments to the initializer:
m = Model(foo=10, bar="a str", baz=[1,2,3,4])
But also by setting the attributes on an instance:
m.foo = 20
Attempts to set a property to a value of the wrong type will result in a ValueError exception:
>>> m.foo = 2.3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/bryan/work/bokeh/bokeh/properties.py", line 585, in __setattr__
super(HasProps, self).__setattr__(name, value)
File "/Users/bryan/work/bokeh/bokeh/properties.py", line 159, in __set__
raise e
File "/Users/bryan/work/bokeh/bokeh/properties.py", line 152, in __set__
self.validate(value)
File "/Users/bryan/work/bokeh/bokeh/properties.py", line 707, in validate
(nice_join([ cls.__name__ for cls in self._underlying_type ]), value, type(value).__name__))
ValueError: expected a value of type int8, int16, int32, int64 or int, got 2.3 of type float
Additionally, properties know how to serialize themselves, to be understood by BokehJS.
Angle type property.
Any type property accepts any values.
NumPy array type property.
Boolean type property.
Byte type property.
Accepts color definition in a variety of ways, and produces an appropriate serialization of its value for whatever backend.
For colors, because we support named colors and hex values prefaced with a “#”, when we are handed a string value, there is a little interpretation: if the value is one of the 147 SVG named colors or it starts with a “#”, then it is interpreted as a value.
If a 3-tuple is provided, then it is treated as an RGB (0..255). If a 4-tuple is provided, then it is treated as an RGBa (0..255), with alpha as a float between 0 and 1. (This follows the HTML5 Canvas API.)
Subclass of DataSpec for specifying colors.
Although this serves the same role as a DataSpec, its usage is somewhat different because:
For colors, because we support named colors and hex values prefaced with a “#”, when we are handed a string value, there is a little interpretation: if the value is one of the 147 SVG named colors or it starts with a “#”, then it is interpreted as a value. Otherwise, it is treated as a field name.
If a 3-tuple is provided, then it is treated as an RGB (0..255). If a 4-tuple is provided, then it is treated as an RGBa (0..255), with alpha as a float between 0 and 1. (This follows the HTML5 Canvas API.)
Unlike DataSpec, ColorSpecs do not have a “units” property.
When reading out a ColorSpec, it returns a tuple, hex value, or field name
There are two common use cases for ColorSpec: setting a constant value, and indicating a field name to look for on the datasource:
>>> class Bar(HasProps):
... col = ColorSpec(default="green")
... col2 = ColorSpec("colorfield")
>>> b = Bar()
>>> b.col = "red" # sets a fixed value of red
>>> b.col
"red"
>>> b.col = "myfield" # Use the datasource field named "myfield"
>>> b.col
"myfield"
For more examples, see tests/test_glyphs.py
Returns True if the argument is a literal color. Check for a well-formed hexadecimal color value.
Complex floating point type property.
Base class for Container-like type properties.
Dash type property.
Express patterns that describe line dashes. DashPattern values can be specified in a variety of ways:
To indicate that dashing is turned off (solid lines), specify the empty list [].
Because the BokehJS glyphs support a fixed value or a named field for most data fields, we capture that in this descriptor. Fields can have a fixed value, or be a name that is looked up on the datasource (usually as a column or record array field). Numerical data can also have units of screen or data space.
We mirror the JS convention in this Python descriptor. For details, see renderers/properties.coffee in BokehJS, and specifically the select() function.
There are multiple ways to set a DataSpec, illustrated below with comments and example code.
Setting DataSpecs
Simple example:
class Foo(HasProps):
x = DataSpec("x", units="data")
f = Foo()
f.x = "fieldname" # Use the datasource field named "fieldname"
f.x = 12 # A fixed value of 12
Can provide a dict with the fields explicitly named:
f.width = {"name": "foo"}
f.size = {"name": "foo", "units": "screen"}
Reading DataSpecs
In the cases when the dataspec is set to just a field name or a fixed value, then those are returned. If the no values have been set, then the value of to_dict() is returned.
In all cases, to determine the full dict that will be used to represent this dataspec, use the to_dict() method.
Implementation
The DataSpec instance is stored in the class dict, and acts as a descriptor. Thus, it is shared between all instances of the class. Instance-specific data is stored in the instance dict, in a private variable named _[attrname]. This stores the actual value that the user last set (and does not exist if the user has not yet set the value).
Date (not datetime) type property.
Datetime type property.
Python dict type property.
If a default value is passed in, then a shallow copy of it will be used for each new use of this property.
Takes a list of valid properties and validates against them in succession.
An Enum with a list of allowed values. The first value in the list is the default value, unless a default is provided with the “default” keyword argument.
Event type property.
Floating point type property.
Function type property.
Include other properties from mixin Models, with a given prefix.
Instance type property, for references to other Models in the object graph.
Signed integer type property.
Range type property ensures values are contained inside a given interval.
Python list type property.
Base class for Properties that have type parameters, e.g. List(String).
Percentage type property.
Percents are useful for specifying alphas and coverage and extents; more semantically meaningful than Float(0..1).
A base class for simple property types. Subclasses should define a class attribute _underlying_type that is a tuple of acceptable type values for the property.
Base class for all type properties.
Called by the metaclass to create a new instance of this descriptor if the user just assigned it to a property without trailing parentheses.
Regex type property validates that text values match the given regular expression.
RelativeDelta type property for time deltas.
Sequence (list, tuple) type property.
Size type property.
Note
Size is equivalent to an unsigned int.
String type property.
A reference to an instance of the class being defined.
Tuple type property.
Properties to use when performing fill operations while rendering.
Mirrors the BokehJS properties.Fill class.
property type: DataSpec
An alpha value to use to fill paths with.
Acceptable values are floating point numbers between 0 (transparent) and 1 (opaque).
property type: ColorSpec
A color to use to fill paths with.
Acceptable values are:
Properties to use when performing stroke operations while rendering.
Mirrors the BokehJS properties.Line class.
property type: DataSpec
An alpha value to use to stroke paths with.
Acceptable values are floating point numbers between 0 (transparent) and 1 (opaque).
property type: Enum(‘butt’, ‘round’, ‘square’)
How path segments should be terminated.
Acceptable values are:
property type: ColorSpec
A color to use to stroke paths with.
Acceptable values are:
property type: DashPattern
How should the line be dashed.
property type: Int
The distance into the line_dash (in pixels) that the pattern should start from.
Properties to use when performing text drawing operations while rendering.
Mirrors the BokehJS properties.Text class.
Note
There is currently only support for filling text. An interface to stroke the outlines of text has not yet been exposed.
property type: Enum(‘left’, ‘right’, ‘center’)
Horizontal anchor point to use when rendering text.
Acceptable values are:
property type: DataSpec
An alpha value to use to fill text with.
Acceptable values are floating point numbers between 0 (transparent) and 1 (opaque).
property type: Enum(‘top’, ‘middle’, ‘bottom’, ‘alphabetic’, ‘hanging’)
Vertical anchor point to use when rendering text.
Acceptable values are:
property type: ColorSpec
A color to use to fill text with.
Acceptable values are: