summary = [['Metrics','Status']]
try:
for i in output['responsetimes']:
if i['metric'] == 'ResponseTime':
k = i['value'].split(' ')
if int(k[0])<1000:
temp = ['Response Times','Green']
summary.append(temp)
else:
temp = ['Response Times','Red']
summary.append(temp)
except:
summary.append(['Response Times','NA'])
try:
for i in output['runtimedumps']:
if i['metric'] == 'Shortdumps Frequency':
k = i['value'].split(' ')
if int(k[0])==0:
temp = ['Runtime Dumps','Green']
summary.append(temp)
else:
temp = ['Runtime Dumps','Red']
summary.append(temp)
except:
summary.append(['Runtime Dumps','NA'])
try:
temp = []
for i in output['buffer']:
if (i['metric'] == 'HitRatio'):
k = i['value'].split(' ')
if int(k[0])>95:
temp.append('green')
else:
temp.append('red')
if 'red' in temp:
summary.append(['Buffer','Red'])
else:
summary.append(['Buffer','Green'])
except:
summary.append(['Buffer','NA'])
try:
for i in output['updatemonitoring']:
if i['metric'] == 'ErrorsInWpUD1':
if int(i['value'])==0:
temp = ['Update Monitoring','Green']
summary.append(temp)
else:
temp = ['Update Monitoring','Red']
summary.append(temp)
except:
summary.append(['Update Monitoring','NA'])
try:
for i in output['memory']:
if i['metric'] == 'Physical':
total = int(i['value'].split(' ')[0])
if i['metric'] == 'Free (Value)':
free = int(i['value'].split(' ')[0])
if int((free*100)/total)<5:
summary.append(['Memory Utilization','Red'])
else:
summary.append(['Memory Utilization','Green'])
except:
summary.append(['Memory Utilization','Green'])
try:
for i in output['cpu']:
if i['metric'] == 'CPU_Utilization':
used = int(i['value'].split(' ')[0])
if used>80:
summary.append(['CPU Utilization','Red'])
else:
summary.append(['CPU Utilization','Green'])
except:
summary.append(['CPU Utilization','NA'])
try:
temp = []
for i in output['fs']:
if int(i['perc'].split(' ')[0])>85:
temp.append('red')
else:
temp.append('green')
if 'red' in temp:
summary.append(['File System','Red'])
else:
summary.append(['File System','Green'])
except:
summary.append(['File System','NA'])
t=Table(summary,hAlign='LEFT')
GRID_STYLE = TableStyle(
[
('GRID',(0,0),(-1,-1),0.5,colors.black),
('BACKGROUND', (0, 0), (-1, 0), '#2766A8'),
('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
]
)
t.setStyle(GRID_STYLE)
Story.append(t)
Story.append(Spacer(1, 12))
I am creating a table using report lab,
You can see how it looks in the image below:
I want to highlight the cell based on their values.
For example,
Response Times
cell would be green if its value is green or red otherwise.
I'm new at this, and can use some guidance on how to achieve this.
Found the answer myself after some more searching stackoverflow questions.
Might help someone else..
In my case, list summary
has the heading by default which is ['Metrics','Status']
and I'm appending the rest of the values based on validations like this,
for i in output['responsetimes']:
if i['metric'] == 'ResponseTime':
k = i['value'].split(' ')
if int(k[0])<1000:
temp = ['Response Times','Green']
summary.append(temp)
else:
temp = ['Response Times','Red']
summary.append(temp)
So in the end my summary
list looks something like this,
[
['Metrics','Status'],
['Response','Green'],
['Metrics2','Red'],
['Metrics4','Green'],
['Metrics3','Red']
]
And now I just need to loop over this summary
list and add styles to the already existing GRID_STYLE
with the help of TableStyle
class like this,
GRID_STYLE = TableStyle(
[
('GRID',(0,0),(-1,-1),0.5,colors.black),
('BACKGROUND', (0, 0), (-1, 0), '#2766A8'),
('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
]
)
for row, values, in enumerate(summary):
for column, value in enumerate(values):
if value == "Red":
GRID_STYLE.add('BACKGROUND', (column, row), (column, row), colors.red)
if value == "Green":
GRID_STYLE.add('BACKGROUND', (column, row), (column, row), colors.green)
t.setStyle(GRID_STYLE)