This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
/
_core.py
275 lines (243 loc) · 10.4 KB
/
_core.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
from collections import OrderedDict
from typing import Any, Dict, IO, Iterable, Optional, Set, Type, Union
import warnings
import altair as alt
from altair_saver.savers import (
Saver,
BasicSaver,
HTMLSaver,
NodeSaver,
SeleniumSaver,
)
from altair_saver.types import JSONDict, Mimebundle
from altair_saver._utils import extract_format, infer_mode_from_spec
_SAVER_METHODS: Dict[str, Type[Saver]] = OrderedDict(
[
("basic", BasicSaver),
("html", HTMLSaver),
("selenium", SeleniumSaver),
("node", NodeSaver),
]
)
def _select_saver(
method: Optional[Union[str, Type[Saver]]],
mode: str,
fmt: Optional[str] = None,
fp: Optional[Union[IO, str]] = None,
) -> Type[Saver]:
"""Get an enabled Saver class that supports the specified format.
Parameters
----------
method : string or Saver class or None
The saver class to use. If None, the saver class will be chosen
automatically.
mode : string
One of "vega" or "vega-lite".
fmt : string, optional
The format to which the spec will be saved. If not specified, it
is inferred from `fp`.
fp : string or file-like object, optional
Only referenced if fmt is None. The name is used to infer the format
if possible.
Returns
-------
Saver : Saver class
The Saver subclass that implements the desired operation.
"""
if isinstance(method, type) and issubclass(method, Saver):
return method
elif isinstance(method, str):
if method in _SAVER_METHODS:
return _SAVER_METHODS[method]
else:
raise ValueError(f"Unrecognized method: {method!r}")
elif method is None:
if fmt is None:
if fp is None:
raise ValueError("Either fmt or fp must be specified")
fmt = extract_format(fp)
for s in _SAVER_METHODS.values():
if s.enabled() and fmt in s.valid_formats[mode]:
return s
raise ValueError(f"No enabled saver found that supports format={fmt!r}")
else:
raise ValueError(f"Unrecognized method: {method}")
def save(
chart: Union[alt.TopLevelMixin, JSONDict],
fp: Optional[Union[IO, str]] = None,
fmt: Optional[str] = None,
mode: Optional[str] = None,
embed_options: Optional[JSONDict] = None,
method: Optional[Union[str, Type[Saver]]] = None,
suppress_data_warning: bool = False,
**kwargs: Any,
) -> Optional[Union[str, bytes]]:
"""Save an Altair, Vega, or Vega-Lite chart
Parameters
----------
chart : alt.Chart or dict
The chart or Vega/Vega-Lite chart specification to be saved
fp : file or filename (optional)
location to save the result. For fmt in ["png", "pdf"], file must be binary.
For fmt in ["svg", "vega", "vega-lite"], file must be text. If not specified,
the serialized chart will be returned.
fmt : string (optional)
The format in which to save the chart. If not specified and fp is a string,
fmt will be determined from the file extension. Options are
["html", "pdf", "png", "svg", "vega", "vega-lite"].
mode : string (optional)
The mode of the input spec. Either "vega-lite" or "vega". If not specified,
it will be inferred from the spec.
embed_options : dict (optional)
A dictionary of options to pass to vega-embed. If not specified, the default
will be drawn from alt.renderers.options.
method : string or type
The save method to use: one of {"node", "selenium", "html", "basic"},
or a subclass of Saver.
suppress_data_warning : bool (optional)
If True, suppress warning about json & csv data transformers.
Additional Parameters
---------------------
vega_version : string (optional)
For method in {"selenium", "html"}, the version of the vega javascript
package to use. Default is alt.VEGA_VERSION.
vegalite_version : string (optional)
For method in {"selenium", "html"}, the version of the vega-lite javascript
package to use. Default is alt.VEGALITE_VERSION.
vegaembed_version : string (optional)
For method in {"selenium", "html"}, the version of the vega-embed javascript
package to use. Default is alt.VEGAEMBED_VERSION.
vega_cli_options : list (optional)
For method="node", a list of additional arguments to pass to vega's CLI functions.
All options will be passed to all Vega commands (e.g., `vg2svg`, `vg2pdf`, etc.).
inline : boolean (optional)
For method="html", specify whether javascript sources should be included
inline rather than loaded from an external CDN. Default: False.
standalone : boolean (optional)
For method="html", specify whether to create a standalone HTML file.
Default is True for save().
webdriver : string or Object (optional)
For method="selenium", the type of webdriver to use: one of "chrome", "firefox",
or a selenium.WebDriver object. Defaults to what is available on your system.
offline : bool (optional)
For method="selenium", whether to save charts in offline mode (default=True). If
false, saving charts will require a web connection to load Javascript from CDN.
scale_factor : integer (optional)
For method="selenium", scale saved image by this factor (default=1). This parameter
value is overridden by embed_options["scaleFactor"] when both are specified.
**kwargs :
Additional keyword arguments are passed to Saver initialization.
Returns
-------
chart : string, bytes, or None
If fp is None, the serialized chart is returned.
If fp is specified, the return value is None.
"""
spec: JSONDict = {}
if isinstance(chart, dict):
spec = chart
else:
if alt.data_transformers.get() in [alt.data.to_json, alt.data.to_csv]:
warnings.warn(
f"save() may not function properly with the {alt.data_transformers.active!r} "
"data transformer: use alt.data_transformers.enable('default'). To "
"suppress this warning, pass suppress_data_warning=True."
)
spec = chart.to_dict()
if mode is None:
mode = infer_mode_from_spec(spec)
if embed_options is None:
embed_options = alt.renderers.options.get("embed_options", None)
Saver = _select_saver(method, mode=mode, fmt=fmt, fp=fp)
saver = Saver(spec, mode=mode, embed_options=embed_options, **kwargs)
return saver.save(fp=fp, fmt=fmt)
def render(
chart: Union[alt.TopLevelMixin, JSONDict],
fmts: Union[str, Iterable[str]],
mode: Optional[str] = None,
embed_options: Optional[JSONDict] = None,
method: Optional[Union[str, Type[Saver]]] = None,
**kwargs: Any,
) -> Mimebundle:
"""Render a chart, returning a mimebundle.
This implements an Altair renderer entry-point, enabled via::
alt.renderers.enable("altair_saver")
Parameters
----------
chart : alt.Chart or dict
The chart or Vega/Vega-Lite chart specification
fmts : string or list of strings
The format(s) to include in the mimebundle. Options are
["html", "pdf", "png", "svg", "vega", "vega-lite"].
mode : string (optional)
The mode of the input spec. Either "vega-lite" or "vega". If not specified,
it will be inferred from the spec.
embed_options : dict (optional)
A dictionary of options to pass to vega-embed. If not specified, the default
will be drawn from alt.renderers.options.
method : string or type
The save method to use: one of {"node", "selenium", "html", "basic"},
or a subclass of Saver.
Additional Parameters
---------------------
vega_version : string (optional)
For method in {"selenium", "html"}, the version of the vega javascript
package to use. Default is alt.VEGA_VERSION.
vegalite_version : string (optional)
For method in {"selenium", "html"}, the version of the vega-lite javascript
package to use. Default is alt.VEGALITE_VERSION.
vegaembed_version : string (optional)
For method in {"selenium", "html"}, the version of the vega-embed javascript
package to use. Default is alt.VEGAEMBED_VERSION.
vega_cli_options : list (optional)
For method="node", a list of additional arguments to pass to vega's CLI functions.
All options will be passed to all Vega commands (e.g., `vg2svg`, `vg2pdf`, etc.).
inline : boolean (optional)
For method="html", specify whether javascript sources should be included
inline rather than loaded from an external CDN. Default: False.
standalone : boolean (optional)
For method="html", specify whether to create a standalone HTML file.
Default is False for render().
webdriver : string or Object (optional)
For method="selenium", the type of webdriver to use: one of "chrome", "firefox",
or a selenium.WebDriver object. Defaults to what is available on your system.
offline : bool (optional)
For method="selenium", whether to save charts in offline mode (default=True). If
false, saving charts will require a web connection to load Javascript from CDN.
**kwargs :
Additional keyword arguments are passed to Saver initialization.
"""
if isinstance(fmts, str):
fmts = [fmts]
mimebundle: Mimebundle = {}
spec: JSONDict = {}
if isinstance(chart, dict):
spec = chart
else:
spec = chart.to_dict()
if mode is None:
mode = infer_mode_from_spec(spec)
if embed_options is None:
embed_options = alt.renderers.options.get("embed_options", None)
for fmt in fmts:
Saver = _select_saver(method, mode=mode, fmt=fmt)
saver = Saver(spec, mode=mode, embed_options=embed_options, **kwargs)
mimebundle.update(saver.mimebundle(fmt))
return mimebundle
def available_formats(mode: str = "vega-lite") -> Set[str]:
"""Return the set of available formats.
Parameters
----------
mode : str
The kind of input; one of "vega", "vega-lite"
Returns
-------
formats : set of strings
Formats available in the current session.
"""
valid_modes = ("vega", "vega-lite")
if mode not in valid_modes:
raise ValueError(f"Invalid mode: {mode!r}. Must be one of {valid_modes!r}")
return set.union(
*(set(s.valid_formats[mode]) for s in _SAVER_METHODS.values() if s.enabled())
)