7

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  
meteor23
  • 267
  • 3
  • 6
  • 11

2 Answers2

9

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
TouhidShaikh
  • 91
  • 1
  • 4
2

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  
L.kifa
  • 95
  • 1
  • 9