Conditional Format Html Table Rows With Python
I must conditionally format rows of an html table that is generated with Python. I previously had to sort table columns and did so using javascript. Rather than conditionally forma
Solution 1:
So you'll need to add additional logic when you add your table rows:
## Create table with test information records#for tr in self.output_records:
test_count += 1
new_row, row_result = '', None#Modifiedfor fn in field_names:
if fn == 'Result':
if tr.Result > 4:
fail_count += 1
output_value, row_result = 'Fail', False#Modifiedelif tr.Result == 4:
skip_count += 1
output_value = 'Skipped'else:
output_value, row_result = 'Success', True#Modifiedelif fn == 'Output':
output_value = ''if tr.Output != '':
output_value = '<a target="_blank" href=' + \
FILE_LINK + tr.Output + \
' style="display:block;">Output</a>'elif fn == 'Execution_time':
output_value = ('%d:%02d:%02d' %
return_seconds_as_h_m_s(tr.Execution_time)
)
else:
output_value = str(getattr(tr, fn)) #Modified
new_row += '<td>' + output_value + '</td>'#Added new line
result_class = ''if row_result isNoneelse' class="{0}"'.format('selected'if row_result else'bad')
new_row = '<tr{0}>{1}</tr>'.format(result_class, new_row) #Modified
html_output.append(new_row)
I introduced another variable row_result
that will keep track of rows that pass or fail. Once that has been calculated, you can add that value to the row class (<tr class=""
) to stylize that row's output.
For the record, string building in loops is best accomplised by using the join
method. Also, for a cleaner approach you can use .format
to build out each line. You can see lots of evidence online.
Baca Juga
- Event Listener For Input's Value Change Through Changing With .val() In Jquery?
- Selecting A Default Value In An R Plotly Plot Using A Selectize Box Via Crosstalk In R, Using Static Html Not Shiny
- Getting Cors (cross-origin...) Error When Using Python Flask-restful With Consuming Angularjs (using $http)
Finally, don't use eval
if you can avoid it. It can easily introduce vulnerabilities. For your case, you can use getattr
to get the variable parameter name from tr
.
Post a Comment for "Conditional Format Html Table Rows With Python"