Skip to main content

Simple table tool for Python

Project description

jdutable

Command line table data visualisation tool.

Installation

> pip(3) install (-U) jdutable

and

from jdutable import TableWriter

Instantiation

table = TableWriter()

Methods

Add data

def set_header(self, header: list[str])

... to add a list of strings as the header of the table.

def set_footer(self, footer: list[str])

... to add a list of strings as the footer of the table.

def append(self, line:list[str])

... to add a line of strings to the content of the table.

def append_bulk(self, data: list[list[str]])

... to add multiple lines to the content of the table.

def from_csv(self, filename: Path, header: bool = False, footer: bool = False, sep: str = ",")

... to import a csv file. The parameter are:

  • filename: path to the csv file to import
  • header: boolean to tell if csv file contains a header
  • footer: boolean to tell if csv file contains a footer
  • sep: string defining what the csv delimiter is

Parameters

def set_uppercased(self, state: bool)

... to print the header and footer with uppercased letters.

def set_border(self, state: bool)

... to show a border around the table.

def set_alignment(self, alignment: str or list[str])

... to define alignment guide for each column. Alignement can be a single string defining alignment for all columns, or a list of strings specifying the alignment guide for each column. To align to the:

  • left: either "<", "left" or "l"
  • center: either "^", "center" or "c"
  • right: either ">", "right" or "r"
def set_center_separator(self, separator: str)

... to change the table lines intersection character.

def set_column_separator(self, separator: str)

... to change the table vertical lines character.

Display

def render(self, file=sys.stdout)

... to render the table using previously specified content, header/footer and parameters. file parameter can be add to write the table to a file, or a different standard output.

Reset

def reset_header(self)

... to reset the header to an empty list.

def reset_body(self)

... to reset the body to an empty list.

def reset_footer(self)

... to reset the footer to an empty list.

def reset_content(self)

... to reset header, body, and footer to an empty list.

Example

Let's use the following content, that is in file assets/example.csv:

Date, Description, Amout
1/1/2014, Domain name, $10.98
1/1/2014, January Hosting, $54.95
1/4/2014, February Hosting, $51.00
1/4/2014, February Extra Bandwidth, $30.00
, Total, $146.93

Heres an example code to render this file:

from pathlib import Path

from jdutable import TableWriter

table = TableWriter()

table.from_csv(Path("assets/example.csv"), header=True, footer=True)

table.set_uppercased(False)
table.set_border(True)
table.set_alignment(["left", "<", "r"])
table.set_center_separator("-")
table.set_column_separator("|")

table.render()

This shows the following result in the command line:

---------------------------------------------------
! Date     ! Description               !    Amout !
---------------------------------------------------
! 1/1/2014 !  Domain name              !   $10.98 !
! 1/1/2014 !  January Hosting          !   $54.95 !
! 1/4/2014 !  February Hosting         !   $51.00 !
! 1/4/2014 !  February Extra Bandwidth !   $30.00 !
---------------------------------------------------
              Total                       $146.93
           ----------------------------------------

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

jdutable-0.4.tar.gz (6.1 kB view details)

Uploaded Source

File details

Details for the file jdutable-0.4.tar.gz.

File metadata

  • Download URL: jdutable-0.4.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for jdutable-0.4.tar.gz
Algorithm Hash digest
SHA256 c199e8a81ad73d187b1612b43d2d6bc65c2484bb5309b6bcf276c2ccea7fba49
MD5 28b72740574ca021f2a4cba2ece10f06
BLAKE2b-256 8f1db08036e20f791fb2e33f985e3299c14dfe8234bc0a777412034a9f980e67

See more details on using hashes here.

Supported by

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