-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
property_mixins.py
386 lines (288 loc) · 12 KB
/
property_mixins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#-----------------------------------------------------------------------------
# Copyright (c) 2012 - 2024, Anaconda, Inc., and Bokeh Contributors.
# All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
#-----------------------------------------------------------------------------
''' Mix-in classes that bulk add groups of properties to Bokeh models.
Some groups of properties often show up in Bokeh models together. For
instance, any model that exposes a fill color property for use when
rendering will almost always want to expose a fill alpha as well. To
reduce boilerplate code and simplify defining models with these sets
of properties, use the mix-in classes in this module:
* |FillProps| --- properties for fill color and alpha
* |HatchProps| --- properties for hatching pattern, color, alpha, etc.
* |ImageProps| --- properties for global alpha on images
* |LineProps| --- properties for line color, dashing, width, etc.
* |TextProps| --- properties for text color, font, etc.
To include these properties in a Bokeh model, use the |Include| property
as shown here:
.. code-block:: python
class SomeGlyph(Glyph):
fill_props = Include(FillProps, help="""
The {prop} values for the annular wedges.
""")
This adds all the fill properties ``fill_color`` and ``fill_alpha`` to this
model. The help string contains a placeholder `{prop}`. When docs for this class
are rendered by the ``bokeh-model`` directive, the placeholder will be replaced with more information
specific to each property.
.. |Include| replace:: :class:`~bokeh.core.properties.Include`
.. |FillProps| replace:: :class:`~bokeh.core.property_mixins.FillProps`
.. |HatchProps| replace:: :class:`~bokeh.core.property_mixins.HatchProps`
.. |ImageProps| replace:: :class:`~bokeh.core.property_mixins.ImageProps`
.. |LineProps| replace:: :class:`~bokeh.core.property_mixins.LineProps`
.. |TextProps| replace:: :class:`~bokeh.core.property_mixins.TextProps`
.. |miter_join| image:: /_images/miter_join.png
:height: 15
:alt: Two line segments joined with a miter (sharp-angle) join style.
.. |round_join| image:: /_images/round_join.png
:height: 15
:alt: Two line segments joined with a round join style.
.. |bevel_join| image:: /_images/bevel_join.png
:height: 15
:alt: Two line segments joined with a bevel (truncated) join style.
.. |butt_cap| image:: /_images/butt_cap.png
:height: 12
:alt: A line segment with no end cap extending beyond the explicit end coordinate.
.. |round_cap| image:: /_images/round_cap.png
:height: 12
:alt: A line segment with a rounded end cap extending beyond the explicit end coordinate.
.. |square_cap| image:: /_images/square_cap.png
:height: 12
:alt: A line segment with a squared end cap extending beyond the explicit end coordinate.
'''
#-----------------------------------------------------------------------------
# Boilerplate
#-----------------------------------------------------------------------------
from __future__ import annotations
import logging # isort:skip
log = logging.getLogger(__name__)
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Bokeh imports
from .enums import (
FontStyle,
HatchPattern,
HatchPatternAbbreviation,
LineCap,
LineJoin,
TextAlign,
TextBaseline,
)
from .has_props import HasProps
from .properties import (
Alpha,
AlphaSpec,
Color,
ColorSpec,
DashPattern,
DashPatternSpec,
Dict,
Enum,
Float,
FontSize,
FontSizeSpec,
FontStyleSpec,
HatchPatternSpec,
Instance,
Int,
IntSpec,
LineCapSpec,
LineJoinSpec,
Nullable,
NumberSpec,
Size,
String,
StringSpec,
TextAlignSpec,
TextBaselineSpec,
value,
)
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
__all__ = (
'FillProps',
'HatchProps',
'ImageProps',
'LineProps',
'TextProps',
'ScalarFillProps',
'ScalarHatchProps',
'ScalarImageProps',
'ScalarLineProps',
'ScalarTextProps',
)
#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------
_color_help = """
A color to use to %s with.
"""
_alpha_help = """
An alpha value to use to %s with.
"""
_line_width_help = """
Stroke width in units of pixels.
"""
_line_join_help = """
How path segments should be joined together.
Acceptable values are:
- ``'miter'`` |miter_join|
- ``'round'`` |round_join|
- ``'bevel'`` |bevel_join|
"""
_line_cap_help = """
How path segments should be terminated.
Acceptable values are:
- ``'butt'`` |butt_cap|
- ``'round'`` |round_cap|
- ``'square'`` |square_cap|
"""
_text_font_help = """
Name of a font to use for rendering text, e.g., ``'times'``, ``'helvetica'``.
"""
_text_font_style_help = """
A style to use for rendering text.
Acceptable values are:
- ``'normal'`` normal text
- ``'italic'`` *italic text*
- ``'bold'`` **bold text**
- ``"bold italic"`` ***bold italic text***
"""
_text_align_help = """
Horizontal anchor point to use when rendering text.
Acceptable values are:
- ``'left'``
- ``'right'``
- ``'center'``
"""
_text_baseline_help = """
Vertical anchor point to use when rendering text.
Acceptable values are:
- ``'top'``
- ``'middle'``
- ``'bottom'``
- ``'alphabetic'``
- ``'hanging'``
- ``'ideographic'``
"""
_text_line_height_help = """
In multi-line text, how much additional space should be allocated for
each line. The value is provided as a number, but should be treated as
a percentage of font size. The default is 120%. Setting it to 1.0, so
100%, means no additional space will be used.
"""
_hatch_scale_help = """
A rough measure of the 'size' of the hatching pattern. Generally speaking, the
higher the number, the more spread out the pattern will be.
"""
_hatch_pattern_help = f"""
Built-in patterns are can either be specified as long names:
{', '. join(HatchPattern)}
or as one-letter abbreviations:
{', '. join(repr(x) for x in HatchPatternAbbreviation)}
"""
_hatch_weight_help = """
A width value for line-strokes used in hatching.
"""
#----------------------------------------------------------------------------
# General API
#----------------------------------------------------------------------------
class FillProps(HasProps):
''' Properties relevant to rendering fill regions.
Mirrors the BokehJS ``properties.FillVector`` class.
'''
fill_color = ColorSpec(default="gray", help=_color_help % "fill paths")
fill_alpha = AlphaSpec(help=_alpha_help % "fill paths")
class ScalarFillProps(HasProps):
''' Properties relevant to rendering fill regions.
Mirrors the BokehJS ``properties.Fill`` class.
'''
fill_color = Nullable(Color, default="gray", help=_color_help % "fill paths")
fill_alpha = Alpha(help=_alpha_help % "fill paths")
class HatchProps(HasProps):
''' Properties relevant to rendering fill regions.
Mirrors the BokehJS ``properties.HatchVector`` class.
'''
hatch_color = ColorSpec(default="black", help=_color_help % "hatching")
hatch_alpha = AlphaSpec(help=_alpha_help % "hatching")
hatch_scale = NumberSpec(default=12.0, accept_datetime=False, accept_timedelta=False, help=_hatch_scale_help)
hatch_pattern = HatchPatternSpec(default=None, help=_hatch_pattern_help)
hatch_weight = NumberSpec(default=1.0, accept_datetime=False, accept_timedelta=False, help=_hatch_weight_help)
hatch_extra = Dict(String, Instance("bokeh.models.textures.Texture"))
class ScalarHatchProps(HasProps):
''' Properties relevant to rendering fill regions.
Mirrors the BokehJS ``properties.Hatch`` class.
'''
hatch_color = Nullable(Color, default="black", help=_color_help % "hatching")
hatch_alpha = Alpha(help=_alpha_help % "hatching")
hatch_scale = Size(default=12.0, help=_hatch_scale_help)
hatch_pattern = Nullable(String, help=_hatch_pattern_help) # String to accommodate user custom values
hatch_weight = Size(default=1.0, help=_hatch_weight_help)
hatch_extra = Dict(String, Instance("bokeh.models.textures.Texture"))
class ImageProps(HasProps):
''' Properties relevant to rendering images.
Mirrors the BokehJS ``properties.ImageVector`` class.
'''
global_alpha = AlphaSpec(help=_alpha_help % "images")
class ScalarImageProps(HasProps):
''' Properties relevant to rendering images.
Mirrors the BokehJS ``properties.Image`` class.
'''
global_alpha = Alpha(help=_alpha_help % "images")
class LineProps(HasProps):
''' Properties relevant to rendering path operations.
Mirrors the BokehJS ``properties.LineVector`` class.
'''
line_color = ColorSpec(default="black", help=_color_help % "stroke paths")
line_alpha = AlphaSpec(help=_alpha_help % "stroke paths")
line_width = NumberSpec(default=1, accept_datetime=False, accept_timedelta=False, help=_line_width_help)
line_join = LineJoinSpec(default="bevel", help=_line_join_help)
line_cap = LineCapSpec(default="butt", help=_line_cap_help)
line_dash = DashPatternSpec(default=[], help="""How should the line be dashed.""")
line_dash_offset = IntSpec(default=0, help="""The distance into the ``line_dash`` (in pixels) that the pattern should start from.""")
class ScalarLineProps(HasProps):
''' Properties relevant to rendering path operations.
Mirrors the BokehJS ``properties.Line`` class.
'''
line_color = Nullable(Color, default="black", help=_color_help % "stroke paths")
line_alpha = Alpha(help=_alpha_help % "stroke paths")
line_width = Float(default=1, help=_line_width_help)
line_join = Enum(LineJoin, default="bevel", help=_line_join_help)
line_cap = Enum(LineCap, default="butt", help=_line_cap_help)
line_dash = DashPattern(default=[], help="""How should the line be dashed.""")
line_dash_offset = Int(default=0, help="""The distance into the ``line_dash`` (in pixels) that the pattern should start from.""")
class TextProps(HasProps):
''' Properties relevant to rendering text.
Mirrors the BokehJS ``properties.TextVector`` class.
'''
text_color = ColorSpec(default="#444444", help=_color_help % "fill text")
text_outline_color = ColorSpec(default=None, help=_color_help % "outline text")
text_alpha = AlphaSpec(help=_alpha_help % "fill text")
text_font = StringSpec(default=value("helvetica"), help=_text_font_help)
text_font_size = FontSizeSpec(default=value("16px"))
text_font_style = FontStyleSpec(default="normal", help=_text_font_style_help)
text_align = TextAlignSpec(default="left", help=_text_align_help)
text_baseline = TextBaselineSpec(default="bottom", help=_text_baseline_help)
text_line_height = NumberSpec(default=1.2, help=_text_line_height_help)
class ScalarTextProps(HasProps):
''' Properties relevant to rendering text.
Mirrors the BokehJS ``properties.Text`` class.
'''
text_color = Nullable(Color, default="#444444", help=_color_help % "fill text")
text_outline_color = Nullable(Color, default=None, help=_color_help % "outline text")
text_alpha = Alpha(help=_alpha_help % "fill text")
text_font = String(default="helvetica", help=_text_font_help)
text_font_size = FontSize("16px") # XXX not great XXX why?
text_font_style = Enum(FontStyle, default="normal", help=_text_font_style_help)
text_align = Enum(TextAlign, default="left", help=_text_align_help)
text_baseline = Enum(TextBaseline, default="bottom", help=_text_baseline_help)
text_line_height = Float(default=1.2, help=_text_line_height_help)
#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------