Skip to content

mtwomey/table-formatter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table Formatter

A Python library for creating beautifully formatted terminal tables with Unicode borders, automatic text wrapping, and 30 predefined color schemes optimized for dark terminal backgrounds.

Features

✨ Beautiful Unicode Tables - Clean box-drawing characters for professional-looking output
🎨 30 Color Schemes - Predefined themes from subtle to vibrant, all optimized for dark terminals
📏 Automatic Text Wrapping - Intelligent column width management and text wrapping
đź”§ Flexible Sizing - Independent or unified column sizing across multiple tables
🎯 Simple API - Easy-to-use functions for single tables, multiple tables, or auto-detection
⚡ High Performance - Efficient rendering with automatic terminal width detection
đź”™ Backward Compatible - Direct color overrides still supported alongside named schemes

Installation

pip install table-formatter

Quick Start

Simple Table

from table_formatter import format_table

data = [
    ["Alice", "25", "Engineer"],
    ["Bob", "30", "Designer"], 
    ["Carol", "27", "Manager"]
]

print(format_table(data, 
                   title="Team Members",
                   header=["Name", "Age", "Role"]))

Table with Color Scheme

from table_formatter import format_tables

tables = [{
    'title': 'Project Status',
    'header': ['Task', 'Status', 'Assignee'],
    'data': [
        ['Database Design', 'Complete', 'Alice'],
        ['API Development', 'In Progress', 'Bob'],
        ['Frontend UI', 'Planned', 'Carol']
    ],
    'color_scheme': 'matrix_green'  # Use predefined color scheme
}]

print(format_tables(tables))

Multiple Tables with Unified Sizing

from table_formatter import format_tables

tables = [
    {
        'title': 'Q1 Sales',
        'header': ['Product', 'Revenue'],
        'data': [['Widget A', '$1,200'], ['Widget B', '$800']],
        'color_scheme': 'blue_theme'
    },
    {
        'title': 'Q2 Sales', 
        'header': ['Product', 'Revenue'],
        'data': [['Widget A', '$1,500'], ['Widget B', '$950']],
        'color_scheme': 'green_theme'
    }
]

print(format_tables(tables, unified_sizing=True))

Color Schemes

The library includes 30 professionally designed color schemes organized into categories:

Core Collection (1-10)

  • subtle_alternating - Light cyan/gray distinction
  • blue_theme - Professional blue tones
  • green_theme - Nature-inspired greens
  • high_contrast - Bold white/yellow alternation
  • warm_tones - Orange/peach alternation
  • purple_theme - Elegant purples
  • monochrome_elegant - Sophisticated grays
  • minimal_contrast - Very subtle distinction
  • teal_cyan_theme - Cool water tones
  • clean_and_bright - Vibrant but readable

High-Impact Themes (11-15)

  • matrix_green - Retro terminal feel
  • sunset - Warm gradient feel
  • ocean_depths - Deep blue theme
  • pastel_dream - Soft and gentle
  • neon_cyberpunk - Electric bright

Natural Themes (16-20)

  • earth_tones - Natural and grounding
  • ice_blue - Cool and crisp
  • amber_terminal - Retro monochrome
  • forest - Deep woodland greens
  • royal_purple - Majestic and rich

Dramatic Themes (21-25)

  • fire_and_ice - Dramatic contrast
  • vintage_terminal - Classic green on black
  • rose_gold - Elegant warm metallics
  • midnight_blue - Deep night sky
  • copper - Warm metallic tones

Specialty Themes (26-30)

  • lavender_fields - Soft purple theme
  • terminal_hacker - Classic hacker aesthetic
  • coral_reef - Vibrant ocean colors
  • silver - Metallic monochrome
  • rainbow - Playful multicolor

Advanced Usage

Color Scheme Discovery

from table_formatter import list_color_schemes, get_color_scheme

# List all available schemes
schemes = list_color_schemes()
for scheme in schemes:
    print(f"{scheme['scheme_key']}: {scheme['name']} - {scheme['description']}")

# Get specific scheme details
matrix_colors = get_color_scheme('matrix_green')
print(matrix_colors)
# Output: {'even_row_fg': 46, 'odd_row_fg': 40, 'line_fg': 34, 'title_fg': 82, 'header_fg': 118}

