Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ For an API reference and usage examples, [see our online documentation](https://

## IPython Theme

A minimal configuration:
Install `catppuccin[ipython]` in the same environment as IPython, then add
the following to your [IPython configuration](https://ipython.readthedocs.io/en/stable/config/intro.html):

```python
c.TerminalInteractiveShell.true_color = True
c.TerminalInteractiveShell.highlighting_style = "catppuccin-mocha"
c.TerminalInteractiveShell.colors = "catppuccin-mocha"
```

Putting this into your [IPython configuration](https://ipython.readthedocs.io/en/stable/config/intro.html)
and ensuring `catppuccin[pygments]` is installed in the same environment will
give you Catppuccin Mocha syntax highlighting in the REPL. See [here](https://github.com/backwardspy/dots/blob/f6991570d6691212e27e266517656192f910ccbf/dot_config/ipython/profile_default/ipython_config.py)
for an example of a more complete configuration.
You can also switch themes interactively with `%colors catppuccin-latte`.

Available IPython themes:

Expand All @@ -44,6 +42,17 @@ Available IPython themes:
- `catppuccin-macchiato`
- `catppuccin-mocha`

> [!IMPORTANT]
> This requires IPython 9.18 or newer.
>
> IPython versions 9.0-9.17 are in a weird place where it requires an extension for the theme to work.
> I've chosen to skip that in the hope you can upgrade to 9.18 and use the new theme system.
> If you absolutely require support for these pre-9.18 versions, then open an issue explaining
> your situation and I will reconsider adding the extension as an option.
>
> For IPython 8, install `catppuccin[pygments]` and use
> `c.TerminalInteractiveShell.highlighting_style = "catppuccin-mocha"` instead.

## Contribution

If you are looking to contribute, please read through our
Expand Down
60 changes: 60 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

DPI = 200

# must match the `ipython.themes` entry points in `pyproject.toml`.
IPYTHON_THEME_DIRECTORY = (
Path(__file__).parent / "catppuccin" / "extras" / "ipython_themes"
)


def load_palette_json() -> dict[str, Any]:
"""Load palette data from `./palette.json`."""
Expand Down Expand Up @@ -54,6 +59,49 @@ def make_flavor(identifier: str, fields: dict[str, Any]) -> Flavor:
)


def ipython_theme(flavor: Flavor) -> dict[str, Any]:
"""Create an IPython theme for a flavor.

Syntax highlighting comes from the matching pygments style via `base`.
`extra_style` covers the IPython tokens used for prompts, tracebacks, and
the debugger.
"""
c = flavor.colors
name = f"catppuccin-{flavor.identifier}"
return {
"name": name,
"base": name,
"extra_style": {
# prompts
"Prompt": c.green.hex,
"PromptNum": f"{c.green.hex} bold",
"OutPrompt": c.peach.hex,
"OutPromptNum": f"{c.peach.hex} bold",
# tracebacks
"Header": c.red.hex,
"Topline": c.red.hex,
"ExcName": f"{c.red.hex} bold",
"Filename": c.blue.hex,
"FilenameEm": f"{c.blue.hex} bold",
"Lineno": c.overlay1.hex,
"LinenoEm": f"{c.lavender.hex} bold",
"Line": c.text.hex,
"TB.Name": c.mauve.hex,
"TB.NameEm": f"{c.mauve.hex} bold",
"VName": c.teal.hex,
"ValEm": f"{c.peach.hex} bold",
"Normal": "",
"NormalEm": f"{c.text.hex} bold",
"Caret": "",
"TbHighlight": f"bg:{c.surface1.hex}",
# debugger
"Breakpoint": "",
"Breakpoint.Enabled": c.red.hex,
"Breakpoint.Disabled": c.overlay1.hex,
},
}


def codegen() -> str:
"""Generate contents of `catppuccin/palette.py`."""
palette_json = load_palette_json()
Expand Down Expand Up @@ -105,6 +153,18 @@ def codegen() -> str:
f.write(text)
print("matplotlib styles generation complete")

# Generate the IPython themes
print("generating ipython themes")
IPYTHON_THEME_DIRECTORY.mkdir(exist_ok=True)
for flavor in PALETTE:
theme = ipython_theme(flavor)
print(f"- {theme['name']}")
theme_path = IPYTHON_THEME_DIRECTORY / f"{theme['name']}.json"
with theme_path.open("w", newline="\n") as f:
json.dump(theme, f, indent=2)
f.write("\n")
print("ipython themes generation complete")

# Generate matplotlib assets for the docs
import matplotlib as mpl
import matplotlib.pyplot as plt
Expand Down
29 changes: 29 additions & 0 deletions catppuccin/extras/ipython_themes/catppuccin-frappe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "catppuccin-frappe",
"base": "catppuccin-frappe",
"extra_style": {
"Prompt": "#a6d189",
"PromptNum": "#a6d189 bold",
"OutPrompt": "#ef9f76",
"OutPromptNum": "#ef9f76 bold",
"Header": "#e78284",
"Topline": "#e78284",
"ExcName": "#e78284 bold",
"Filename": "#8caaee",
"FilenameEm": "#8caaee bold",
"Lineno": "#838ba7",
"LinenoEm": "#babbf1 bold",
"Line": "#c6d0f5",
"TB.Name": "#ca9ee6",
"TB.NameEm": "#ca9ee6 bold",
"VName": "#81c8be",
"ValEm": "#ef9f76 bold",
"Normal": "",
"NormalEm": "#c6d0f5 bold",
"Caret": "",
"TbHighlight": "bg:#51576d",
"Breakpoint": "",
"Breakpoint.Enabled": "#e78284",
"Breakpoint.Disabled": "#838ba7"
}
}
29 changes: 29 additions & 0 deletions catppuccin/extras/ipython_themes/catppuccin-latte.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "catppuccin-latte",
"base": "catppuccin-latte",
"extra_style": {
"Prompt": "#40a02b",
"PromptNum": "#40a02b bold",
"OutPrompt": "#fe640b",
"OutPromptNum": "#fe640b bold",
"Header": "#d20f39",
"Topline": "#d20f39",
"ExcName": "#d20f39 bold",
"Filename": "#1e66f5",
"FilenameEm": "#1e66f5 bold",
"Lineno": "#8c8fa1",
"LinenoEm": "#7287fd bold",
"Line": "#4c4f69",
"TB.Name": "#8839ef",
"TB.NameEm": "#8839ef bold",
"VName": "#179299",
"ValEm": "#fe640b bold",
"Normal": "",
"NormalEm": "#4c4f69 bold",
"Caret": "",
"TbHighlight": "bg:#bcc0cc",
"Breakpoint": "",
"Breakpoint.Enabled": "#d20f39",
"Breakpoint.Disabled": "#8c8fa1"
}
}
29 changes: 29 additions & 0 deletions catppuccin/extras/ipython_themes/catppuccin-macchiato.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "catppuccin-macchiato",
"base": "catppuccin-macchiato",
"extra_style": {
"Prompt": "#a6da95",
"PromptNum": "#a6da95 bold",
"OutPrompt": "#f5a97f",
"OutPromptNum": "#f5a97f bold",
"Header": "#ed8796",
"Topline": "#ed8796",
"ExcName": "#ed8796 bold",
"Filename": "#8aadf4",
"FilenameEm": "#8aadf4 bold",
"Lineno": "#8087a2",
"LinenoEm": "#b7bdf8 bold",
"Line": "#cad3f5",
"TB.Name": "#c6a0f6",
"TB.NameEm": "#c6a0f6 bold",
"VName": "#8bd5ca",
"ValEm": "#f5a97f bold",
"Normal": "",
"NormalEm": "#cad3f5 bold",
"Caret": "",
"TbHighlight": "bg:#494d64",
"Breakpoint": "",
"Breakpoint.Enabled": "#ed8796",
"Breakpoint.Disabled": "#8087a2"
}
}
29 changes: 29 additions & 0 deletions catppuccin/extras/ipython_themes/catppuccin-mocha.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "catppuccin-mocha",
"base": "catppuccin-mocha",
"extra_style": {
"Prompt": "#a6e3a1",
"PromptNum": "#a6e3a1 bold",
"OutPrompt": "#fab387",
"OutPromptNum": "#fab387 bold",
"Header": "#f38ba8",
"Topline": "#f38ba8",
"ExcName": "#f38ba8 bold",
"Filename": "#89b4fa",
"FilenameEm": "#89b4fa bold",
"Lineno": "#7f849c",
"LinenoEm": "#b4befe bold",
"Line": "#cdd6f4",
"TB.Name": "#cba6f7",
"TB.NameEm": "#cba6f7 bold",
"VName": "#94e2d5",
"ValEm": "#fab387 bold",
"Normal": "",
"NormalEm": "#cdd6f4 bold",
"Caret": "",
"TbHighlight": "bg:#45475a",
"Breakpoint": "",
"Breakpoint.Enabled": "#f38ba8",
"Breakpoint.Disabled": "#7f849c"
}
}
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ catppuccin-frappe = "catppuccin.extras.pygments:FrappeStyle"
catppuccin-macchiato = "catppuccin.extras.pygments:MacchiatoStyle"
catppuccin-mocha = "catppuccin.extras.pygments:MochaStyle"

