Skip to main content

Builds and Displays Customizable Tables

Project description

📌 PyTabFy

PyTabFy is a module in development that allows users to build customizable tables. Its primary goal is to transform raw data into visually appealing and organized tables. The module was built to take into account ASCII and Non-ASCII characters (for example CJK characters, which have larger width than normal characters).

🖼 Gallery:

T1
T2

📦 Installation

You can install PyTabFy using:

pip install pytabfy

🔨 Build

If you prefer to build PyTabFy manually, follow these steps:

# Clone this repository:
git clone https://github.com/FabricioDosSantosMoreira/PyTabFy.git

# Change directory:
cd PyTabFy

# Normal install 
python -m pip install .
make install

# Editable install
python -m pip install -e .
make install-editable

🚀 Getting Started

Examples:

You can start by running the examples of PytabFy:

Option 1 - Running examples.py directly

python -m PyTabFy.Examples.examples   

Option 2 - Importing specific examples

from PyTabFy.Examples import (
    left_aligned_table_example, 
    right_aligned_table_example, 
    center_aligned_table_example, 
    multi_lines_table_example,
    log_table_example,
)
from PyTabFy.Dummy import get_not_only_ascii_movie_data


multi_lines_table_example()
multi_lines_table_example(get_not_only_ascii_movie_data)

Building Your Own Table

To create a custom table, follow this example:

# Importing necessary modules
from pathlib import Path
import sys

from PyTabFy.PyTUtils import get_terminal_width
from PyTabFy.PyTConfigs import TableConfigs
from PyTabFy.PyTCore.Types import TableData
from PyTabFy.PyTCore.Tables import (
    DefaultTable,
)
from PyTabFy.PyTEnums import (
    Alignment, 
    TableSymbols, 
    TableFitMode, 
    StringBreakMode,
    StringFunctions, 
    StringSlicingMode, 
    StringLengthValidation, 
)

# Defining a custom configuration class
class CustomConfigs(TableConfigs):
    
    # NOTE: The __init__() method and calling super().__init__() are required!
    def __init__(self) -> None:
        super().__init__()

        # NOTES:
        # - You don't need to manually set each configuration, as PyTabFy provides defaults.
        # - Some configurations can influence the behavior of others.
        # - For all configuration variables that are lists:
        #     - They cannot be empty; at least one value must be provided.
        #     - Each list index corresponds to the respective table section index.
        #     - If the table section index exceeds the list length, the last value is used.

        # Defining the fitting mode for the table
        self.table_fit_mode = TableFitMode.DYNAMIC_TABLE_FIT

        # Defining the min and max table size
        # NOTE: Only used if TableFitMode.MAX_TABLE_FIT or MIN_TABLE_FIT are enable
        self.min_table_size = get_terminal_width(multiplier=0.75) # Minimum table width (75% of terminal width)
        self.max_table_size = get_terminal_width(multiplier=0.95) # Maximum table width (95% of terminal width)

        # Defining the symbols for the table
        self.table_symbols  = TableSymbols.DEFAULT_MIXED

        # Defining how strings will be validated
        # NOTE: Use WCSWIDTH if your data contains non-ASCII characters
        self.string_lenght_validation = StringLengthValidation.WCSWIDTH  

        # Defining the maximum string length for different sections
        self.max_title_string_lenght    = sys.maxsize
        self.max_header_strings_lenght  = [50, ] # Each list item corresponds to a header index
        self.max_content_strings_lenght = [30, ] # Each list item corresponds to a content index

        # Defining how the data is sliced (dependent on max string length)
        self.string_slicing_mode = StringSlicingMode.STRING_END
        self.string_break_mode   = StringBreakMode.BREAK_DYNAMIC

        # Defining delimiters for truncated data
        self.title_delimiter     = '...'
        self.headers_delimiters  = ['...', ] # Each list item corresponds to a header index
        self.contents_delimiters = ['...', ] # Each list item corresponds to a content index
        
        # Defining string replacement for empty data values
        self.headers_null_str_replacement = 'NULL'
        self.contents_null_str_replacement = 'NULL'

        # Defining the alignment settings for each section of the table
        self.title_alignment    = Alignment.CENTER
        self.header_alignments  = [Alignment.CENTER, ] # Each list item corresponds to a header index
        self.content_alignments = [Alignment.LEFT, ]   # Each list item corresponds to a content index

        # Defining functions applied to strings in each section
        self.title_string_function = StringFunctions.STR_KEEP_AS_IS
        self.header_strings_function = [StringFunctions.STR_UPPER, ] # Each list item corresponds to a header index
        self.content_strings_function = [StringFunctions.STR_LOWER, ] # Each list item corresponds to a content index

        # Defining padding settings for the table
        self.title_left_padding     = 1
        self.title_right_padding    = 1
        self.header_left_paddings   = [1, ] # Each list item corresponds to a header index
        self.header_right_paddings  = [1, ] # Each list item corresponds to a header index
        self.content_left_paddings  = [1, ] # Each list item corresponds to a content index
        self.content_right_paddings = [1, ] # Each list item corresponds to a content index

        # Defining empty borders for better table visualization
        self.upper_title_empty_border_size = 2
        self.lower_title_empty_border_size = 2
        self.upper_header_empty_border_size = 1
        self.lower_header_empty_border_size = 1
        self.upper_content_empty_border_size = 0 
        self.lower_content_empty_border_size = 0 

        # Ensures border character alternation is respected in some TableSymbols
        self.force_alternating_chars_respect = True 

        # Displays the table even if it's broken
        self.force_display = True 

        # Outer margin for the table
        self.margin = 1

        # Enables multiline tables, affecting several configurations
        self.enable_multiline = True


