Skip to content

Theme Reference

Bases: Theme

A container for style information used by rich.console.Console.

Parameters:

Name Type Description Default
styles Optional[dict[str, StyleType]]

Mapping of style names to styles, e.g. {"warning": "b u"}. Defaults to None, which uses DEFAULT_STYLES.

None
Notes

The theme always inherits Rich's default styles (inherit=True).

Source code in src/rich_gradient/theme.py
class GradientTheme(Theme):
    """A container for style information used by ``rich.console.Console``.

    Args:
        styles (Optional[dict[str, StyleType]]): Mapping of style names to styles, e.g.
            ``{"warning": "b u"}``. Defaults to None, which uses
            ``DEFAULT_STYLES``.

    Notes:
        The theme always inherits Rich's default styles (``inherit=True``).
    """

    # styles: Dict[str, Style] = {}

    def __init__(self, styles: dict[str, StyleType] | None = None) -> None:
        """Initialize the theme; see the class docstring for arguments."""
        if styles is None:
            styles = dict(DEFAULT_STYLES)
        super().__init__(styles=styles, inherit=True)
        self._theme: Theme = Theme(styles, inherit=True)
        self._styles: dict[str, StyleType] = styles

    @property
    def theme(self) -> Theme:
        """The ``rich.theme.Theme`` built from this instance's styles."""
        return self._theme

    @theme.setter
    def theme(self, theme: Theme = Theme(DEFAULT_STYLES)) -> None:
        """Replace the theme used for styling.

        Args:
            theme: The new Rich theme. Defaults to a theme of ``DEFAULT_STYLES``.
        """
        self._theme = theme

    def __call__(self) -> Theme:
        """Return the theme, so the instance can be used as a theme factory.

        Returns:
            The ``rich.theme.Theme`` for this instance.
        """
        return self.theme

    def __repr__(self) -> str:
        """Return a developer-friendly representation.

        Returns:
            ``GradientTheme({...styles...})``.
        """
        return f"GradientTheme({self._styles!r})"

    def __rich__(self) -> RichTable:
        """Render the theme as a table of its styles.

        Returns:
            The styles table from ``default_styles.styles_table()``.
        """
        return styles_table()

    def __getitem__(self, name: str) -> Style:
        """Look up a style by name, e.g. ``theme["warning"]``.

        Args:
            name: The style name.

        Returns:
            The parsed ``Style``.

        Raises:
            KeyError: If no style has that name.
        """
        return Style.parse(str(self._styles[name]))

    @classmethod
    def get_theme_table(cls) -> RichTable:
        """Get a table of all default styles.

        Returns:
            The styles table from ``default_styles.styles_table()``.
        """
        return styles_table()

theme property writable

The rich.theme.Theme built from this instance's styles.

__call__()

Return the theme, so the instance can be used as a theme factory.

Returns:

Type Description
Theme

The rich.theme.Theme for this instance.

Source code in src/rich_gradient/theme.py
def __call__(self) -> Theme:
    """Return the theme, so the instance can be used as a theme factory.

    Returns:
        The ``rich.theme.Theme`` for this instance.
    """
    return self.theme

__getitem__(name)

Look up a style by name, e.g. theme["warning"].

Parameters:

Name Type Description Default
name str

The style name.

required

Returns:

Type Description
Style

The parsed Style.

Raises:

Type Description
KeyError

If no style has that name.

Source code in src/rich_gradient/theme.py
def __getitem__(self, name: str) -> Style:
    """Look up a style by name, e.g. ``theme["warning"]``.

    Args:
        name: The style name.

    Returns:
        The parsed ``Style``.

    Raises:
        KeyError: If no style has that name.
    """
    return Style.parse(str(self._styles[name]))

__init__(styles=None)

Initialize the theme; see the class docstring for arguments.

Source code in src/rich_gradient/theme.py
def __init__(self, styles: dict[str, StyleType] | None = None) -> None:
    """Initialize the theme; see the class docstring for arguments."""
    if styles is None:
        styles = dict(DEFAULT_STYLES)
    super().__init__(styles=styles, inherit=True)
    self._theme: Theme = Theme(styles, inherit=True)
    self._styles: dict[str, StyleType] = styles

__repr__()

Return a developer-friendly representation.

Returns:

Type Description
str

GradientTheme({...styles...}).

Source code in src/rich_gradient/theme.py
def __repr__(self) -> str:
    """Return a developer-friendly representation.

    Returns:
        ``GradientTheme({...styles...})``.
    """
    return f"GradientTheme({self._styles!r})"

__rich__()

Render the theme as a table of its styles.

Returns:

Type Description
Table

The styles table from default_styles.styles_table().

Source code in src/rich_gradient/theme.py
def __rich__(self) -> RichTable:
    """Render the theme as a table of its styles.

    Returns:
        The styles table from ``default_styles.styles_table()``.
    """
    return styles_table()

get_theme_table() classmethod

Get a table of all default styles.

Returns:

Type Description
Table

The styles table from default_styles.styles_table().

Source code in src/rich_gradient/theme.py
@classmethod
def get_theme_table(cls) -> RichTable:
    """Get a table of all default styles.

    Returns:
        The styles table from ``default_styles.styles_table()``.
    """
    return styles_table()

Note: GRADIENT_TERMINAL_THEME is a terminal theme constant exported from

rich_gradient.theme for use with Console.save_svg. It is not included in

the auto-generated API table above but is part of the public API.