meteor23
meteor23

Reputation: 287

Python Pretty table with color output

I am using pretty table to generate tables output.

Is it possible to generate in terms of colors. If failed it should display in red and its ok it should display in green.

Code:

from prettytable import PrettyTable
a = "ok"
b = "Failed"
t = PrettyTable(['Input', 'status'])
if a == "ok":
   t.add_row(['FAN', a])
else:
    t.add_row(['FAN', b])
print t  

Upvotes: 8

Views: 18846

Answers (2)

TouhidShaikh
TouhidShaikh

Reputation: 111

Here is an example of an easy way to add colour in table.

from prettytable import PrettyTable

#Color
R = "\033[0;31;40m" #RED
G = "\033[0;32;40m" # GREEN
Y = "\033[0;33;40m" # Yellow
B = "\033[0;34;40m" # Blue
N = "\033[0m" # Reset


a = "ok"
b = "Failed"
t = PrettyTable(['Input', 'status'])

#Adding Both example in table
t.add_row(['FAN', G+a+N])
t.add_row(['FAN', R+b+N])

print t

Upvotes: 11

L.kifa
L.kifa

Reputation: 95

here we go

from prettytable import PrettyTable
a = "ok"
b = "Failed"
t = PrettyTable(['Input', 'status'])
if a == "ok":
  a = "\033[1;32m%s\033[0m" %a 
  t.add_row(['FAN', a])
else:
  b = "\033[1;31m%s\033[0m" %b
  t.add_row(['FAN', b])
print t  

Upvotes: 2

Related Questions