texttable Documentation

repository·master·Indexed 18 days ago

https://github.com/foutaise/texttable

A Python module for creating formatted ASCII tables for terminal output or text files. It provides the Texttable class to manage column alignment, vertical alignment, data types, and table decorations. Features include support for CJK text wrapping via cjkwrap, emoji rendering via wcwidth, and customizable borders and widths.

Tokens
1.4K
Snippets
8
Records
8
Agent score
13%

What's inside texttable

  1. Install texttable

    master

    You can install texttable via PyPI using pip. It is also available in various Linux/Unix package managers like Debian, FreeBSD, Fedora, and Suse.

    pip install texttable
  2. Create a simple ASCII table with Texttable

    master

    To create a table, instantiate the Texttable class, configure its alignment and decoration, add rows using add_rows(), and then call draw() to get the table as a string.

    By default, Texttable uses textwrap for wrapping. If the cjkwrap library is installed, it will be used instead for better CJK text wrapping. If wcwidth is installed, it will be used for better emoji rendering.

    from texttable import Texttable
    
    table = Texttable()
    table.set_cols_align(["l", "r", "c"])
    table.set_cols_valign(["t", "m", "b"])
    table.add_rows([["Name", "Age", "Nickname"],
                    ["Mr\nXavier\nHuon", 32, "Xav'"],
                    ["Mr\nBaptiste\nClement", 1, "Baby"],
                    ["Mme\nLouise\nBourgeau", 28, "Lou\n\nLoue"]])
    print(table.draw())
  3. Configure table decoration and borders

    master

    Use set_deco(deco) to control which lines are drawn. deco is a bitwise combination of the following constants:

    • Texttable.BORDER: Border around the table
    • Texttable.HEADER: Horizontal line below the header
    • Texttable.HLINES: Horizontal lines between rows
    • Texttable.VLINES: Vertical lines between columns

    All are enabled by default. You can also customize the characters used for drawing with set_chars(array), which expects an array of 4 fields: [horizontal, vertical, corner, header] (default: ['-', '|', '+', '=']).

    table.set_deco(Texttable.BORDER | Texttable.HEADER)
  4. Configure column alignment and vertical alignment

    master

    Use set_cols_align(array) to set horizontal alignment for each column. Use set_cols_valign(array) to set vertical alignment.

    Horizontal Alignment (set_cols_align) options:

    • "l": column flushed left
    • "c": column centered
    • "r": column flushed right

    Vertical Alignment (set_cols_valign) options:

    • "t": column aligned on the top of the cell
    • "m": column aligned on the middle of the cell
    • "b": column aligned on the bottom of the cell
    table.set_cols_align(["l", "c", "r"])
    table.set_cols_valign(["t", "m", "b"])
  5. Manage table width and column widths

    master

    Control the dimensions of your table using these methods:

    • set_max_width(max_width): Sets the maximum width of the entire table. If set to 0, size is unlimited and cells won't be wrapped.
    • set_cols_width(array): Sets the specific width for each column using an array of integers (e.g., [10, 20, 5]).
    table.set_max_width(80)
    table.set_cols_width([10, 20, 5])
  6. Add rows and headers

    master

    Use the following methods to populate the table:

    • add_row(array): Adds a single row to the stack. Cells can contain newlines and tabs.
    • add_rows(rows, header=True): Adds multiple rows. rows can be an iterator of arrays or a 2D array. The header flag (default True) determines if the first row is treated as the header.
    • header(array): Explicitly specifies the header of the table.
    table.add_rows([["Col1", "Col2"], ["Val1", "Val2"]], header=True)
  7. Set column data types and precision

    master

    Use set_cols_dtype(array) to specify how data in each column should be formatted. Use set_precision(width) to set the decimal precision for float/exponential formats (default is 3).

    Data Type (set_cols_dtype) options:

    • "a": automatic (tries to use the most appropriate type)
    • "t": treat as text
    • "f": treat as float in decimal format
    • "e": treat as float in exponential format
    • "i": treat as integer
    • "b": treat as boolean
    • callable: a function that returns a formatted string for any given value
    table.set_cols_dtype(['t', 'f', 'e', 'i', 'a'])
    table.set_precision(2)
  8. Texttable class reference

    master

    The Texttable class is the primary interface for creating ASCII tables.

    Constructor:

    • __init__(self, max_width=80): Initializes the table. max_width is an integer specifying the maximum width; 0 means unlimited width (no wrapping).

    Methods:

    • add_row(array): Add a row.
    • add_rows(rows, header=True): Add multiple rows.
    • draw(): Returns the table as a single string.
    • header(array): Set the header.
    • reset(): Resets rows and header.
    • set_chars(array): Set drawing characters [horizontal, vertical, corner, header].
    • set_cols_align(array): Set column alignment (l, c, r).
    • set_cols_dtype(array): Set column data types (a, t, f, e, i, b, or callable).
    • set_cols_valign(array): Set vertical alignment (t, m, b).
    • set_cols_width(array): Set column widths.
    • set_deco(deco): Set decoration using bitwise flags.
    • set_header_align(array): Set header alignment (l, c, r).
    • set_max_width(max_width): Set max table width.
    • set_precision(width): Set float precision.

    Constants:

    • BORDER = 1
    • HEADER = 2
    • HLINES = 4
    • VLINES = 8
    from texttable import Texttable
    
    table = Texttable(max_width=100)