Giter Club home page Giter Club logo

tableprint's Introduction

tableprint

Pretty console printing ๐Ÿ“‹ of tabular data in python ๐Ÿ

build codecov docs pypi

โ„น๏ธŽ About

tableprint lets you easily print formatted tables of data. Unlike other modules, you can print single rows of data at a time (useful for printing ongoing computation results).

Example output

๐Ÿ”Ž Table of Contents

๐Ÿ’ป Installation

pip install tableprint

๐Ÿƒ Usage

The table function takes in a matrix of data, a list of headers, a width (defaults to 11) and a style (defaults to 'round'). To print a dataset consisting of 10 rows of 3 different columns with the default width and style:

import tableprint as tp
import numpy as np

data = np.random.randn(10, 3)
headers = ['Column A', 'Column B', 'Column C']

tp.table(data, headers)

The header and row functions allow you to print just the header or just a row of data, respectively, which is useful for continuously updating a table during a long-running computation. Also, the banner function is useful for just printing out a nicely formatted message to the user.

The TableContext context manager is useful for dynamically updating tables (e.g. during a long running computation):

import tableprint as tp
import numpy as np
import time

with tp.TableContext("ABC") as t:
    for _ in range(10):
        time.sleep(0.1)
        t(np.random.randn(3,))

๐Ÿ“š Documentation

Hosted at Read The Docs: tableprint.readthedocs.org

๐Ÿ“ฆ Dependencies

โค๏ธ Contributors

Thanks to: @nowox, @nicktimko, @mubaris, and @sumanthratna for contributions.

๐Ÿ›  Changelog

Version Release Date Description
0.9.1 Aug 9 2020 Drops python2 support.
0.9.0 May 16 2020 Adds support for automatically determining the table's width.
0.8.0 Oct 24 2017 Improves support for international languages, removes numpy dependency
0.7.0 May 26 2017 Adds a TableContext context manager for easy creation of dynamic tables (tables that update periodically). Adds the ability to pass a list or tuple of widths to specify different widths for different columns
0.6.9 May 25 2017 Splitting the tableprint.py module into a pacakge with multiple files
0.6.7 May 25 2017 Fixes some bugs with ANSI escape sequences
0.5.0 Sept 29 2016 Better handling of ANSI escape sequences in table rows
0.4.0 May 3 2016 Adds a 'block' style
0.3.2 May 3 2016 Adds a test suite
0.3.0 May 3 2016 Adds custom styles for tables, specified by a key ('fancy_grid', 'grid', etc.)
0.2.0 May 2 2016 Adds better python2 (unicode/bytes) compatibility
0.1.5 Oct 1 2015 Renamed hrtime to humantime, added docs
0.1.4 Sept 28 2015 Added human readable string converter (hrtime)
0.1.0 Feb 24 2015 Initial release

๐Ÿ”“ License

MIT. See LICENSE.md

tableprint's People

Contributors

arjunnn avatar nirum avatar s-t-e-v-e-n-k avatar sumanthratna avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

tableprint's Issues

ANSI escape not supported

The column size is improperly computed with the following example:

import tableprint as tp
from numpy.random import randn
from termcolor import colored

tp.table(randn(2,1), [colored('Foo', 'red')], style='fancy_grid')

It gives:

โ•’โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ••
โ”‚Fooโ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚  -0.060884โ”‚
โ”‚     -1.034โ”‚
โ•˜โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•›

I think this can be corrected on header by using this:

    def format_headers(string):
        s = max(width - _ansi_len(string), 0)
        return (' ' * (s / 2)) + string + (' ' * (s - (s / 2))) if s else string

    data = map(format_headers, headers)  

Also the _ansi_len should be rewritten:

def _ansi_len(string):
    """Extra length due to any ANSI sequences in the string."""
    return len(re.sub(r'\x1b\[([0-9,A-Z]{1,2}(;[0-9]{1,2})?(;[0-9]{3})?)?[m|K]?', '', string))

Drop Numpy as a hard requirement

Including Numpy as a necessary requirement is extremely heavy for a terminal printer. It seems like you just use it to convert a dataframe to an array and to do a floor function on a scalar(!). The latter can be easily replicated through math.floor in the stdlib, and for the former, you could attempt to import Numpy if it exists inside the dataframe function (if someone's giving you a Pandas dataframe you know there'll be Numpy).

Custom print command

It'd be useful if tableprint could accept a custom print command. In my use case, I want to progressively (print-as-I-go) print the values of TensorFlow 2 tensors in a table.

In autograph, the only way to print TensorFlow tensors is with tf.printโ€”using print(my_tensor) doesn't print the value.

