labs-reports/gen_static.py

135 lines
3.4 KiB
Python

import json
from datetime import datetime
def generate_metadata_table(trip_report):
metadata_table = """
<table>
<thead>
<tr>
<th>Attribute</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chemical</td>
<td>{}</td>
</tr>
<tr>
<td>Tester</td>
<td>{}</td>
</tr>
<tr>
<td>Dosage</td>
<td>{}</td>
</tr>
<tr>
<td>Tolerance</td>
<td>{}</td>
</tr>
<tr>
<td>Cross-Tolerance</td>
<td>{}</td>
</tr>
</tbody>
</table>
""".format(trip_report['chemical'], trip_report['tester'], trip_report['dosage'], trip_report['tolerance'], trip_report['cross_tolerance'])
return metadata_table
def generate_trip_report_table(trip_report):
trip_report_table = """
<table>
<thead>
<tr>
<th>Timestamp</th>
<th>Elapsed Time</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
"""
for entry in trip_report['entries']:
trip_report_table += """
<tr>
<td>{}</td>
<td>{}</td>
<td>{}</td>
</tr>
""".format(entry['timestamp'], entry['elapsed_time'], entry['notes'])
trip_report_table += """
</tbody>
</table>
"""
return trip_report_table
def generate_html_report(trip_report):
metadata_table = generate_metadata_table(trip_report)
trip_report_table = generate_trip_report_table(trip_report)
submit_date = trip_report.get('submit_date', 'N/A')
generation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
html_report = """
<html>
<head>
<style>
body {{
font-family: Arial, sans-serif;
color: #000;
background-color: #fff;
}}
table {{
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}}
th, td {{
border: 1px solid #000;
padding: 8px;
text-align: left;
}}
th {{
background-color: #000;
color: #fff;
}}
tr:nth-child(even) {{
background-color: #f2f2f2;
}}
</style>
</head>
<body>
<h3>Metadata</h3>
{}
<h3>Report</h3>
{}
<p>Report submitted at: {}</p>
<p>Static HTML generated at: {}</p>
</body>
</html>
""".format(metadata_table, trip_report_table, submit_date, generation_date)
return html_report
def save_html_report(html_report, destination_path):
with open(destination_path, 'w') as html_file:
html_file.write(html_report)
print("HTML report saved to {}.".format(destination_path))
if __name__ == "__main__":
json_path = input("Enter path to JSON report: ")
destination_path = input("Enter path for destination HTML file: ")
with open(json_path, 'r') as json_file:
trip_report = json.load(json_file)
html_report = generate_html_report(trip_report)
save_html_report(html_report, destination_path)