Skip to content

Commit 64c29f7

Browse files
authored
Merge branch 'master' into patch-4
2 parents cc02702 + 992a307 commit 64c29f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2076
-11068
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ node_modules/
2626

2727
# IDE
2828
.idea
29+
30+
src/py/site/*

src/py/.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repos:
3030
- id: add-trailing-comma
3131
- repo: https://github.com/astral-sh/ruff-pre-commit
3232
# Ruff version.
33-
rev: v0.8.2
33+
rev: v0.12.10
3434
hooks:
3535
# Run the linter.
3636
- id: ruff

src/py/CHANGELOG.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
v1.1.0
2+
- Add testing
3+
- Fix a variety of type bugs
4+
- Change order of browser closer to fix hang
5+
- Explicitly handle certain argument options better
6+
- Move temp file creation to .open() out of __init__()
7+
- Reduce mathjax version to plotly.py
8+
- Fix hang and add automatic close with stop_sync_server
9+
- Add option to silence warnings in start/stop_sync_server
10+
- Fix bug where attribute was inconsistently named
11+
112
v1.1.0rc0
213
- Improve verbosity of errors when starting kaleido improperly
314
- Add new api functions start/stop_sync_server

src/py/kaleido/__init__.py

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,23 @@
66

77
from __future__ import annotations
88

9+
from typing import TYPE_CHECKING
10+
911
from choreographer.cli import get_chrome, get_chrome_sync
1012

1113
from . import _sync_server
1214
from ._page_generator import PageGenerator
1315
from .kaleido import Kaleido
1416

15-
_global_server = _sync_server.GlobalKaleidoServer()
16-
17-
18-
def start_sync_server(*args, **kwargs):
19-
"""
20-
Start a kaleido server which will process all sync generation requests.
21-
22-
Only one server can be started at a time.
23-
24-
This wrapper function takes the exact same arguments as kaleido.Kaleido().
25-
"""
26-
_global_server.open(*args, **kwargs)
17+
if TYPE_CHECKING:
18+
from collections.abc import AsyncIterable, Iterable
19+
from pathlib import Path
20+
from typing import Any, TypeVar, Union
2721

22+
from ._fig_tools import Figurish, LayoutOpts
2823

29-
def stop_sync_server():
30-
"""Stop the kaleido server. It can be restarted."""
31-
_global_server.close()
32-
24+
T = TypeVar("T")
25+
AnyIterable = Union[AsyncIterable[T], Iterable[T]]
3326

3427
__all__ = [
3528
"Kaleido",
@@ -46,14 +39,48 @@ def stop_sync_server():
4639
"write_fig_sync",
4740
]
4841

42+
_global_server = _sync_server.GlobalKaleidoServer()
43+
44+
45+
def start_sync_server(*args: Any, silence_warnings: bool = False, **kwargs: Any):
46+
"""
47+
Start a kaleido server which will process all sync generation requests.
48+
49+
The kaleido server is a singleton, so it can't be opened twice. This
50+
function will warn you if the server is already running.
51+
52+
This wrapper function takes the exact same arguments as kaleido.Kaleido(),
53+
except one extra, `silence_warnings`.
54+
55+
Args:
56+
*args: all arguments `Kaleido()` would take.
57+
silence_warnings: (bool, default False): If True, don't emit warning if
58+
starting an already started server.
59+
**kwargs: all keyword arguments `Kaleido()` would take.
60+
61+
"""
62+
_global_server.open(*args, silence_warnings=silence_warnings, **kwargs)
63+
64+
65+
def stop_sync_server(*, silence_warnings: bool = False):
66+
"""
67+
Stop the kaleido server. It can be restarted. Warns if not started.
68+
69+
Args:
70+
silence_warnings: (bool, default False): If True, don't emit warning if
71+
stopping a server that's not running.
72+
73+
"""
74+
_global_server.close(silence_warnings=silence_warnings)
75+
4976

5077
async def calc_fig(
51-
fig,
52-
path=None,
53-
opts=None,
78+
fig: Figurish,
79+
path: str | None | Path = None,
80+
opts: LayoutOpts | None = None,
5481
*,
55-
topojson=None,
56-
kopts=None,
82+
topojson: str | None = None,
83+
kopts: dict[str, Any] | None = None,
5784
):
5885
"""
5986
Return binary for plotly figure.
@@ -70,7 +97,7 @@ async def calc_fig(
7097
7198
"""
7299
kopts = kopts or {}
73-
kopts["n"] = 1
100+
kopts["n"] = 1 # should we force this?
74101
async with Kaleido(**kopts) as k:
75102
return await k.calc_fig(
76103
fig,
@@ -80,15 +107,14 @@ async def calc_fig(
80107
)
81108

82109

83-
async def write_fig( # noqa: PLR0913 (too many args, complexity)
84-
fig,
85-
path=None,
86-
opts=None,
110+
async def write_fig(
111+
fig: Figurish,
112+
path: str | None | Path = None,
113+
opts: LayoutOpts | None = None,
87114
*,
88-
topojson=None,
89-
error_log=None,
90-
profiler=None,
91-
kopts=None,
115+
topojson: str | None = None,
116+
kopts: dict[str, Any] | None = None,
117+
**kwargs,
92118
):
93119
"""
94120
Write a plotly figure(s) to a file.
@@ -108,17 +134,15 @@ async def write_fig( # noqa: PLR0913 (too many args, complexity)
108134
path=path,
109135
opts=opts,
110136
topojson=topojson,
111-
error_log=error_log,
112-
profiler=profiler,
137+
**kwargs,
113138
)
114139

115140

116141
async def write_fig_from_object(
117-
generator,
142+
generator: AnyIterable, # this could be more specific with []
118143
*,
119-
error_log=None,
120-
profiler=None,
121-
kopts=None,
144+
kopts: dict[str, Any] | None = None,
145+
**kwargs,
122146
):
123147
"""
124148
Write a plotly figure(s) to a file.
@@ -135,28 +159,27 @@ async def write_fig_from_object(
135159
async with Kaleido(**(kopts or {})) as k:
136160
await k.write_fig_from_object(
137161
generator,
138-
error_log=error_log,
139-
profiler=profiler,
162+
**kwargs,
140163
)
141164

142165

143-
def calc_fig_sync(*args, **kwargs):
166+
def calc_fig_sync(*args: Any, **kwargs: Any):
144167
"""Call `calc_fig` but blocking."""
145168
if _global_server.is_running():
146169
return _global_server.call_function("calc_fig", *args, **kwargs)
147170
else:
148171
return _sync_server.oneshot_async_run(calc_fig, args=args, kwargs=kwargs)
149172

150173

151-
def write_fig_sync(*args, **kwargs):
174+
def write_fig_sync(*args: Any, **kwargs: Any):
152175
"""Call `write_fig` but blocking."""
153176
if _global_server.is_running():
154177
_global_server.call_function("write_fig", *args, **kwargs)
155178
else:
156179
_sync_server.oneshot_async_run(write_fig, args=args, kwargs=kwargs)
157180

158181

159-
def write_fig_from_object_sync(*args, **kwargs):
182+
def write_fig_from_object_sync(*args: Any, **kwargs: Any):
160183
"""Call `write_fig_from_object` but blocking."""
161184
if _global_server.is_running():
162185
_global_server.call_function("write_fig_from_object", *args, **kwargs)

0 commit comments

Comments
 (0)