Accessible Pygments Styles

raw JSON →
0.0.5 verified Fri Apr 24 auth: no python

A collection of accessible Pygments styles, currently at version 0.0.5. It provides themes that meet WCAG 2.1 color contrast requirements, aiming to make syntax highlighting more inclusive for users with low vision or color blindness. The library is actively maintained with regular minor releases adding new themes and improvements.

pip install accessible-pygments
error ModuleNotFoundError: No module named 'accessible_pygments'
cause The 'accessible-pygments' package is not installed or is installed incorrectly.
fix
Ensure the package is installed by running 'pip install accessible-pygments'.
error ImportError: cannot import name 'a11y_dark' from 'accessible_pygments'
cause The theme 'a11y_dark' is not available in the 'accessible_pygments' package.
fix
Verify the correct theme name and import it accordingly, e.g., 'from accessible_pygments.styles import a11y_dark'.
error TypeError: 'NoneType' object is not callable
cause Attempting to use a theme that is not properly registered or is missing.
fix
Ensure the theme is correctly installed and registered in Pygments by checking the 'accessible_pygments' documentation.
error pygments.util.ClassNotFound: No style found for name 'non_existent_style'
cause You have specified a Pygments style name (e.g., 'non_existent_style') that does not exist or is not correctly registered within Pygments, or you have misspelled a valid `accessible-pygments` theme name.
fix
Ensure you are using a correct style name provided by accessible-pygments, such as 'a11y-light' or 'github-dark', in your Pygments HtmlFormatter instantiation or Sphinx conf.py setting. Refer to the accessible-pygments documentation for a list of available themes.
error error: 'Python.h' file not found
cause This compilation error occurs during installation (often when installing a dependency with C extensions) because the Python development headers are missing on your system.
fix
On Debian/Ubuntu, install python3-dev (or python-dev for Python 2). On RedHat/CentOS, install python3-devel (or python-devel). On macOS, ensure Xcode command line tools are installed (xcode-select --install).
breaking Version 0.0.3 included critical fixes to the highlighting colors across all themes. Users upgrading from versions prior to 0.0.3 should expect visual changes in their highlighted code, as older versions may have had incorrect or less accessible color contrasts.
fix Upgrade to version 0.0.3 or newer to benefit from WCAG 2.1 compliant color contrast. Review your applications to ensure the new color schemes meet your visual and accessibility requirements.
gotcha When using `accessible-pygments` styles in Sphinx documentation, the style is specified as a string in `conf.py` (e.g., `pygments_style = "a11y-light"`). Do not attempt to import a Python class for the style directly into `conf.py`. The `accessible-pygments` package must be installed in the Sphinx environment.
fix Ensure `accessible-pygments` is in your documentation's dependencies. In your `conf.py`, set `pygments_style = "your-accessible-style-name"`.
gotcha Pygments styles are referenced by their string names (e.g., `'a11y-light'`) in `HtmlFormatter(style='...')`. Do not attempt to directly import and pass a style class from `accessible_pygments` to the formatter (e.g., `HtmlFormatter(style=A11yLight)`). The package automatically registers these names upon import.
fix Always use the string name of the desired accessible style when initializing Pygments formatters.
runtime status import time mem disk
3.10-alpine
3.10-slim
3.11-alpine
3.11-slim
3.12-alpine
3.12-slim
3.13-alpine
3.13-slim
3.9-alpine
3.9-slim

This example demonstrates how to highlight Python code using the `a11y-light` style from `accessible-pygments`. The key is to import `accessible_pygments` to register its styles, then refer to the style by its string name when creating a Pygments formatter. The `full=True` option generates a complete HTML document including the stylesheet.

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
import accessible_pygments # This registers the styles

code_to_highlight = """
def greet(name):
    print(f"Hello, {name}!")

greet("World")
"""

# Choose an accessible style, e.g., 'a11y-light', 'github-dark-colorblind'
style_name = 'a11y-light'

# Create an HTML formatter with the chosen accessible style
formatter = HtmlFormatter(style=style_name, full=True, encoding='utf-8')

# Highlight the code
highlighted_code = highlight(
    code_to_highlight,
    PythonLexer(),
    formatter
)

# The highlighted_code is now a bytes object (due to encoding='utf-8' in full=True mode)
# Decode to string if you want to print or save to a text file
print(highlighted_code.decode('utf-8'))

# To save to an HTML file (example)
# with open("highlighted_code.html", "wb") as f:
#     f.write(highlighted_code)