Skip to content

Commit 15f1151

Browse files
dangotbannedbinste
andauthored
feat: Adds 4 missing carbon themes, provide autocomplete (#3516)
* style: Sort `VEGA_THEMES` alphabetically * feat: Add missing `carbon...` themes Examples https://vega.github.io/vega-themes/?theme=carbonwhite Related vega/vega-themes#587 * feat(typing): Support autocomplete for `themes.enable(name)` And provide link to `vega-themes` playground * chore: Fix `PluginRegistery` comment typo * Add comments --------- Co-authored-by: Stefan Binder <binder_stefan@outlook.com>
1 parent c12ca27 commit 15f1151

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

altair/utils/theme.py

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
11
"""Utilities for registering and working with themes."""
22

3-
from typing import Callable
3+
from __future__ import annotations
4+
5+
import sys
6+
from typing import TYPE_CHECKING, Callable
47

58
from .plugin_registry import PluginRegistry
69

10+
if sys.version_info >= (3, 11):
11+
from typing import LiteralString
12+
else:
13+
from typing_extensions import LiteralString
14+
15+
if TYPE_CHECKING:
16+
from altair.utils.plugin_registry import PluginEnabler
17+
from altair.vegalite.v5.theme import _ThemeName
18+
719
ThemeType = Callable[..., dict]
820

921

1022
class ThemeRegistry(PluginRegistry[ThemeType, dict]):
11-
pass
23+
def enable(
24+
self, name: LiteralString | _ThemeName | None = None, **options
25+
) -> PluginEnabler:
26+
"""
27+
Enable a theme by name.
28+
29+
This can be either called directly, or used as a context manager.
30+
31+
Parameters
32+
----------
33+
name : string (optional)
34+
The name of the theme to enable. If not specified, then use the
35+
current active name.
36+
**options :
37+
Any additional parameters will be passed to the theme as keyword
38+
arguments
39+
40+
Returns
41+
-------
42+
PluginEnabler:
43+
An object that allows enable() to be used as a context manager
44+
45+
Notes
46+
-----
47+
Default `vega` themes can be previewed at https://vega.github.io/vega-themes/
48+
"""
49+
return super().enable(name, **options)

altair/vegalite/v5/theme.py

+41-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,54 @@
22

33
from __future__ import annotations
44

5-
from typing import Final
5+
from typing import TYPE_CHECKING, Final, Literal
66

77
from altair.utils.theme import ThemeRegistry
88

9+
if TYPE_CHECKING:
10+
import sys
11+
12+
if sys.version_info >= (3, 10):
13+
from typing import TypeAlias
14+
else:
15+
from typing_extensions import TypeAlias
16+
17+
# If you add a theme here, also add it in `VEGA_THEMES` below.
18+
_ThemeName: TypeAlias = Literal[
19+
"default",
20+
"carbonwhite",
21+
"carbong10",
22+
"carbong90",
23+
"carbong100",
24+
"dark",
25+
"excel",
26+
"fivethirtyeight",
27+
"ggplot2",
28+
"googlecharts",
29+
"latimes",
30+
"opaque",
31+
"powerbi",
32+
"quartz",
33+
"urbaninstitute",
34+
"vox",
35+
]
36+
37+
# If you add a theme here, also add it in `_ThemeName` above.
938
VEGA_THEMES = [
10-
"ggplot2",
11-
"quartz",
12-
"vox",
13-
"fivethirtyeight",
39+
"carbonwhite",
40+
"carbong10",
41+
"carbong90",
42+
"carbong100",
1443
"dark",
15-
"latimes",
16-
"urbaninstitute",
1744
"excel",
45+
"fivethirtyeight",
46+
"ggplot2",
1847
"googlecharts",
48+
"latimes",
1949
"powerbi",
50+
"quartz",
51+
"urbaninstitute",
52+
"vox",
2053
]
2154

2255

@@ -38,7 +71,7 @@ def __repr__(self) -> str:
3871

3972
# The entry point group that can be used by other packages to declare other
4073
# themes that will be auto-detected. Explicit registration is also
41-
# allowed by the PluginRegistery API.
74+
# allowed by the PluginRegistry API.
4275
ENTRY_POINT_GROUP: Final = "altair.vegalite.v5.theme"
4376
themes = ThemeRegistry(entry_point_group=ENTRY_POINT_GROUP)
4477

0 commit comments

Comments
 (0)