How do people package Altair themes?
Tags: python, altair, datavisualization, vega
Published: 2022-02-24 19:44:37

In [Altair](https://altair-viz.github.io/), we can create custom and shareable themes to apply to different charts. So, several Python packages are available with these themes ready to go. Considering four examples, let's see how other people package their Altair themes. For more information before we talk about them, check the [_Altair Themes_ section available in the documentation](https://altair-viz.github.io/user_guide/configuration.html#altair-themes).

As a first example, we have the [altair-latimes](https://github.com/datadesk/altair-latimes) package. Here we can find the [Los Angeles Times](https://www.latimes.com/) theme for Altair. More specifically, in addition to the `theme()` function that contains some constants and returns a dictionary with the configuration for the theme, there is a color dictionary (`palette`). This color dictionary can also be imported and used directly.

For a theme based on the [University of Washington brand](https://www.washington.edu/brand/graphic-elements/), there is [husky_theme](https://github.com/deppen8/husky-altair-theme). To leverage it, we need to run the respective [file](https://github.com/deppen8/husky-altair-theme/blob/master/husky_theme.py) as a script or add it to a given project (and import the `husky_theme()` function), as opposed to installing and importing a Python package. Like the first example, the `husky_theme()` function contains some constants and returns a dictionary.

In the [hueniversitypy](https://github.com/UBC-MDS/hueniversitypy) (great name!) package, we can find different themes based on the visual identities of Canadian universities. Each theme is defined in its file, similar to the previous examples (a function with some constants and which returns a dictionary). The main difference between this package and the previous examples is that it brings together more than one theme that can be used separately (instead of being dedicated to a single theme).

To use any of the themes available in the three examples mentioned above globally, we need to `alt.themes.register()` and `alt.themes.enable()` them. In the case of the [carbonplan-styles](https://github.com/carbonplan/styles) package, which provides a light theme and a dark theme for Altair and [Matplotlib](https://matplotlib.org/), we just need to enable the theme (and we don't need to import any extras). In other words, there is no need to register the theme, as it is added to the available themes "automatically" from the [entry points](https://github.com/altair-viz/altair/blob/v4.2.0/altair/vegalite/v4/theme.py#L35) defined in the `setup.py` file (or equivalent):

```python
from setuptools import setup

ENTRY_POINTS = {
    "altair.vegalite.v4.theme": [
        "carbonplan_dark = carbonplan_styles.altair:dark",
        "carbonplan_light = carbonplan_styles.altair:light",
    ],
}

setup(name="carbonplan-styles", entry_points=ENTRY_POINTS)
```

As for the Altair themes themselves, there is a color dictionary for each one (`colors.py` file), importable constants for the fonts, and a function for each theme, defined from a more general function (`altair.py` file).

Finally, if you have any questions or suggestions, feel free to leave a comment below!