or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

core-table.mdfactory-functions.mdindex.mdoutput-formats.mdstyling.md
tile.json

tessl/pypi-prettytable

A simple Python library for easily displaying tabular data in a visually appealing ASCII table format

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/prettytable@3.16.x

To install, run

npx @tessl/cli install tessl/pypi-prettytable@3.16.0

index.mddocs/

PrettyTable

A comprehensive Python library for creating and formatting ASCII tables. PrettyTable provides extensive table creation, data manipulation, styling options, and export capabilities, making it ideal for displaying tabular data in terminal applications, reports, and documentation.

Package Information

  • Package Name: prettytable
  • Language: Python
  • Installation: pip install prettytable

Core Imports

from prettytable import PrettyTable

For color support:

from prettytable.colortable import ColorTable, Themes, Theme, RESET_CODE

For factory functions:

from prettytable import from_csv, from_json, from_html, from_db_cursor

For styling enums:

from prettytable import HRuleStyle, VRuleStyle, TableStyle

Basic Usage

from prettytable import PrettyTable

# Create a new table
table = PrettyTable()

# Set field names (column headers)
table.field_names = ["City", "Area", "Population", "Annual Rainfall"]

# Add rows
table.add_row(["Adelaide", 1295, 1158259, 600.5])
table.add_row(["Brisbane", 5905, 2074003, 1146.4])
table.add_row(["Darwin", 112, 120900, 1714.7])
table.add_row(["Hobart", 1357, 205556, 619.5])

# Display the table
print(table)

# Customize alignment
table.align["City"] = "l"  # Left align
table.align["Population"] = "r"  # Right align

# Set table style
table.set_style(TableStyle.MARKDOWN)
print(table)

Architecture

PrettyTable follows a straightforward object-oriented design:

  • PrettyTable: Core table class handling data storage, formatting, and output generation
  • ColorTable: Extended table class adding ANSI color theming support
  • Factory Functions: Utilities for creating tables from various data sources (CSV, JSON, HTML, databases)
  • Styling Enums: Type-safe styling options for borders, rules, and table styles
  • Theme System: Color theming infrastructure for enhanced visual presentation

The library supports multiple output formats (ASCII, HTML, CSV, JSON, LaTeX, MediaWiki) and provides extensive customization options for alignment, borders, spacing, and visual styling.

Capabilities

Core Table Operations

Primary table functionality including creation, data manipulation, field management, and basic formatting options. Handles row and column operations, copying, clearing, and fundamental table structure management.

class PrettyTable:
    def __init__(self, field_names: list[str] | None = None, **kwargs): ...
    def add_row(self, row: list[Any], *, divider: bool = False) -> None: ...
    def add_rows(self, rows: list[list[Any]], *, divider: bool = False) -> None: ...
    def add_column(self, fieldname: str, column: list[Any], align: str = 'c', valign: str = 't') -> None: ...
    def del_row(self, row_index: int) -> None: ...
    def del_column(self, fieldname: str) -> None: ...
    def clear_rows(self) -> None: ...
    def clear(self) -> None: ...
    def copy(self) -> 'PrettyTable': ...

Core Table Operations

Output Formats

Multiple export formats for different use cases including ASCII text, HTML, CSV, JSON, LaTeX, and MediaWiki markup. Supports customizable formatting options, pagination, and integration with various output systems.

def get_string(self, **kwargs) -> str: ...
def get_html_string(self, **kwargs) -> str: ...
def get_csv_string(self, **kwargs) -> str: ...
def get_json_string(self, **kwargs) -> str: ...
def get_latex_string(self, **kwargs) -> str: ...
def get_mediawiki_string(self, **kwargs) -> str: ...
def paginate(self, page_length: int = 58, line_break: str = '\f', **kwargs) -> str: ...

Output Formats

Styling and Theming

Comprehensive styling system including border styles, alignment options, color theming, and predefined table styles. Supports both ASCII and colored output with extensive customization capabilities.

class HRuleStyle(IntEnum):
    FRAME: int
    ALL: int
    NONE: int
    HEADER: int

class VRuleStyle(IntEnum):
    FRAME: int
    ALL: int
    NONE: int

class TableStyle(IntEnum):
    DEFAULT: int
    MARKDOWN: int
    ORGMODE: int
    DOUBLE_BORDER: int
    # ... additional styles

Styling and Theming

Factory Functions

Utilities for creating tables from various data sources including CSV files, JSON data, HTML tables, database cursors, and MediaWiki markup. Simplifies table creation from existing structured data.

def from_csv(fp, field_names: list[str] | None = None, **kwargs) -> PrettyTable: ...
def from_json(json_string: str, **kwargs) -> PrettyTable: ...
def from_html(html_code: str, **kwargs) -> list[PrettyTable]: ...
def from_html_one(html_code: str, **kwargs) -> PrettyTable: ...
def from_db_cursor(cursor, **kwargs) -> PrettyTable | None: ...
def from_mediawiki(wiki_text: str, **kwargs) -> PrettyTable: ...

Factory Functions

Types

from typing import Any, Literal
from collections.abc import Sequence

RowType = list[Any]
AlignType = Literal["l", "c", "r"]
VAlignType = Literal["t", "m", "b"] 
HeaderStyleType = Literal["cap", "title", "upper", "lower", None]

Deprecated Constants

For backward compatibility, the following constants are still available but deprecated. Use the corresponding enum values instead:

# Deprecated constants (use HRuleStyle/VRuleStyle/TableStyle enums instead)
ALL: int           # Use HRuleStyle.ALL or VRuleStyle.ALL
DEFAULT: int       # Use TableStyle.DEFAULT  
DOUBLE_BORDER: int # Use TableStyle.DOUBLE_BORDER
FRAME: int         # Use HRuleStyle.FRAME or VRuleStyle.FRAME
HEADER: int        # Use HRuleStyle.HEADER
MARKDOWN: int      # Use TableStyle.MARKDOWN
MSWORD_FRIENDLY: int   # Use TableStyle.MSWORD_FRIENDLY
NONE: int          # Use HRuleStyle.NONE or VRuleStyle.NONE
ORGMODE: int       # Use TableStyle.ORGMODE
PLAIN_COLUMNS: int # Use TableStyle.PLAIN_COLUMNS
RANDOM: int        # Use TableStyle.RANDOM
SINGLE_BORDER: int # Use TableStyle.SINGLE_BORDER

Note: These constants trigger deprecation warnings when accessed.