Skip to main content

Quickly convert a list of dictionaries to human readable tables

Project description

TableGen

Quickly convert a list of dictionaries to human readable tables

Create Table Object

from TableGen import Table

dict = [
    {"name": "Joseph" ,"age": 45, "favorite Animal": "Zebra" },
    {"name": "Jane" ,"age": 39, "favorite Animal": "Cat" },
    {"name": "Alex" ,"age": 57, "favorite Animal": "Black Bear" }
        ]

table = Table(dict)

now

print(table.TableString)

will print

----------------------------------
| name   | age | favorite Animal |
----------------------------------
| Joseph | 45  | Zebra           |
----------------------------------
| Jane   | 39  | Cat             |
----------------------------------
| Alex   | 57  | Black Bear      |
----------------------------------

add index with the index option

dict = [
    {"name": "Joseph" ,"age": 45, "favorite Animal": "Zebra" },
    {"name": "Jane" ,"age": 39, "favorite Animal": "Cat" },
    {"name": "Alex" ,"age": 57, "favorite Animal": "Black Bear" }
        ]

table = Table(dict,index=True)


print(table.TableString)

output is:

--------------------------------------
| # | name   | age | favorite Animal |
--------------------------------------
| 1 | Joseph | 45  | Zebra           |
--------------------------------------
| 2 | Jane   | 39  | Cat             |
--------------------------------------
| 3 | Alex   | 57  | Black Bear      |
--------------------------------------

Limit Rows

you can limit the number of rows contained in the string and HTML table:

from TableGen import Table  

dict = [  
    {"name": "Joseph" ,"age": 45, "favorite Animal": "Zebra" },  
  {"name": "Jane" ,"age": 39, "favorite Animal": "Cat" },  
  {"name": "Alex" ,"age": 57, "favorite Animal": "Black Bear" }  
        ]  

table = Table(dict ,rowlimit= 2)  

print(table.TableString)  

output is :

----------------------------------
| name   | age | favorite Animal |
----------------------------------
| Joseph | 45  | Zebra           |
----------------------------------
| Jane   | 39  | Cat             |
----------------------------------

change appearance of the text table

change the vertical and horizontal lines with yLineChar and xLineChar:

from TableGen import Table  

dict = [  
    {"name": "Joseph" ,"age": 45, "favorite Animal": "Zebra" },  
  {"name": "Jane" ,"age": 39, "favorite Animal": "Cat" },  
  {"name": "Alex" ,"age": 57, "favorite Animal": "Black Bear" }  
        ]  

table = Table(dict ,xLineChar= "++", yLineChar="║")  

print(table.TableString)

output:

++++++++++++++++++++++++++++++++++
║ name   ║ age ║ favorite Animal ║
++++++++++++++++++++++++++++++++++
║ Joseph ║ 45  ║ Zebra           ║
++++++++++++++++++++++++++++++++++
║ Jane   ║ 39  ║ Cat             ║
++++++++++++++++++++++++++++++++++
║ Alex   ║ 57  ║ Black Bear      ║
++++++++++++++++++++++++++++++++++

Change header with headertop, headerbottom and headery:

from TableGen import Table  

dict = [  
    {"name": "Joseph" ,"age": 45, "favorite Animal": "Zebra" },  
  {"name": "Jane" ,"age": 39, "favorite Animal": "Cat" },  
  {"name": "Alex" ,"age": 57, "favorite Animal": "Black Bear" }  
        ]  

table = Table(dict ,headertop= "_-+-_",headerbottom= "══.══",headerY= "{}" )  

print(table.TableString)

output:

_-+-__-+-__-+-__-+-__-+-__-+-__-+-__-+
{} name   {} age {} favorite Animal {}
══.════.════.════.════.════.════.════.
|  Joseph  | 45   | Zebra            |
--------------------------------------
|  Jane    | 39   | Cat              |
--------------------------------------
|  Alex    | 57   | Black Bear       |
--------------------------------------

Save as txt file

the savetxt() methood can be used to save the table string in a file . this will open a tkinter save dialog. save dialog

HTML Tables

A Table object also contains an html string of this table.

from TableGen import Table  

dict = [  
    {"name": "Joseph" ,"age": 45, "favorite Animal": "Zebra" },  
  {"name": "Jane" ,"age": 39, "favorite Animal": "Cat" },  
  {"name": "Alex" ,"age": 57, "favorite Animal": "Black Bear" }  
        ]  

table = Table(dict)  

print(table.HTMLString)

output:

<!DOCTYPE html>
<html>
<head>
<style>
 table.gen-table {
 font-family: Tahoma, Geneva, sans-serif;
 border: 1px solid #1C6EA4;
 background-color: #EEEEEE;
 width: 100%;
 text-align: left;
 border-collapse: collapse;
 }
 table.gen-table td, table.gen-table th {
 border: 1px solid #AAAAAA;
 padding: 3px 2px;
 }
 table.gen-table tbody td {
 font-size: 13px;
 }
 table.gen-table tr:nth-child(even) {
 background: #F88888;
 }
 table.gen-table thead {
 background: #900C3F ;
 border-bottom: 2px solid #444444;
 }
 table.gen-table thead th {
 font-size: 15px;
 font-weight: bold;
 color: #FFFFFF;
 border-left: 2px solid #D0E4F5;
 }
 table.gen-table thead th:first-child {
 border-left: none;
 }
 table.gen-table tfoot td {
 font-size: 14px;
 }
 table.gen-table tfoot .links {
 text-align: right;
 }
 table.gen-table tfoot .links a{
 display: inline-block;
 background: #1C6EA4;
 color: #FFFFFF;
 padding: 2px 8px;
 border-radius: 5px;
 }
 </style>
<title>Generated Table</title>
</head>
<body>
<table class="gen-table">
<thead>
<tr>
<th>name</th>
<th>age</th>
<th>favorite Animal</th>

</tr>
</thead>
<tbody>

<tr>
<td>Joseph</td>
<td>45</td>
<td>Zebra</td>
</tr>

<tr>
<td>Jane</td>
<td>39</td>
<td>Cat</td>
</tr>

<tr>
<td>Alex</td>
<td>57</td>
<td>Black Bear</td>
</tr>

</tbody>
</table>
</body>
</html>

the viewHTML() method can be used to open the html in browser using the webbrowser module.

from TableGen import Table  

dict = [  
    {"name": "Joseph" ,"age": 45, "favorite Animal": "Zebra" },  
  {"name": "Jane" ,"age": 39, "favorite Animal": "Cat" },  
  {"name": "Alex" ,"age": 57, "favorite Animal": "Black Bear" }  
        ]  

table = Table(dict)  

table.viewHTML()

this will open the default web browser with the table

Change table css

you can insert custom css and change the class of the table in the HTML by using the options HTMLstyle and HTMLtableclass

from TableGen import Table  

dict = [  
    {"name": "Joseph" ,"age": 45, "favorite Animal": "Zebra" },  
  {"name": "Jane" ,"age": 39, "favorite Animal": "Cat" },  
  {"name": "Alex" ,"age": 57, "favorite Animal": "Black Bear" }  
        ]  
style = '''  
.changedtable{  
border: solid;  
border-color : black;  
}  

.changedtable th{  
padding: 5px;  
background-color: #cce6ff;  
}  

.changedtable td{  
padding: 5px;  
background-color: #f2ffcc;  
}  

'''  

table = Table(dict,HTMLstyle=style,HTMLtableclass='changedtable')  

table.viewHTML()

this is the table generated from this code.

Get Table Directly from CSV

a Table object can be created from a CSV file using the fromCsv() method.

from TableGen import Table  

table = Table.fromCsv("pokemon.csv", rowlimit= 20,index=True)  

