| 1 |
# Copyright (c) NumFOCUS. |
| 2 |
# Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
# Dependency to bqplot is temporary, we should remove this |
| 5 |
# dependency once scales are extracted from bqplot |
| 6 |
from bqplot import ColorScale, Scale |
| 7 |
from ipywidgets import Color, Widget, widget_serialization |
| 8 |
from py2vega import Variable, py2vega |
| 9 |
from traitlets import ( |
| 10 |
Any, |
| 11 |
Bool, |
| 12 |
Enum, |
| 13 |
Float, |
| 14 |
Instance, |
| 15 |
Unicode, |
| 16 |
Union, |
| 17 |
default, |
| 18 |
validate, |
| 19 |
) |
| 20 |
|
| 21 |
from ._frontend import module_name, module_version |
| 22 |
|
| 23 |
|
| 24 |
class VegaExpr(Widget): |
| 25 |
_model_name = Unicode("VegaExprModel").tag(sync=True) |
| 26 |
_model_module = Unicode(module_name).tag(sync=True) |
| 27 |
_model_module_version = Unicode(module_version).tag(sync=True) |
| 28 |
_view_name = Unicode("VegaExprView").tag(sync=True) |
| 29 |
_view_module = Unicode(module_name).tag(sync=True) |
| 30 |
_view_module_version = Unicode(module_version).tag(sync=True) |
| 31 |
|
| 32 |
value = Unicode("default_value").tag(sync=True) |
| 33 |
|
| 34 |
def __init__(self, value="", **kwargs): |
| 35 |
super().__init__(value=value, **kwargs) |
| 36 |
|
| 37 |
|
| 38 |
class Expr(VegaExpr): |
| 39 |
value = Any().tag(sync=True) |
| 40 |
|
| 41 |
@validate("value") |
| 42 |
def _validate_value(self, proposal): |
| 43 |
return py2vega( |
| 44 |
proposal["value"], |
| 45 |
[ |
| 46 |
Variable("cell", ["value", "row", "column", "metadata"]), |
| 47 |
"default_value", |
| 48 |
"index", |
| 49 |
], |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
class CellRenderer(Widget): |
| 54 |
_model_name = Unicode("CellRendererModel").tag(sync=True) |
| 55 |
_model_module = Unicode(module_name).tag(sync=True) |
| 56 |
_model_module_version = Unicode(module_version).tag(sync=True) |
| 57 |
_view_name = Unicode("CellRendererView").tag(sync=True) |
| 58 |
_view_module = Unicode(module_name).tag(sync=True) |
| 59 |
_view_module_version = Unicode(module_version).tag(sync=True) |
| 60 |
|
| 61 |
|
| 62 |
class TextRenderer(CellRenderer): |
| 63 |
_model_name = Unicode("TextRendererModel").tag(sync=True) |
| 64 |
_view_name = Unicode("TextRendererView").tag(sync=True) |
| 65 |
|
| 66 |
text_value = Union( |
| 67 |
(Unicode(), Instance(VegaExpr), Instance(Scale)), |
| 68 |
allow_none=True, |
| 69 |
default_value=None, |
| 70 |
).tag(sync=True, **widget_serialization) |
| 71 |
text_wrap = Bool(default_value=False).tag(sync=True, **widget_serialization) |
| 72 |
text_elide_direction = Enum( |
| 73 |
values=["right", "left"], default_value="right" |
| 74 |
).tag(sync=True, **widget_serialization) |
| 75 |
font = Union( |
| 76 |
(Unicode(), Instance(VegaExpr), Instance(Scale)), |
| 77 |
default_value="12px sans-serif", |
| 78 |
).tag(sync=True, **widget_serialization) |
| 79 |
text_color = Union( |
| 80 |
(Color(), Instance(VegaExpr), Instance(ColorScale)), |
| 81 |
).tag(sync=True, **widget_serialization) |
| 82 |
background_color = Union( |
| 83 |
(Color(), Instance(VegaExpr), Instance(ColorScale)), |
| 84 |
).tag(sync=True, **widget_serialization) |
| 85 |
vertical_alignment = Union( |
| 86 |
( |
| 87 |
Enum(values=["top", "center", "bottom"]), |
| 88 |
Instance(VegaExpr), |
| 89 |
Instance(Scale), |
| 90 |
), |
| 91 |
default_value="center", |
| 92 |
).tag(sync=True, **widget_serialization) |
| 93 |
horizontal_alignment = Union( |
| 94 |
( |
| 95 |
Enum(values=["left", "center", "right"]), |
| 96 |
Instance(VegaExpr), |
| 97 |
Instance(Scale), |
| 98 |
), |
| 99 |
default_value="left", |
| 100 |
).tag(sync=True, **widget_serialization) |
| 101 |
format = Union( |
| 102 |
(Unicode(), Instance(VegaExpr)), allow_none=True, default_value=None |
| 103 |
).tag(sync=True, **widget_serialization) |
| 104 |
format_type = Enum(values=["number", "time"], default_value="number").tag( |
| 105 |
sync=True |
| 106 |
) |
| 107 |
missing = Unicode("").tag(sync=True) |
| 108 |
|
| 109 |
@default("text_color") |
| 110 |
def _default_text_color(self): |
| 111 |
return Expr("default_value") |
| 112 |
|
| 113 |
@default("background_color") |
| 114 |
def _default_background_color(self): |
| 115 |
return Expr("default_value") |
| 116 |
|
| 117 |
|
| 118 |
class BarRenderer(TextRenderer): |
| 119 |
_model_name = Unicode("BarRendererModel").tag(sync=True) |
| 120 |
_view_name = Unicode("BarRendererView").tag(sync=True) |
| 121 |
|
| 122 |
bar_value = Union( |
| 123 |
(Float(allow_none=True), Instance(VegaExpr), Instance(Scale)), |
| 124 |
default_value=0.0, |
| 125 |
).tag(sync=True, **widget_serialization) |
| 126 |
bar_color = Union( |
| 127 |
(Color(), Instance(VegaExpr), Instance(ColorScale)), |
| 128 |
default_value="#4682b4", |
| 129 |
).tag(sync=True, **widget_serialization) |
| 130 |
orientation = Union( |
| 131 |
(Unicode(), Instance(VegaExpr), Instance(Scale)), |
| 132 |
default_value="horizontal", |
| 133 |
).tag(sync=True, **widget_serialization) |
| 134 |
bar_vertical_alignment = Union( |
| 135 |
( |
| 136 |
Enum(values=["top", "center", "bottom"]), |
| 137 |
Instance(VegaExpr), |
| 138 |
Instance(Scale), |
| 139 |
), |
| 140 |
default_value="bottom", |
| 141 |
).tag(sync=True, **widget_serialization) |
| 142 |
bar_horizontal_alignment = Union( |
| 143 |
( |
| 144 |
Enum(values=["left", "center", "right"]), |
| 145 |
Instance(VegaExpr), |
| 146 |
Instance(Scale), |
| 147 |
), |
| 148 |
default_value="left", |
| 149 |
).tag(sync=True, **widget_serialization) |
| 150 |
show_text = Union((Bool(), Instance(VegaExpr)), default_value=True).tag( |
| 151 |
sync=True, **widget_serialization |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
class HyperlinkRenderer(TextRenderer): |
| 156 |
_model_name = Unicode("HyperlinkRendererModel").tag(sync=True) |
| 157 |
_view_name = Unicode("HyperlinkRendererView").tag(sync=True) |
| 158 |
|
| 159 |
url = Instance(VegaExpr, allow_none=False).tag( |
| 160 |
sync=True, **widget_serialization |
| 161 |
) |
| 162 |
url_name = Instance(VegaExpr, allow_none=False).tag( |
| 163 |
sync=True, **widget_serialization |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
class ImageRenderer(CellRenderer): |
| 168 |
_model_name = Unicode("ImageRendererModel").tag(sync=True) |
| 169 |
_view_name = Unicode("ImageRendererView").tag(sync=True) |
| 170 |
|
| 171 |
background_color = Union( |
| 172 |
(Color(), Instance(VegaExpr), Instance(ColorScale)), |
| 173 |
allow_none=True, |
| 174 |
default_value=None, |
| 175 |
).tag(sync=True, **widget_serialization) |
| 176 |
placeholder = Union( |
| 177 |
(Unicode(), Instance(VegaExpr), Instance(Scale)), |
| 178 |
allow_none=True, |
| 179 |
default_value="...", |
| 180 |
).tag(sync=True, **widget_serialization) |
| 181 |
text_color = Union( |
| 182 |
(Color(), Instance(VegaExpr), Instance(ColorScale)), |
| 183 |
allow_none=True, |
| 184 |
default_value="#000000", |
| 185 |
).tag(sync=True, **widget_serialization) |
| 186 |
width = Union( |
| 187 |
(Unicode(), Instance(VegaExpr), Instance(Scale)), |
| 188 |
allow_none=True, |
| 189 |
default_value="", |
| 190 |
).tag(sync=True, **widget_serialization) |
| 191 |
height = Union( |
| 192 |
(Unicode(), Instance(VegaExpr), Instance(Scale)), |
| 193 |
allow_none=True, |
| 194 |
default_value="100%", |
| 195 |
).tag(sync=True, **widget_serialization) |
| 196 |
|
| 197 |
|
| 198 |
class HtmlRenderer(CellRenderer): |
| 199 |
_model_name = Unicode("HtmlRendererModel").tag(sync=True) |
| 200 |
_view_name = Unicode("HtmlRendererView").tag(sync=True) |
| 201 |
|
| 202 |
font = Union( |
| 203 |
(Unicode(), Instance(VegaExpr), Instance(Scale)), |
| 204 |
default_value="12px sans-serif", |
| 205 |
).tag(sync=True, **widget_serialization) |
| 206 |
text_color = Union( |
| 207 |
(Color(), Instance(VegaExpr), Instance(ColorScale)), |
| 208 |
).tag(sync=True, **widget_serialization) |
| 209 |
background_color = Union( |
| 210 |
(Color(), Instance(VegaExpr), Instance(ColorScale)), |
| 211 |
).tag(sync=True, **widget_serialization) |
| 212 |
placeholder = Unicode().tag(sync=True) |
| 213 |
vertical_alignment = Union( |
| 214 |
( |
| 215 |
Enum(values=["top", "center", "bottom"]), |
| 216 |
Instance(VegaExpr), |
| 217 |
Instance(Scale), |
| 218 |
), |
| 219 |
default_value="center", |
| 220 |
).tag(sync=True, **widget_serialization) |
| 221 |
horizontal_alignment = Union( |
| 222 |
( |
| 223 |
Enum(values=["left", "center", "right"]), |
| 224 |
Instance(VegaExpr), |
| 225 |
Instance(Scale), |
| 226 |
), |
| 227 |
default_value="left", |
| 228 |
).tag(sync=True, **widget_serialization) |
| 229 |
|
| 230 |
@default("text_color") |
| 231 |
def _default_text_color(self): |
| 232 |
return Expr("default_value") |
| 233 |
|
| 234 |
@default("background_color") |
| 235 |
def _default_background_color(self): |
| 236 |
return Expr("default_value") |