A syntax highlighting package that supports over 500 programming languages and text formats with extensive output format options
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Functions for working with color schemes and visual styles that define how different token types are rendered in formatted output.
Get a style class by name.
def get_style_by_name(name: str):
"""
Get style class by name.
Parameters:
- name: Style name (e.g., 'default', 'github', 'monokai', 'vim')
Returns:
Style class that can be used with formatters
Raises:
ClassNotFound: If no style with that name is found
"""Usage example:
from pygments.styles import get_style_by_name
from pygments.formatters import HtmlFormatter
# Get specific styles
default_style = get_style_by_name('default')
github_style = get_style_by_name('github')
monokai_style = get_style_by_name('monokai')
# Use with formatter
formatter = HtmlFormatter(style=github_style)
# Or by name
formatter = HtmlFormatter(style='github')List all available styles.
def get_all_styles() -> Iterator[str]:
"""
Return generator of all available style names.
Yields:
Style name strings for both built-in and plugin styles
"""Usage example:
from pygments.styles import get_all_styles
# List all available styles
print("Available styles:")
for style_name in get_all_styles():
print(f" {style_name}")
# Find dark themes
dark_styles = [name for name in get_all_styles()
if 'dark' in name.lower() or name in ['monokai', 'vim', 'native']]Pygments includes many built-in styles:
default - Default Pygments styleemacs - Emacs editor stylefriendly - Friendly light stylefriendly_grayscale - Grayscale friendly stylecolorful - Colorful light styleautumn - Autumn colorsmurphy - Murphy stylemanni - Manni stylematerial - Material design inspiredperldoc - Perl documentation stylepastie - Pastie web service styleborland - Borland IDE styletrac - Trac styleigor - Igor Pro stylevs - Visual Studio stylexcode - Xcode stylemonokai - Popular dark themevim - Vim editor dark themenative - Native terminal stylefruity - Fruity dark colorsgithub-dark - GitHub dark themedracula - Dracula themegruvbox-dark - Gruvbox dark variantnord - Nord color paletteonedark - One Dark themezenburn - Zenburn stylebw - Black and white (no colors)rrt - Radically Reduced Themesolarized-dark / solarized-light - Solarized color schemeparaiso-dark / paraiso-light - Paraiso themesrainbow_dash - Colorful rainbow themefrom pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
from pygments.styles import get_style_by_name
code = '''
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
'''
lexer = PythonLexer()
# Different styles produce different visual output
styles_to_try = ['default', 'github', 'monokai', 'vim', 'colorful']
for style_name in styles_to_try:
formatter = HtmlFormatter(style=style_name, cssclass=f'highlight-{style_name}')
result = highlight(code, lexer, formatter)
print(f"=== {style_name.upper()} STYLE ===")
print(result[:200] + "...")Each style defines colors and formatting for token types:
from pygments.styles import get_style_by_name
from pygments.token import Keyword, String, Comment
style = get_style_by_name('monokai')
# Access style definitions (internal API)
print(f"Keyword color: {style.style_for_token(Keyword)}")
print(f"String color: {style.style_for_token(String)}")
print(f"Comment color: {style.style_for_token(Comment)}")You can create custom styles by subclassing the Style class:
from pygments.style import Style
from pygments.token import *
class MyCustomStyle(Style):
name = 'mycustom'
styles = {
Comment: 'italic #75715e',
Keyword: 'bold #66d9ef',
Name: '#f8f8f2',
Name.Attribute: '#a6e22e',
Name.Class: 'bold #a6e22e',
Name.Function: '#a6e22e',
Number: '#ae81ff',
Operator: '#f92672',
String: '#e6db74',
Generic.Deleted: '#f92672',
Generic.Inserted: '#a6e22e',
}
# Use custom style
from pygments.formatters import HtmlFormatter
formatter = HtmlFormatter(style=MyCustomStyle)Style definitions use this format:
#ff0000), ANSI colors (ansiblue), or color namesbold, italic, underline, nobold, noitalic, noinheritbg:#ffffff for background colorsExamples:
'bold #ff0000' - Bold red text'italic #008000' - Italic green text'bg:#ffff00 #000000' - Black text on yellow background'underline #0000ff' - Underlined blue text'noinherit' - Don't inherit parent stylesInstall with Tessl CLI
npx tessl i tessl/pypi-pygments