print(table.TableString)

output:

-----------------------------------------------------------------------------------------------------------------------------------------------
| #   | Name                      | Type 1   | Type 2   | Total | HP  | Attack | Defense | Sp. Atk | Sp. Def | Speed | Generation | Legendary |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 1   | Bulbasaur                 | Grass    | Poison   | 318   | 45  | 49     | 49      | 65      | 65      | 45    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 2   | Ivysaur                   | Grass    | Poison   | 405   | 60  | 62     | 63      | 80      | 80      | 60    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 3   | Venusaur                  | Grass    | Poison   | 525   | 80  | 82     | 83      | 100     | 100     | 80    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 4   | VenusaurMega Venusaur     | Grass    | Poison   | 625   | 80  | 100    | 123     | 122     | 120     | 80    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 5   | Charmander                | Fire     |          | 309   | 39  | 52     | 43      | 60      | 50      | 65    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 6   | Charmeleon                | Fire     |          | 405   | 58  | 64     | 58      | 80      | 65      | 80    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 7   | Charizard                 | Fire     | Flying   | 534   | 78  | 84     | 78      | 109     | 85      | 100   | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 8   | CharizardMega Charizard X | Fire     | Dragon   | 634   | 78  | 130    | 111     | 130     | 85      | 100   | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 9   | CharizardMega Charizard Y | Fire     | Flying   | 634   | 78  | 104    | 78      | 159     | 115     | 100   | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 10  | Squirtle                  | Water    |          | 314   | 44  | 48     | 65      | 50      | 64      | 43    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 11  | Wartortle                 | Water    |          | 405   | 59  | 63     | 80      | 65      | 80      | 58    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 12  | Blastoise                 | Water    |          | 530   | 79  | 83     | 100     | 85      | 105     | 78    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 13  | BlastoiseMega Blastoise   | Water    |          | 630   | 79  | 103    | 120     | 135     | 115     | 78    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 14  | Caterpie                  | Bug      |          | 195   | 45  | 30     | 35      | 20      | 20      | 45    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 15  | Metapod                   | Bug      |          | 205   | 50  | 20     | 55      | 25      | 25      | 30    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 16  | Butterfree                | Bug      | Flying   | 395   | 60  | 45     | 50      | 90      | 80      | 70    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 17  | Weedle                    | Bug      | Poison   | 195   | 40  | 35     | 30      | 20      | 20      | 50    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 18  | Kakuna                    | Bug      | Poison   | 205   | 45  | 25     | 50      | 25      | 25      | 35    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 19  | Beedrill                  | Bug      | Poison   | 395   | 65  | 90     | 40      | 45      | 80      | 75    | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------
| 20  | BeedrillMega Beedrill     | Bug      | Poison   | 495   | 65  | 150    | 40      | 15      | 80      | 145   | 1          | FALSE     |
-----------------------------------------------------------------------------------------------------------------------------------------------

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

tablegen-1.1.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

tablegen-1.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file tablegen-1.1.0.tar.gz.

File metadata

  • Download URL: tablegen-1.1.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.2

File hashes

Hashes for tablegen-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ac4f328f058b4a8831ba83f3a4c1f658cd65090aad9bfe728b45122e2baf1d6c
MD5 24007b7f66c61a52427c9aa5c38b3d73
BLAKE2b-256 d84c187c1c4907a7260f752d37fa0f25eec8bf2c8378be0f374c0bada21c854a

See more details on using hashes here.

File details

Details for the file tablegen-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: tablegen-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.2

File hashes

Hashes for tablegen-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4487e07b33b2245f682b654cfa419356fcc056d38150f69a27dbdc6d4dabad8b
MD5 e420e928ab1a6729b6abcd01673cb1a5
BLAKE2b-256 8c7974790b0f318f94114083ca596fdf2bf16200d5c5cd1dc623be3e65a3c060

See more details on using hashes here.

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