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.
bokeh.properties
¶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
(default=None, help=None)¶Angle type property.
Any
(default=None, help=None)¶Any type property accepts any values.
Array
(item_type, default=None, help=None)¶NumPy array type property.
Bool
(default=None, help=None)¶Boolean type property.
Byte
(default=0, help=None)¶Byte type property.
Color
(default=None, help=None)¶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.)
Complex
(default=None, help=None)¶Complex floating point type property.
ContainerProperty
(default=None, help=None)¶Base class for Container-like type properties.
DashPattern
(default=[], help=None)¶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 [].
Date
(default=datetime.date(2015, 9, 25), help=None)¶Date (not datetime) type property.
Datetime
(default=datetime.date(2015, 9, 25), help=None)¶Datetime type property.
Dict
(keys_type, values_type, default={}, help=None)¶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.
Either
(tp1, tp2, *type_params, **kwargs)¶Takes a list of valid properties and validates against them in succession.
Enum
(enum, *values, **kwargs)¶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
(default=None, help=None)¶Event type property.
Float
(default=None, help=None)¶Floating point type property.
Function
(default=None, help=None)¶Function type property.
Include
(delegate, help='', use_prefix=True)¶Include other properties from mixin Models, with a given prefix.
Instance
(instance_type, default=None, help=None)¶Instance type property, for references to other Models in the object graph.
Int
(default=None, help=None)¶Signed integer type property.
Interval
(interval_type, start, end, default=None, help=None)¶Range type property ensures values are contained inside a given interval.
JSON
(default=None, help=None)¶JSON type property validates that text values are valid JSON.
Note
The string is transmitted and received by BokehJS as a string
containing JSON content. i.e., you must use JSON.parse
to unpack
the value into a JavaScript hash.
List
(item_type, default=[], help=None)¶Python list type property.
ParameterizedProperty
(default=None, help=None)¶Base class for Properties that have type parameters, e.g.
List(String)
.
Percent
(default=None, help=None)¶Percentage type property.
Percents are useful for specifying alphas and coverage and extents; more semantically meaningful than Float(0..1).
PrimitiveProperty
(default=None, help=None)¶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.
Property
(default=None, help=None)¶Base class for all type properties.
autocreate
(name=None)¶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
(regex, default=None, help=None)¶Regex type property validates that text values match the given regular expression.
RelativeDelta
(default={}, help=None)¶RelativeDelta type property for time deltas.
Seq
(item_type, default=None, help=None)¶Sequence (list, tuple) type property.
Size
(default=None, help=None)¶Size type property.
Note
Size
is equivalent to an unsigned int.
String
(default=None, help=None)¶String type property.
This
(default=None, help=None)¶A reference to an instance of the class being defined.
Tuple
(tp1, tp2, *type_params, **kwargs)¶Tuple type property.
abstract
(cls)¶A phony decorator to mark abstract base classes.
field
(name)¶Convenience function do explicitly mark a field specification for a Bokeh model property.
Parameters: | name (str) – name of a data source field to reference for a property. |
---|---|
Returns: | {“field”: name} |
Return type: | dict |
Note
This function is included for completeness. String values for property specifications are by default interpreted as field names.
value
(val)¶Convenience function do explicitly mark a value specification for a Bokeh model property.
Parameters: | val (any) – a fixed value to specify for a property. |
---|---|
Returns: | {“value”: name} |
Return type: | dict |
Note
String values for property specifications are by default interpreted as field names. This function is especially useful when you want to specify a fixed value with text properties.
Example:
# The following will take text values to render from a data source
# column "text_column", but use a fixed value "12pt" for font size
p.text("x", "y", text="text_column",
text_font_size=value("12pt"), source=source)
bokeh.mixins
¶FillProps
(**properties)¶Properties to use when performing fill operations while rendering.
Mirrors the BokehJS properties.Fill
class.
fill_alpha
¶property type: NumberSpec
(1.0)
An alpha value to use to fill paths with.
Acceptable values are floating point numbers between 0 (transparent) and 1 (opaque).
fill_color
¶property type: ColorSpec
(‘gray’)
A color to use to fill paths with.
Acceptable values are:
'green'
, 'indigo'
'#FF0000'
, '#44444444'
LineProps
(**properties)¶Properties to use when performing stroke operations while rendering.
Mirrors the BokehJS properties.Line
class.
line_alpha
¶property type: NumberSpec
(1.0)
An alpha value to use to stroke paths with.
Acceptable values are floating point numbers between 0 (transparent) and 1 (opaque).
line_cap
¶property type: Enum
(‘butt’, ‘round’, ‘square’)
How path segments should be terminated.
Acceptable values are:
line_color
¶property type: ColorSpec
(‘black’)
A color to use to stroke paths with.
Acceptable values are:
'green'
, 'indigo'
'#FF0000'
, '#44444444'
line_dash
¶property type: DashPattern
How should the line be dashed.
line_dash_offset
¶property type: Int
The distance into the line_dash
(in pixels) that the pattern should
start from.
line_join
¶property type: Enum
(‘miter’, ‘round’, ‘bevel’)
How path segments should be joined together.
Acceptable values are:
line_width
¶property type: NumberSpec
(1)
Stroke width in units of pixels.
TextProps
(**properties)¶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.
text_align
¶property type: Enum
(‘left’, ‘right’, ‘center’)
Horizontal anchor point to use when rendering text.
Acceptable values are:
'left'
'right'
'center'
text_alpha
¶property type: NumberSpec
(1.0)
An alpha value to use to fill text with.
Acceptable values are floating point numbers between 0 (transparent) and 1 (opaque).
text_baseline
¶property type: Enum
(‘top’, ‘middle’, ‘bottom’, ‘alphabetic’, ‘hanging’)
Vertical anchor point to use when rendering text.
Acceptable values are:
'top'
'middle'
'bottom'
'alphabetic'
'hanging'
text_color
¶property type: ColorSpec
(‘#444444’)
A color to use to fill text with.
Acceptable values are:
'green'
, 'indigo'
'#FF0000'
, '#44444444'