# Defining the data
title = 'PyTabFy README.md Table Example'
headers = ["Header 0", "Header 1", "Header 2"]
contents = [
    ["Content [0][0]", "Content [1][0]", "Content [2][0]"], 
    ["Content [0][1]", "Content [1][1]", "Content [2][1]"], 
    ["Content [0][2]", "Content [1][2]", "Content [2][2]"],
]

data = TableData().set_data_from_list(title=title, headers=headers, contents=contents)

# Defining the table and building it
custom_configs = CustomConfigs()
table = DefaultTable(custom_configs=custom_configs) # NOTE: Using a custom_configs is optional 
table.build(data)
    
# Using different methods on the table
table.display(border_between_content=False)
print('\n\n')

# NOTE: The display_and_select() method prompts the user to select an content starting from 1
selection = table.display_and_select(index=0, border_between_content=True)
print(f"You selected: {selection}")
print('\n\n')

selection = table.display_and_select(select_full_content=True, border_between_content=False)
print(f"You selected: {selection}")
print('\n\n')

table.log_table(file_path=Path().home() / 'table.txt', border_between_content=True)
print('\n\n')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytabfy-1.0.1.tar.gz (35.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytabfy-1.0.1-py3-none-any.whl (43.7 kB view details)

Uploaded Python 3

File details

Details for the file pytabfy-1.0.1.tar.gz.

File metadata

  • Download URL: pytabfy-1.0.1.tar.gz
  • Upload date:
  • Size: 35.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytabfy-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ecfa2c1c2926fb80b2b77c4c90261b3cd855da3a07686a3f884954952c3dfff5
MD5 c39b95e7deb4c114e9d1ec24c534b740
BLAKE2b-256 1dea86c0053d6e7c61c5b0133e96a2a18f480b2aae711d114a7b9af4649e6d9c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytabfy-1.0.1.tar.gz:

Publisher: publish_to_pypi.yml on FabricioDosSantosMoreira/PyTabFy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytabfy-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pytabfy-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 43.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytabfy-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 669619761c7e0bf9bb147db4aa2db8c900e082877453f089bfdf9c79acdfe62f
MD5 5a2f87de23d890eaa172efb21dd0e280
BLAKE2b-256 f6192aefc973a85d72d7322760c94858428e0924f11a032f90e60c7a749379e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytabfy-1.0.1-py3-none-any.whl:

Publisher: publish_to_pypi.yml on FabricioDosSantosMoreira/PyTabFy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page