I can think of two ways to approach this:

  • accept a callable that defaults to Python's print.
    • The only problem is that there are certain options that tableprint may need to pass to tf.print; for example, output_stream. Since these arguments differ from those of Python's print, tableprint's implementation will need to use if statements to see if the callable is tf.print. This brings me to the second option:
  • accept a boolean called print_tensorflow that defaults to False. If True, print using tf.print.
    • I like this idea better since the first option implies that any callable can be used, such as click.echo, even if tableprint doesn't have click.echo's options implemented (of course, a NotImplementedError could be raised).
    • However, this would limit tableprint to only print and tf.print. If you want to add more callables in the future, you could always change this argument to a string, and use an if statement to see if the string is python, tensorflow, click, etc.

Error executing on windows

I try this:

import tableprint as tp
tp.banner('test')

error:

Traceback (most recent call last):
  File "stats.py", line 83, in <module>
    main()
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\click\core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\click\core.py", line 697, in main
    rv = self.invoke(ctx)
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\click\core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\click\core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\click\core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "stats.py", line 40, in dataset
    tp.banner('title_text')
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\tableprint\printer.py", line 259, in banner
    out.write(header([message], max(width, len(message)), style) + '\n')
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\colorama\ansitowin32.py", line 40, in write
    self.__convertor.write(text)
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\colorama\ansitowin32.py", line 141, in write
    self.write_and_convert(text)
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\colorama\ansitowin32.py", line 169, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\colorama\ansitowin32.py", line 174, in write_plain_text
    self.wrapped.write(text[start:end])
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\colorama\ansitowin32.py", line 40, in write
    self.__convertor.write(text)
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\colorama\ansitowin32.py", line 141, in write
    self.write_and_convert(text)
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\colorama\ansitowin32.py", line 169, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\site-packages\colorama\ansitowin32.py", line 174, in write_plain_text
    self.wrapped.write(text[start:end])
  File "C:\Users\acnazarejr\Anaconda3\envs\harhealth-sensors\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-31: character maps to <undefined>

Python version: 3.5.3
OS: Windows 10

feature request: print multiple items in a column (group by)

Something like:


-------------------------------
| A               | B1                   |
|                   |  B2                  |
|                   |   B3                 | 
--------------------------------

if A elements are the same, but B1 & B2 elements are different.
(in fact, table is a result of a "group by column 1" )
I can try to simulate this by inserting rows with zero value for column A, but i don't know that order will be kept.

str(array) is not stable for table print

967757	2	30	14	100.00	1	[100.00]	[30, 14]
967722	2	14	6	100.00	2	[100.00, 100.00]	[30, 14, 6]
967699	2	6	2	100.00	3	[100.00, 100.00, 100.00]	[30, 14, 6, 2]
967686	2	2	0	100.00	4	[100.00, 100.00, 100.00, 100.00]	[30, 14, 6, 2, 0]
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚  policy_id  โ”‚  iteration  โ”‚     id      โ”‚   next_id   โ”‚ proability  โ”‚   length    โ”‚ proability_path โ”‚   id_path   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚      967757 โ”‚           2 โ”‚          30 โ”‚          14 โ”‚      100.00 โ”‚           1 โ”‚ [100.00]                         โ”‚ [30, 14]          โ”‚
โ”‚      967722 โ”‚           2 โ”‚          14 โ”‚           6 โ”‚      100.00 โ”‚           2 โ”‚ [100.00, 100.00]                 โ”‚ [30, 14, 6]       โ”‚
โ”‚      967699 โ”‚           2 โ”‚           6 โ”‚           2 โ”‚      100.00 โ”‚           3 โ”‚ [100.00, 100.00, 100.00]         โ”‚ [30, 14, 6, 2]    โ”‚
โ”‚      967686 โ”‚           2 โ”‚           2 โ”‚           0 โ”‚      100.00 โ”‚           4 โ”‚ [100.00, 100.00, 100.00, 100.00] โ”‚ [30, 14, 6, 2, 0] โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Feature request: Horizontal alignment

This is a simple, lightweight, and awesome package and exactly what I needed to print some database data in my CLI app. Any chance you could add two parameters for horizontal alignment that takes a string for the alignment of the cell data, such as "left", "center", or "right"? Something like header_align and row_align?? As it is right now I have to pad all the strings to be the same length as the longest one otherwise the table is difficult to read.

Thanks so much!

No width validation

When adding data that is wider than the tty screen, the table just breaks.

Auto-width

Love tableprint!

Since everything is monospaced, it should be easy to set the width on each column automatically based on the longest string value of each column (and its header). Could you please implement this? It would save precious horizontal space and fit more columns.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.