# for ipython >=9.18
[project.entry-points."ipython.themes"]
catppuccin-latte = "catppuccin.extras:ipython_themes"
catppuccin-frappe = "catppuccin.extras:ipython_themes"
catppuccin-macchiato = "catppuccin.extras:ipython_themes"
catppuccin-mocha = "catppuccin.extras:ipython_themes"

[project.optional-dependencies]
matplotlib = ["matplotlib>=3.8.4"]
pygments = ["pygments>=2.17.2"]
rich = ["rich>=13.7.0"]
ipython = ["ipython>=9.18.0"]
gh-pages = ["tinycss2>=1.2.1", "pdoc>=15.0.1"]

[dependency-groups]
Expand Down
24 changes: 24 additions & 0 deletions tests/test_ipython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from IPython.utils.PyColorize import theme_table
from pygments.styles import get_style_by_name
from pygments.token import Keyword, Token

from catppuccin import PALETTE
from catppuccin.models import Flavor


@pytest.mark.parametrize("flavor", list(PALETTE), ids=lambda f: f.identifier)
def test_ipython_theme_is_registered(flavor: Flavor) -> None:
name = f"catppuccin-{flavor.identifier}"
assert name in theme_table

theme = theme_table[name]
assert theme.name == name
assert theme.base == name

styles = theme.as_pygments_style().styles
# syntax highlighting comes from the pygments style
assert styles[Keyword] == get_style_by_name(name).styles[Keyword]
# rest is ipython-specific
assert styles[Token.TbHighlight] == f"bg:{flavor.colors.surface1.hex}"
assert styles[Token.Prompt] == flavor.colors.green.hex
Loading
Loading