-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy path__init__.py
More file actions
246 lines (203 loc) · 7.02 KB
/
__init__.py
File metadata and controls
246 lines (203 loc) · 7.02 KB
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
"""
Kaleido is a library for generating static images from Plotly figures.
Please see the README.md for more information and a quickstart.
"""
from __future__ import annotations
import warnings
from typing import TYPE_CHECKING, Literal
from choreographer.cli import get_chrome, get_chrome_sync
from . import _sync_server
from ._page_generator import PageGenerator
from .kaleido import Kaleido, _resolve_timeout
if TYPE_CHECKING:
from collections.abc import AsyncIterable, Iterable
from pathlib import Path
from typing import Any, TypeVar, Union
from ._utils.fig_tools import Figurish, LayoutOpts
T = TypeVar("T")
AnyIterable = Union[AsyncIterable[T], Iterable[T]]
from .kaleido import FigureDict
__all__ = [
"Kaleido",
"PageGenerator",
"calc_fig",
"calc_fig_sync",
"get_chrome",
"get_chrome_sync",
"start_sync_server",
"stop_sync_server",
"write_fig",
"write_fig_from_object",
"write_fig_from_object_sync",
"write_fig_sync",
]
_global_server = _sync_server.GlobalKaleidoServer()
def start_sync_server(*args: Any, silence_warnings: bool = False, **kwargs: Any):
"""
Start a kaleido server which will process all sync generation requests.
The kaleido server is a singleton, so it can't be opened twice. This
function will warn you if the server is already running.
This wrapper function takes the exact same arguments as kaleido.Kaleido(),
except one extra: `silence_warnings`.
Args:
*args: all arguments `Kaleido()` would take.
silence_warnings: (bool, default False): If True, don't emit warning if
starting an already started server.
**kwargs: all keyword arguments `Kaleido()` would take.
"""
_global_server.open(*args, silence_warnings=silence_warnings, **kwargs)
def stop_sync_server(*, silence_warnings: bool = False):
"""
Stop the kaleido server. It can be restarted.
This function will warn you if the server is already stopped.
Args:
silence_warnings: (bool, default False): If True, don't emit warning if
stopping an already stopped server.
"""
_global_server.close(silence_warnings=silence_warnings)
async def calc_fig(
fig: Figurish,
opts: LayoutOpts | None = None,
*,
topojson: str | None = None,
kopts: dict[str, Any] | None = None,
timeout: float | None | Literal["auto"] = "auto",
):
"""
Return binary for plotly figure.
A convenience wrapper for `Kaleido.calc_fig()` which starts a `Kaleido` and
executes `calc_fig()`.
It takes an additional argument, `kopts`, a dictionary of arguments to pass
to the kaleido process. See the `kaleido.Kaleido` docs. However,
`calc_fig()` will never use more than one processor, so any `n` value will
be overridden.
See also the documentation for `Kaleido.calc_fig()`.
"""
kopts = kopts or {}
kopts.setdefault("timeout", timeout)
kopts["n"] = 1 # should we force this?
async with Kaleido(**kopts) as k:
return await k.calc_fig(
fig,
opts=opts,
topojson=topojson,
)
async def write_fig(
fig: Figurish,
path: str | None | Path = None,
opts: LayoutOpts | None = None,
*,
topojson: str | None = None,
kopts: dict[str, Any] | None = None,
timeout: float | None | Literal["auto"] = "auto",
**kwargs,
):
"""
Write a plotly figure(s) to a file.
A convenience wrapper for `Kaleido.write_fig()` which starts a `Kaleido` and
executes the `write_fig()`.
It takes an additional argument, `kopts`, a dictionary of arguments to pass
to the `Kaleido` constructor. See the `kaleido.Kaleido` docs.
See also the documentation for `Kaleido.write_fig()`.
"""
kopts = kopts or {}
kopts.setdefault("timeout", timeout)
async with Kaleido(**kopts) as k:
return await k.write_fig(
fig,
path=path,
opts=opts,
topojson=topojson,
**kwargs,
)
async def write_fig_from_object(
fig_dicts: FigureDict | AnyIterable[FigureDict],
*,
kopts: dict[str, Any] | None = None,
timeout: float | None | Literal["auto"] = "auto",
**kwargs,
):
"""
Write a plotly figure(s) to a file specified by a dictionary or iterable of.
A convenience wrapper for `Kaleido.write_fig_from_object()` which starts a
`Kaleido` and executes the `write_fig_from_object()`
It takes an additional argument, `kopts`, a dictionary of arguments to pass
to the `Kaleido` constructor. See the `kaleido.Kaleido` docs.
See also the documentation for `Kaleido.write_fig_from_object()`.
"""
kopts = kopts or {}
kopts.setdefault("timeout", timeout)
async with Kaleido(**kopts) as k:
return await k.write_fig_from_object(
fig_dicts,
**kwargs,
)
def calc_fig_sync(
*args: Any,
timeout: float | None | Literal["auto"] = "auto",
**kwargs: Any,
):
"""Call `calc_fig` but blocking."""
if _global_server.is_running():
if timeout != "auto":
warnings.warn(
"The timeout argument is ignored if using a server.",
UserWarning,
stacklevel=2,
)
return _global_server.call_function("calc_fig", *args, **kwargs)
else:
kwargs.setdefault("timeout", timeout)
sync_timeout = _resolve_timeout(timeout)
return _sync_server.oneshot_async_run(
calc_fig,
args=args,
kwargs=kwargs,
sync_timeout=sync_timeout,
)
def write_fig_sync(
*args: Any,
timeout: float | None | Literal["auto"] = "auto",
**kwargs: Any,
):
"""Call `write_fig` but blocking."""
if _global_server.is_running():
if timeout != "auto":
warnings.warn(
"The timeout argument is ignored if using a server.",
UserWarning,
stacklevel=2,
)
return _global_server.call_function("write_fig", *args, **kwargs)
else:
kwargs.setdefault("timeout", timeout)
sync_timeout = _resolve_timeout(timeout)
return _sync_server.oneshot_async_run(
write_fig,
args=args,
kwargs=kwargs,
sync_timeout=sync_timeout,
)
def write_fig_from_object_sync(
*args: Any,
timeout: float | None | Literal["auto"] = "auto",
**kwargs: Any,
):
"""Call `write_fig_from_object` but blocking."""
if _global_server.is_running():
if timeout != "auto":
warnings.warn(
"The timeout argument is ignored if using a server.",
UserWarning,
stacklevel=2,
)
return _global_server.call_function("write_fig_from_object", *args, **kwargs)
else:
kwargs.setdefault("timeout", timeout)
sync_timeout = _resolve_timeout(timeout)
return _sync_server.oneshot_async_run(
write_fig_from_object,
args=args,
kwargs=kwargs,
sync_timeout=sync_timeout,
)