-
Notifications
You must be signed in to change notification settings - Fork 793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: alt.themes
-> alt.theme
#3618
base: main
Are you sure you want to change the base?
Changes from 15 commits
54e7e48
66c51c3
754f665
3dd9100
b1be633
0bd8f75
c996c95
c286c38
ef9b846
e8d3062
f7fe38b
0a1c8d3
05eca89
217d7fb
5078f88
2339116
48e44c0
a7dbaca
a0947f8
1e9f639
d895447
a9538bc
e114493
c8033b2
ef0f263
58da996
bc507a9
e0f4721
4e8def0
9bd0316
0d95b44
3e6bde8
6141c7c
d1ae98e
2ca4b11
d3dccab
7c49b51
a3e2d28
a018165
98f4ed3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from __future__ import annotations | ||
|
||
# ruff: noqa | ||
__version__ = "5.5.0dev" | ||
|
||
|
@@ -617,7 +619,6 @@ | |
"mixins", | ||
"param", | ||
"parse_shorthand", | ||
"register_theme", | ||
"renderers", | ||
"repeat", | ||
"sample", | ||
|
@@ -627,7 +628,6 @@ | |
"sequence", | ||
"sphere", | ||
"theme", | ||
"themes", | ||
"to_csv", | ||
"to_json", | ||
"to_values", | ||
|
@@ -653,10 +653,29 @@ def __dir__(): | |
from altair.jupyter import JupyterChart | ||
from altair.expr import expr | ||
from altair.utils import AltairDeprecationWarning, parse_shorthand, Undefined | ||
from altair import typing | ||
from altair import typing, theme | ||
from typing import Any as _Any | ||
|
||
|
||
def load_ipython_extension(ipython): | ||
from altair._magics import vegalite | ||
|
||
ipython.register_magic_function(vegalite, "cell") | ||
|
||
|
||
def __getattr__(name: str) -> _Any: | ||
from altair.utils.deprecation import deprecated_warn | ||
|
||
if name == "themes": | ||
deprecated_warn( | ||
"Most of the `ThemeRegistry` API is accessible via `altair.theme`.\n" | ||
"See the updated User Guide for further details:\n" | ||
"https://altair-viz.github.io/user_guide/customization.html#chart-themes", | ||
dangotbanned marked this conversation as resolved.
Show resolved
Hide resolved
|
||
version="5.5.0", | ||
alternative="altair.theme.themes", | ||
stacklevel=3, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I was testing, the default I'd set of 2 didn't report any warnings. Not sure if that is related to |
||
) | ||
return theme.themes | ||
else: | ||
msg = f"module {__name__!r} has no attribute {name!r}" | ||
raise AttributeError(msg) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
"""Customizing chart configuration defaults.""" | ||
|
||
from __future__ import annotations | ||
|
||
from functools import wraps as _wraps | ||
from typing import TYPE_CHECKING | ||
|
||
from altair.vegalite.v5.schema._config import ( | ||
AreaConfigKwds, | ||
AutoSizeParamsKwds, | ||
AxisConfigKwds, | ||
AxisResolveMapKwds, | ||
BarConfigKwds, | ||
BindCheckboxKwds, | ||
BindDirectKwds, | ||
BindInputKwds, | ||
BindRadioSelectKwds, | ||
dangotbanned marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BindRangeKwds, | ||
BoxPlotConfigKwds, | ||
BrushConfigKwds, | ||
CompositionConfigKwds, | ||
ConfigKwds, | ||
DateTimeKwds, | ||
DerivedStreamKwds, | ||
ErrorBandConfigKwds, | ||
ErrorBarConfigKwds, | ||
FeatureGeometryGeoJsonPropertiesKwds, | ||
FormatConfigKwds, | ||
GeoJsonFeatureCollectionKwds, | ||
GeoJsonFeatureKwds, | ||
GeometryCollectionKwds, | ||
GradientStopKwds, | ||
HeaderConfigKwds, | ||
IntervalSelectionConfigKwds, | ||
IntervalSelectionConfigWithoutTypeKwds, | ||
LegendConfigKwds, | ||
LegendResolveMapKwds, | ||
LegendStreamBindingKwds, | ||
LinearGradientKwds, | ||
LineConfigKwds, | ||
LineStringKwds, | ||
LocaleKwds, | ||
MarkConfigKwds, | ||
MergedStreamKwds, | ||
MultiLineStringKwds, | ||
MultiPointKwds, | ||
MultiPolygonKwds, | ||
NumberLocaleKwds, | ||
OverlayMarkDefKwds, | ||
PaddingKwds, | ||
PointKwds, | ||
PointSelectionConfigKwds, | ||
PointSelectionConfigWithoutTypeKwds, | ||
PolygonKwds, | ||
ProjectionConfigKwds, | ||
ProjectionKwds, | ||
RadialGradientKwds, | ||
RangeConfigKwds, | ||
RectConfigKwds, | ||
ResolveKwds, | ||
RowColKwds, | ||
ScaleConfigKwds, | ||
ScaleInvalidDataConfigKwds, | ||
ScaleResolveMapKwds, | ||
SelectionConfigKwds, | ||
StepKwds, | ||
StyleConfigIndexKwds, | ||
ThemeConfig, | ||
TickConfigKwds, | ||
TimeIntervalStepKwds, | ||
TimeLocaleKwds, | ||
TitleConfigKwds, | ||
TitleParamsKwds, | ||
TooltipContentKwds, | ||
TopLevelSelectionParameterKwds, | ||
VariableParameterKwds, | ||
ViewBackgroundKwds, | ||
ViewConfigKwds, | ||
) | ||
from altair.vegalite.v5.theme import themes | ||
|
||
if TYPE_CHECKING: | ||
import sys | ||
from typing import Callable | ||
|
||
if sys.version_info >= (3, 11): | ||
from typing import LiteralString | ||
else: | ||
from typing_extensions import LiteralString | ||
if sys.version_info >= (3, 10): | ||
from typing import ParamSpec | ||
else: | ||
from typing_extensions import ParamSpec | ||
|
||
from altair.utils.plugin_registry import Plugin | ||
|
||
P = ParamSpec("P") | ||
|
||
__all__ = [ | ||
"AreaConfigKwds", | ||
"AutoSizeParamsKwds", | ||
"AxisConfigKwds", | ||
"AxisResolveMapKwds", | ||
"BarConfigKwds", | ||
"BindCheckboxKwds", | ||
"BindDirectKwds", | ||
"BindInputKwds", | ||
"BindRadioSelectKwds", | ||
"BindRangeKwds", | ||
"BoxPlotConfigKwds", | ||
"BrushConfigKwds", | ||
"CompositionConfigKwds", | ||
"ConfigKwds", | ||
"DateTimeKwds", | ||
"DerivedStreamKwds", | ||
"ErrorBandConfigKwds", | ||
"ErrorBarConfigKwds", | ||
"FeatureGeometryGeoJsonPropertiesKwds", | ||
"FormatConfigKwds", | ||
"GeoJsonFeatureCollectionKwds", | ||
"GeoJsonFeatureKwds", | ||
"GeometryCollectionKwds", | ||
"GradientStopKwds", | ||
"HeaderConfigKwds", | ||
"IntervalSelectionConfigKwds", | ||
"IntervalSelectionConfigWithoutTypeKwds", | ||
"LegendConfigKwds", | ||
"LegendResolveMapKwds", | ||
"LegendStreamBindingKwds", | ||
"LineConfigKwds", | ||
"LineStringKwds", | ||
"LinearGradientKwds", | ||
"LocaleKwds", | ||
"MarkConfigKwds", | ||
"MergedStreamKwds", | ||
"MultiLineStringKwds", | ||
"MultiPointKwds", | ||
"MultiPolygonKwds", | ||
"NumberLocaleKwds", | ||
"OverlayMarkDefKwds", | ||
"PaddingKwds", | ||
"PointKwds", | ||
"PointSelectionConfigKwds", | ||
"PointSelectionConfigWithoutTypeKwds", | ||
"PolygonKwds", | ||
"ProjectionConfigKwds", | ||
"ProjectionKwds", | ||
"RadialGradientKwds", | ||
"RangeConfigKwds", | ||
"RectConfigKwds", | ||
"ResolveKwds", | ||
"RowColKwds", | ||
"ScaleConfigKwds", | ||
"ScaleInvalidDataConfigKwds", | ||
"ScaleResolveMapKwds", | ||
"SelectionConfigKwds", | ||
"StepKwds", | ||
"StyleConfigIndexKwds", | ||
"ThemeConfig", | ||
"ThemeConfig", | ||
"TickConfigKwds", | ||
"TimeIntervalStepKwds", | ||
"TimeLocaleKwds", | ||
"TitleConfigKwds", | ||
"TitleParamsKwds", | ||
"TooltipContentKwds", | ||
"TopLevelSelectionParameterKwds", | ||
"VariableParameterKwds", | ||
"ViewBackgroundKwds", | ||
"ViewConfigKwds", | ||
"enable", | ||
"get", | ||
"names", | ||
"register", | ||
"themes", | ||
"unregister", | ||
] | ||
|
||
|
||
def register( | ||
name: LiteralString, *, enable: bool | ||
) -> Callable[[Plugin[ThemeConfig]], Plugin[ThemeConfig]]: | ||
""" | ||
Decorator for registering a theme function. | ||
|
||
Parameters | ||
---------- | ||
name | ||
Unique name assigned in ``alt.theme.themes``. | ||
enable | ||
Auto-enable the wrapped theme. | ||
|
||
Examples | ||
-------- | ||
Register and enable a theme:: | ||
|
||
import altair as alt | ||
from altair import theme | ||
|
||
|
||
@theme.register("param_font_size", enable=True) | ||
def custom_theme() -> theme.ThemeConfig: | ||
sizes = 12, 14, 16, 18, 20 | ||
return { | ||
"autosize": {"contains": "content", "resize": True}, | ||
"background": "#F3F2F1", | ||
"config": { | ||
"axisX": {"labelFontSize": sizes[1], "titleFontSize": sizes[1]}, | ||
"axisY": {"labelFontSize": sizes[1], "titleFontSize": sizes[1]}, | ||
"font": "'Lato', 'Segoe UI', Tahoma, Verdana, sans-serif", | ||
"headerColumn": {"labelFontSize": sizes[1]}, | ||
"headerFacet": {"labelFontSize": sizes[1]}, | ||
"headerRow": {"labelFontSize": sizes[1]}, | ||
"legend": {"labelFontSize": sizes[0], "titleFontSize": sizes[1]}, | ||
"text": {"fontSize": sizes[0]}, | ||
"title": {"fontSize": sizes[-1]}, | ||
}, | ||
"height": {"step": 28}, | ||
"width": 350, | ||
} | ||
|
||
Until another theme has been enabled, all charts will use defaults set in ``custom_theme``:: | ||
|
||
from vega_datasets import data | ||
|
||
source = data.stocks() | ||
lines = ( | ||
alt.Chart(source, title=alt.Title("Stocks")) | ||
.mark_line() | ||
.encode(x="date:T", y="price:Q", color="symbol:N") | ||
) | ||
lines.interactive(bind_y=False) | ||
|
||
""" | ||
|
||
# HACK: See for `LiteralString` requirement in `name` | ||
# https://github.com/vega/altair/pull/3526#discussion_r1743350127 | ||
def decorate(func: Plugin[ThemeConfig], /) -> Plugin[ThemeConfig]: | ||
themes.register(name, func) | ||
if enable: | ||
themes.enable(name) | ||
|
||
@_wraps(func) | ||
def wrapper(*args: P.args, **kwargs: P.kwargs) -> ThemeConfig: | ||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorate | ||
|
||
|
||
def unregister(name: LiteralString) -> Plugin[ThemeConfig] | None: | ||
""" | ||
Remove and return a previously registered theme. | ||
|
||
Parameters | ||
---------- | ||
name | ||
Unique name assigned in ``alt.theme.themes``. | ||
""" | ||
return themes.register(name, None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note
|
||
|
||
|
||
enable = themes.enable | ||
get = themes.get | ||
names = themes.names | ||
Comment on lines
+280
to
+282
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solution is fine currently, but as soon as we start deprecating other imports it would make sense to generalize