Allows for pretty printing and automatic resizing of tables.
Project description
Python Table Print
A lightweight library to create ASCII tables that automaticallly update their spacing based on the input.
Usage
Python Table Print is object-oriented. This means that a PrintTable
object gets instantiated and added to/edited. When the table is ready to be printed/created, this can be done by calling the get_table
method. An example can be found in example.py
, but is copied here for convenience:
Basic Usage
from table import PrintTable
my_table = PrintTable()
my_table.add_row("Col 1", "Col 2", "Col 3")
my_table.add_row("Entry 1", "Entry number 2", "Entry 3")
my_table.add_row("Another entry", "yay", "an entry in the table")
my_table.add_row("Fun times", "This is kinda cool", "wooow")
print(my_table.get_table())
Output:
┌───────────────┬────────────────────┬───────────────────────┐
│ Col 1 │ Col 2 │ Col 3 │
├───────────────┼────────────────────┼───────────────────────┤
│ Entry 1 │ Entry number 2 │ Entry 3 │
│ Another entry │ yay │ an entry in the table │
│ Fun times │ This is kinda cool │ wooow │
└───────────────┴────────────────────┴───────────────────────┘
As shown in the above example, getting the current table is as simple as calling the get_table()
method. This returns the table as a simple string. This can then be passed to whatever function you like, in this case the print
function.
Header
By default, the first row of the table is treated as the header of the table, and is printed with an extra border around it, not done for any other row. This behaviour can be turned off/on by setting the property has_header
to True
/False
:
from table import PrintTable
my_table = PrintTable()
my_table.add_row("Col 1", "Col 2", "Col 3")
my_table.add_row("Entry 1", "Entry number 2", "Entry 3")
my_table.add_row("Another entry", "yay", "an entry in the table")
my_table.add_row("Fun times", "This is kinda cool", "wooow")
my_table.has_header_row = False
print(my_table.get_table())
Output:
┌───────────────┬────────────────────┬───────────────────────┐
│ Col 1 │ Col 2 │ Col 3 │
│ Entry 1 │ Entry number 2 │ Entry 3 │
│ Another entry │ yay │ an entry in the table │
│ Fun times │ This is kinda cool │ wooow │
└───────────────┴────────────────────┴───────────────────────┘
Justification
Be default, all of the cells are justified to the left. However, justification can be changed for the entire table, for a row or column, or for individual cells. Whenever a justification is set it overrides any previous justification set on that/those cell/cells. Note that Justification
also needs to be imported.
Justification for the Table:
from table import PrintTable, Justification
my_table = PrintTable()
my_table.add_row("Col 1", "Col 2", "Col 3")
my_table.add_row("Entry 1", "Entry number 2", "Entry 3")
my_table.add_row("Another entry", "yay", "an entry in the table")
my_table.add_row("Fun times", "This is kinda cool", "wooow")
my_table.set_table_justification(Justification.CENTRE)
print(my_table.get_table())
Output:
┌───────────────┬────────────────────┬───────────────────────┐
│ Col 1 │ Col 2 │ Col 3 │
├───────────────┼────────────────────┼───────────────────────┤
│ Entry 1 │ Entry number 2 │ Entry 3 │
│ Another entry │ yay │ an entry in the table │
│ Fun times │ This is kinda cool │ wooow │
└───────────────┴────────────────────┴───────────────────────┘
Justification for a Row:
Continuing from the previous example above:
...
my_table.set_row_justification(0, Justification.LEFT)
print(my_table.get_table())
Output:
┌───────────────┬────────────────────┬───────────────────────┐
│ Col 1 │ Col 2 │ Col 3 │
├───────────────┼────────────────────┼───────────────────────┤
│ Entry 1 │ Entry number 2 │ Entry 3 │
│ Another entry │ yay │ an entry in the table │
│ Fun times │ This is kinda cool │ wooow │
└───────────────┴────────────────────┴───────────────────────┘
Justification for a Column:
Continuing from the previous example above:
...
my_table.set_column_justification(0, Justification.RIGHT)
print(my_table.get_table())
Output:
┌───────────────┬────────────────────┬───────────────────────┐
│ Col 1 │ Col 2 │ Col 3 │
├───────────────┼────────────────────┼───────────────────────┤
│ Entry 1 │ Entry number 2 │ Entry 3 │
│ Another entry │ yay │ an entry in the table │
│ Fun times │ This is kinda cool │ wooow │
└───────────────┴────────────────────┴───────────────────────┘
Justification for a Cell:
Continuing from the previous example above:
...
my_table.set_cell_justification(1, 1, Justification.LEFT)
print(my_table.get_table())
Output:
┌───────────────┬────────────────────┬───────────────────────┐
│ Col 1 │ Col 2 │ Col 3 │
├───────────────┼────────────────────┼───────────────────────┤
│ Entry 1 │ Entry number 2 │ Entry 3 │
│ Another entry │ yay │ an entry in the table │
│ Fun times │ This is kinda cool │ wooow │
└───────────────┴────────────────────┴───────────────────────┘
Notice how in the above examples the justifications of the edited cells were overwritten with the latest justification.
Title
You can add a title to your table. Defaults to centre justification
...
my_table.set_title("Title")
Output:
┌────────────────────────────────────────────────────────────┐
│ Title │
├───────────────┬────────────────────┬───────────────────────┤
│ Col 1 │ Col 2 │ Col 3 │
├───────────────┼────────────────────┼───────────────────────┤
│ Entry 1 │ Entry number 2 │ Entry 3 │
│ Another entry │ yay │ an entry in the table │
│ Fun times │ This is kinda cool │ wooow │
└───────────────┴────────────────────┴───────────────────────┘
Clearing a title:
...
my_table.clear_title()
Output:
┌───────────────┬────────────────────┬───────────────────────┐
│ Col 1 │ Col 2 │ Col 3 │
├───────────────┼────────────────────┼───────────────────────┤
│ Entry 1 │ Entry number 2 │ Entry 3 │
│ Another entry │ yay │ an entry in the table │
│ Fun times │ This is kinda cool │ wooow │
└───────────────┴────────────────────┴───────────────────────┘
Justification for Title
You can change the justification of your title:
...
my_table.set_title("Title")
my_table.set_title_justification(Justification.LEFT)
Output:
┌────────────────────────────────────────────────────────────┐
│ Title │
├───────────────┬────────────────────┬───────────────────────┤
│ Col 1 │ Col 2 │ Col 3 │
├───────────────┼────────────────────┼───────────────────────┤
│ Entry 1 │ Entry number 2 │ Entry 3 │
│ Another entry │ yay │ an entry in the table │
│ Fun times │ This is kinda cool │ wooow │
└───────────────┴────────────────────┴───────────────────────┘
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python_table_print-1.0.0.tar.gz
.
File metadata
- Download URL: python_table_print-1.0.0.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e815ab85ad6d30a2de2deda5d3b4ed24bcd5777efe7f726d68ee946486dd906 |
|
MD5 | e3a0e97f4285db9e7eb9a6261ca896dc |
|
BLAKE2b-256 | 468253755e2dc579c18c1cf01e075538fb0400138953381ac0af52eb1c7d2b55 |
File details
Details for the file python_table_print-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: python_table_print-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5d527cbfe01ca41cdfc05e5b34a5e036f5ae70c261474a6070cba50c211f00a |
|
MD5 | 7b8daab4fddb7ddd685bdf8d0cf6c05d |
|
BLAKE2b-256 | 9a0096ebd0c293dfd3910ac9ac644a0529a841fdbd7f4d9dca591211bf273000 |