Background Colors

In addition to foreground colors, the table formatter allows you to set background colors for each element. These can be specified in both predefined color schemes and manual overrides.

Example of Setting Background Colors

from table_formatter import format_tables

tables = [{
    'title': 'Background Colors',
    'header': ['Item', 'Value'],
    'data': [['Row1', 'Value1'], ['Row2', 'Value2']],
    'color_scheme': 'matrix_green',
    'colors': {
        'even_row_bg': 234,  # Dark gray background for even rows
        'odd_row_bg': 232,   # Black background for odd rows
        'header_bg': 240,    # Gray background for header
        'title_bg': 232      # Black background for title
    }
}]

print(format_tables(tables))

Mixing Color Schemes with Overrides

tables = [{
    'title': 'Custom Colors',
    'header': ['Item', 'Value'],
    'data': [['Alpha', '100'], ['Beta', '200']],
    'color_scheme': 'blue_theme',  # Base scheme
    'colors': {
        'header_fg': 226,  # Override header to bright yellow
        'odd_row_fg': 196,  # Override odd rows to bright red
        # Other colors will use blue_theme values
    }
}]

Text Wrapping

long_data = [
    ["Product", "This is a very long description that will be automatically wrapped"],
    ["Service", "Short description"]
]

print(format_table(long_data, 
                   title="Product Catalog",
                   header=["Type", "Description"],
                   max_width=50))  # Force wrapping at 50 characters

API Reference

Main Functions

format_table(data, title=None, header=None, max_width=None)

Format a single table with Unicode box-drawing characters.

Parameters:

  • data (List[List[str]]): Table rows
  • title (str, optional): Title to display above the table
  • header (List[str], optional): Header column names
  • max_width (int, optional): Maximum table width (defaults to terminal width)

Returns: Formatted table as string

format_tables(tables, unified_sizing=False, table_spacing=2, max_width=None)

Format multiple tables with Unicode box-drawing characters.

Parameters:

  • tables (List[Dict]): List of table dictionaries
  • unified_sizing (bool): Use same column widths across all tables
  • table_spacing (int): Number of blank lines between tables
  • max_width (int, optional): Maximum table width

Table Dictionary Format:

{
    'data': List[List[str]],           # Required: table rows
    'title': str,                      # Optional: table title
    'header': List[str],               # Optional: header columns
    'color_scheme': str,               # Optional: predefined color scheme name
    'colors': Dict[str, int],          # Optional: manual color overrides
}

Returns: Formatted tables as string

format_data_as_table(data, ...)

Auto-detect single vs multiple tables and format accordingly.

Color Functions

list_color_schemes()

List all available color schemes with names and descriptions.

Returns: List of dictionaries with scheme info

get_color_scheme(scheme_name)

Get color values for a specific scheme.

Parameters:

  • scheme_name (str): Name of the color scheme

Returns: Dictionary of color values or None if not found

load_color_schemes(color_schemes_file=None)

Load color schemes from YAML file (with caching).

Parameters:

  • color_schemes_file (str, optional): Path to YAML file

Returns: Dictionary of all color schemes

Color Customization

Each color scheme defines these properties:

Foreground Colors

  • even_row_fg - Foreground color for even rows (0, 2, 4, ...)
  • odd_row_fg - Foreground color for odd rows (1, 3, 5, ...)
  • line_fg - Foreground color for borders and separators
  • title_fg - Foreground color for table titles
  • header_fg - Foreground color for header text

Background Colors

  • even_row_bg - Background color for even rows (0, 2, 4, ...)
  • odd_row_bg - Background color for odd rows (1, 3, 5, ...)
  • line_bg - Background color for borders and separators
  • title_bg - Background color for table titles
  • header_bg - Background color for header text

Colors use ANSI 256-color codes (0-255) or null for default terminal colors.

Requirements

  • Python 3.8+
  • PyYAML 6.0+

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Changelog

v1.0.0

  • Initial release
  • 30 predefined color schemes
  • Unicode table formatting
  • Automatic text wrapping
  • Multiple table support with unified sizing
  • Color scheme system with YAML configuration

About

A library for printing nice tables of data in Python

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages