-
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
feat: Adds 4 missing carbon
themes, provide autocomplete
#3516
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a529481
style: Sort `VEGA_THEMES` alphabetically
dangotbanned 8e2990b
feat: Add missing `carbon...` themes
dangotbanned dca03b9
feat(typing): Support autocomplete for `themes.enable(name)`
dangotbanned 42fac3b
chore: Fix `PluginRegistery` comment typo
dangotbanned 105aab6
Merge branch 'main' into update-vega-themes
dangotbanned 2f08c63
Add comments
binste File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,49 @@ | ||
"""Utilities for registering and working with themes.""" | ||
|
||
from typing import Callable | ||
from __future__ import annotations | ||
|
||
import sys | ||
from typing import TYPE_CHECKING, Callable | ||
|
||
from .plugin_registry import PluginRegistry | ||
|
||
if sys.version_info >= (3, 11): | ||
from typing import LiteralString | ||
else: | ||
from typing_extensions import LiteralString | ||
|
||
if TYPE_CHECKING: | ||
from altair.utils.plugin_registry import PluginEnabler | ||
from altair.vegalite.v5.theme import _ThemeName | ||
|
||
ThemeType = Callable[..., dict] | ||
|
||
|
||
class ThemeRegistry(PluginRegistry[ThemeType, dict]): | ||
pass | ||
def enable( | ||
self, name: LiteralString | _ThemeName | None = None, **options | ||
) -> PluginEnabler: | ||
""" | ||
Enable a theme by name. | ||
|
||
This can be either called directly, or used as a context manager. | ||
|
||
Parameters | ||
---------- | ||
name : string (optional) | ||
The name of the theme to enable. If not specified, then use the | ||
current active name. | ||
**options : | ||
Any additional parameters will be passed to the theme as keyword | ||
arguments | ||
|
||
Returns | ||
------- | ||
PluginEnabler: | ||
An object that allows enable() to be used as a context manager | ||
|
||
Notes | ||
----- | ||
Default `vega` themes can be previewed at https://vega.github.io/vega-themes/ | ||
""" | ||
return super().enable(name, **options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Instead of defining the theme names twice, I think you could specify
_ThemeName
as you've done here and thenVEGA_THEMES
could becometyping.get_args(_ThemeName
). See https://docs.python.org/3/library/typing.html#typing.get_argsThere 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.
Thanks @binste for the review!
I think I could rework this to use
typing.get_args
, but wanted to note some things first that led to my duplication here.I'm viewing
_ThemeName
as a short-term solution:TYPE_CHECKING
block to prevent it existing at runtime.I don't think I've used this pattern anywhere in
altair
before, but some examples in other projects (for their own reasons):Examples
IIRC, I'd need to move
_ThemeName
out of that block or usetyping.get_type_hints
I'd also need to remove
"default", "opaque"
in the def of_ThemeName
or later during iteration:Registration source
altair/altair/vegalite/v5/theme.py
Lines 45 to 61 in c12ca27
For a long-term solution, I'd want to:
VegaThemes: Literal[...]
)altair
themes as (AltairThemes: Literal["opaque", ...
)->_ThemeName
DefaultThemes: TypeAlias = AltairThemes | VegaThemes
themes.enable(name: DefaultThemes)
theme.py
modules, as part of Flatten the package structure #3337Essentially I want to keep this as flexible as possible for now.
Planning to open another issue to discuss themes more generally.
There are other ideas I've had and I got the impression you might also have some of your own 🤝
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.
Steps re long-term solution sound great! We could probably keep it simple and just fetch the themes from the latest source file in vega-themes which you linked. Or the file which was part of the latest release.
Let's continue the discussion in a new issue as you suggested, thanks for bringing it up! :) I have some notes on providing a better default theme and additional documentation around customization (advanced legend manipulation, vertical y-axis titles, y-axis on the right side with tick labels on top of the ticks, ...)
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.
I've added 2 comments to mention that these two variables need to be kept in sync for now. Just in case we don't get to an improved solution for a while.
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.
Perfect, thanks again @binste