2

I am have simple CLI app written in python with argparse module. Basically I am fetching some cryptocurrency data with external api, I transform it into pandas dataframe and print it with tabulate module. But I have issue with printing tables in my terminal. When I am doing that everything goes wrong and the tables do not maintain the proper structure and in the end all looks like on screen belowe.

Bad formatting

Here is snippet for printing pandas data frame with tabulate

# file: example.py
import requests
from tabulate import tabulate


def get_recommendations():
    url = "https://min-api.cryptocompare.com/data/recommended/all"
    req = requests.get(url, params={'fsym' : 'BTC' ,"tsym": 'USD'})
    return pd.DataFrame(req.json()['Data']['exchanges']).T


df = get_recommendations()

print(
    tabulate(
        df,
        headers=df.columns,
        floatfmt=".5f",
        showindex=True,
        tablefmt="psql",
    )
)

if you run in terminal python example.py you will see mess in terminal like on screenshot above code snippet.

Can I some how fix it, and display nice, well formatted tables in my terminal like below ? enter image description here

Jakub Pluta
  • 147
  • 5
  • 13
  • It looks like the table is wider than your terminal, do you have an example snippet of the data to make this a [mcve]? – Alex May 15 '21 at 15:52
  • Try [termtables](https://github.com/nschloe/termtables), a lightweight Python 3 package for pretty-printing tables on the command line – Ailurophile May 15 '21 at 15:54
  • @Alex I added full example. Hope that it is enough to recreate this. – Jakub Pluta May 15 '21 at 17:07
  • @Pluviophile I will try to use this package, but I think maybe it's possible to somehowe add some constraints for terminal to display all in proper form, even if table has bigger size then terminal window. – Jakub Pluta May 15 '21 at 17:09

2 Answers2

0

I was faced with a similar issue: How best to print out tables in the Terminal? There are lots of different approaches (Of course, you can always just use PrettyPrinter). I found that if I use a pandas DataFrame, combined with the rich-dataframe module, I can produce a decent-looking table in the Terminal. So the code might look like this:

Suppose you start with a list. Convert the list to a dict. Then pass the dict to the dataframe. Finally, call the print statement at the end. It works fine in a Terminal.

Edit: In your MWE, you may need to convert the JSON object to a Python dict. Use import json, then json.loads(dict) See examples

from rich_dataframe import prettify
import pandas as pd

languages = ['English', 'Italiano', 'Espanol']

x = dict(enumerate(languages))

df = pd.DataFrame.from_dict(x, orient='index', columns=['Languages Supported'])

table = prettify(df, row_limit=10)

print(table)

lino
  • 51
  • 2
  • 5
0

Pandas has the DataFrame.to_markdown() method, which uses tabulate to generate nice tables that you can print to the console. You can pass the tablefmt= parameter to get different style tables as per tabulate documentation.

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_markdown.html

RustyPython
  • 370
  • 1
  • 7