Created
November 24, 2022 07:08
-
-
Save erap129/c7d44c2f302b4bf912c784f189b80887 to your computer and use it in GitHub Desktop.
HTML Reporter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import io | |
import base64 | |
import plotly.express as px | |
def generate_html_report(report_items): | |
html_string = ''' | |
<html> | |
<head> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> | |
<style>body{ margin:0 100; background:whitesmoke; }</style> | |
</head> | |
<body> | |
''' + '\n'.join(report_items) + ''' | |
</body> | |
</html>''' | |
with open('report.html', 'w') as f: | |
f.write(html_string) | |
return 'report.html' | |
def get_matplotlib_plot(): | |
t = np.arange(0.0, 2.0, 0.01) | |
s = 1 + np.sin(2 * np.pi * t) | |
fig, ax = plt.subplots() | |
ax.plot(t, s) | |
ax.set(xlabel='time (s)', ylabel='voltage (mV)', | |
title='About as simple as it gets, folks') | |
ax.grid() | |
buf = io.BytesIO() | |
fig.savefig(buf, format='png') | |
plt.close() | |
return "data:image/png;base64,{}".format(base64.b64encode(buf.getvalue()).decode('utf8')) | |
if __name__ == '__main__': | |
generate_html_report([ | |
'<h1>This is an example HTML report</h1>', | |
'<hr/>' | |
'<h3>This is a table</h3>', | |
pd.DataFrame({'Food': ['pizza', 'pasta', 'ravioli'], | |
'How much I like it': [5, 4, 3], | |
'How much people in Italy like it': [3, 4, 5]}).to_html(index=False, classes=['table']), | |
'<hr/>', | |
'<h3>This is a matplotlib plot</h3>', | |
f'<img src={get_matplotlib_plot()}>' | |
'<hr/>', | |
'<h3>This is a plotly plot</h3>', | |
px.scatter(px.data.iris(), x="sepal_width", y="sepal_length", color="species").to_html(), | |
'<hr/>', | |
'<h3>This is some text</h3>', | |
'<pre>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et\n' | |
'dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex\n' | |
' ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat\n' | |
' nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia\n' | |
' deserunt mollit anim id est laborum.</pre>' | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment