Registry / web-framework / accessible-pygments

accessible-pygments

JSON →
library0.0.5pypypi✓ verified 23d ago

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
INSTALL
IMPORT
SIG · ACCESSIBLE-PYGMENT
A
accessible-pygments
web-frameworkpythonv0.0.5
Install
2.3s avg
Import
19ms
Disk
27MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.0.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.018s · 28.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.3s · import 0.020s · 29MB
27MB installed
● package 27MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

logger
from a11y_pygments import logger
from accessible_pygments import logger
logging
from a11y_pygments import logging
from accessible_pygments import logging

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)
Debug
Known issues
breakingVersion 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.
affects: <0.0.3
gotchaWhen 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"`.
affects: All versions
gotchaPygments 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.
affects: All versions
Upgrade
Version history
0.0.5latest on PyPI · released May 10, 2024
Audit
Dependencies
PygmentsrequiredProvides the core syntax highlighting engine that accessible-pygments extends with new styles.
Agent activity
47 hits · last 30 days
node
38
OpenAI (training)
1
Resources
accessible-pygments — pip install accessible-pygments · libregistry