From 40639cee8e4de6ffbc707c86adb9d4ba9bb29c34 Mon Sep 17 00:00:00 2001 From: Chuck SmileyRekiere Date: Tue, 30 Mar 2021 07:33:55 -0600 Subject: [PATCH] lf_graph.py created , updates to lf_report.py, lf_report_test.py Signed-off-by: Chuck SmileyRekiere --- py-scripts/lf_graph.py | 139 +++++++++++++++++++++++++++++++++++ py-scripts/lf_report.py | 70 ++++++++---------- py-scripts/lf_report_test.py | 26 +++++++ 3 files changed, 194 insertions(+), 41 deletions(-) create mode 100644 py-scripts/lf_graph.py diff --git a/py-scripts/lf_graph.py b/py-scripts/lf_graph.py new file mode 100644 index 00000000..7c22f30d --- /dev/null +++ b/py-scripts/lf_graph.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python3 + +import matplotlib.pyplot as plt +import matplotlib as mpl +import numpy as np +import pandas as pd +import pdfkit +import math + +# internal candela references included during intial phases, to be deleted at future date + +# graph reporting classes +class lf_bar_graph(): + def __init__(self, + _data_set= [[30,55,69,37],[45,67,34,22],[22,45,12,34]], + _xaxis_name="x-axis", + _yaxis_name="y-axis", + _xaxis_categories=[1,2,3,4], + _graph_image_name="image_name", + _label=["bi-downlink", "bi-uplink",'uplink'], + _color=None, + _bar_width=0.25, + _color_edge='grey', + _font_weight='bold', + _color_name=['lightcoral','darkgrey','r','g','b','y'], + _figsize=(10,5), + _dpi=96): + + self.data_set=_data_set + self.xaxis_name=_xaxis_name + self.yaxis_name=_yaxis_name + self.xaxis_categories=_xaxis_categories + self.graph_image_name=_graph_image_name + self.label=_label + self.color=_color + self.bar_width=_bar_width + self.color_edge=_color_edge + self.font_weight=_font_weight + self.color_name=_color_name + self.figsize=_figsize + + + + def build_bar_graph(self): + if self.color is None: + i = 0 + self.color = [] + for col in self.data_set: + self.color.append(self.color_name[i]) + i = i+1 + + fig = plt.subplots(figsize=self.figsize) + i = 0 + for set in self.data_set: + if i > 0: + br = br1 + br2 = [x + self.bar_width for x in br] + plt.bar(br2, self.data_set[i], color=self.color[i], width=self.bar_width, + edgecolor=self.color_edge, label=self.label[i]) + br1 = br2 + i = i+1 + else: + br1 = np.arange(len(self.data_set[i])) + plt.bar(br1, self.data_set[i], color=self.color[i], width=self.bar_width, + edgecolor=self.color_edge, label=self.label[i]) + i=i+1 + plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) + plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) + plt.xticks([r + self.bar_width for r in range(len(self.data_set[0]))], + self.xaxis_categories) + plt.legend() + + fig = plt.gcf() + plt.savefig("%s.png"% (self.graph_image_name), dpi=96) + print("{}.png".format(self.graph_image_name)) + + return "%s.png" % (self.graph_image_name) + + + +# Unit Test +if __name__ == "__main__": + + output_html_1 = "graph_1.html" + output_pdf_1 = "graph_1.pdf" + + # test build_bar_graph with defaults + graph = lf_bar_graph() + graph_html_obj = """ + +

+ """ + # + test_file = open(output_html_1, "w") + test_file.write(graph_html_obj) + test_file.close() + + # write to pdf + # write logic to generate pdf here + # wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb + # sudo apt install ./wkhtmltox_0.12.6-1.focal_amd64.deb + options = {"enable-local-file-access" : None} # prevent eerror Blocked access to file + pdfkit.from_file(output_html_1, output_pdf_1, options=options) + + + # test build_bar_graph setting values + dataset = [[45,67,34,22],[22,45,12,34],[30,55,69,37]] + x_axis_values = [1,2,3,4] + + output_html_2 = "graph_2.html" + output_pdf_2 = "graph_2.pdf" + + # test build_bar_graph with defaults + graph = lf_bar_graph(_data_set=dataset, + _xaxis_name="stations", + _yaxis_name="Throughput 2 (Mbps)", + _xaxis_categories=x_axis_values, + _graph_image_name="Bi-single_radio_2.4GHz", + _label=["bi-downlink", "bi-uplink",'uplink'], + _color=None, + _color_edge='red') + graph_html_obj = """ + +

+ """ + # + test_file = open(output_html_2, "w") + test_file.write(graph_html_obj) + test_file.close() + + # write to pdf + # write logic to generate pdf here + # wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb + # sudo apt install ./wkhtmltox_0.12.6-1.focal_amd64.deb + options = {"enable-local-file-access" : None} # prevent eerror Blocked access to file + pdfkit.from_file(output_html_2, output_pdf_2, options=options) + + + diff --git a/py-scripts/lf_report.py b/py-scripts/lf_report.py index 996fc94e..055a8c83 100755 --- a/py-scripts/lf_report.py +++ b/py-scripts/lf_report.py @@ -15,6 +15,7 @@ class lf_report(): _dataframe="", _title="LANForge Test Run Heading", _table_title="LANForge Table Heading", + _graph_title="LANForge Graph Title", _obj = "", _obj_title = "", _date="1/1/2-21 00:00(UTC)", @@ -24,11 +25,13 @@ class lf_report(): self.dataframe=_dataframe self.title=_title self.table_title=_table_title + self.graph_title=_graph_title self.date=_date self.output_html=_output_html self.output_pdf=_output_pdf self.banner_html = "" self.graph_titles="" + self.graph_image="" self.html = "" self.custom_html = "" self.objective = _obj @@ -41,6 +44,9 @@ class lf_report(): def set_table_title(self,_table_title): self.table_title = _table_title + def set_graph_title(self,_graph_title): + self.graph_title = _graph_title + def set_date(self,_date): self.date = _date @@ -54,6 +60,9 @@ class lf_report(): self.objective = _obj self.obj_title = _obj_title + def set_graph_image(self,_graph_image): + self.graph_image = _graph_image + def write_html(self): # dataframe_html = self.dataframe.to_html(index=False) # have the index be able to be passed in. # self.build_banner() @@ -72,6 +81,11 @@ class lf_report(): options = {"enable-local-file-access" : None} # prevent eerror Blocked access to file pdfkit.from_file(self.output_html, self.output_pdf, options=options) + + def generate_report(self): + self.write_html() + self.write_pdf() + # only use is pass all data in constructor, no graph output def build_all(self): self.build_banner() @@ -132,48 +146,28 @@ class lf_report(): """ self.html += self.obj_html - def build_bar_graph(self, data_set, xaxis_name, yaxis_name, xaxis_categories, graph_image_name, label,color=None): - barWidth = 0.25 - color_name = ['lightcoral','darkgrey','r','g','b','y'] - if color is None: - i = 0 - color = [] - for col in data_set: - color.append(color_name[i]) - i = i+1 + def build_graph_title(self): + self.table_graph_html = """ + + + + +
+

""" + str(self.graph_title) + """

+ """ + self.html += self.table_graph_html - - - fig = plt.subplots(figsize=(10, 5)) - i = 0 - for set in data_set: - if i > 0: - br = br1 - br2 = [x + barWidth for x in br] - plt.bar(br2, data_set[i], color=color[i], width=barWidth, - edgecolor='grey', label=label[i]) - br1 = br2 - i = i+1 - else: - br1 = np.arange(len(data_set[i])) - plt.bar(br1, data_set[i], color=color[i], width=barWidth, - edgecolor='grey', label=label[i]) - i=i+1 - plt.xlabel(xaxis_name, fontweight='bold', fontsize=15) - plt.ylabel(yaxis_name, fontweight='bold', fontsize=15) - plt.xticks([r + barWidth for r in range(len(data_set[0]))], - xaxis_categories) - plt.legend() - - fig = plt.gcf() - plt.savefig("%s.png"% (graph_image_name), dpi=96) + def build_graph(self): self.graph_html_obj = """ - +

""" self.html +=self.graph_html_obj + + + # Unit Test if __name__ == "__main__": @@ -213,12 +207,6 @@ if __name__ == "__main__": report.set_dataframe(dataframe2) report.build_table() - dataset = [[30,55,69,37],[45,67,34,22],[22,45,12,34]] - x_axis_values = [1,2,3,4] - report.build_bar_graph(dataset, "stations", "Throughput (Mbps)", x_axis_values, - "Bi-single_radio_2.4GHz", - ["bi-downlink", "bi-uplink",'uplink'], None) - #report.build_all() diff --git a/py-scripts/lf_report_test.py b/py-scripts/lf_report_test.py index 1e11cd45..41e8b929 100644 --- a/py-scripts/lf_report_test.py +++ b/py-scripts/lf_report_test.py @@ -6,6 +6,7 @@ import numpy as np import pandas as pd import pdfkit from lf_report import lf_report +from lf_graph import lf_bar_graph # Unit Test if __name__ == "__main__": @@ -47,6 +48,29 @@ if __name__ == "__main__": report.set_dataframe(dataframe2) report.build_table() + # test lf_graph in report + dataset = [[45,67,34,22],[22,45,12,34],[30,55,69,37]] + x_axis_values = [1,2,3,4] + + report.set_graph_title("Graph Title") + report.build_graph_title() + graph = lf_bar_graph(_data_set=dataset, + _xaxis_name="stations", + _yaxis_name="Throughput 2 (Mbps)", + _xaxis_categories=x_axis_values, + _graph_image_name="Bi-single_radio_2.4GHz", + _label=["bi-downlink", "bi-uplink",'uplink'], + _color=None, + _color_edge='red') + + + graph_png = graph.build_bar_graph() + + print("graph name {}".format(graph_png)) + + report.set_graph_image(graph_png) + + report.build_graph() #report.build_all() @@ -54,3 +78,5 @@ if __name__ == "__main__": print("returned file {}".format(html_file)) print(html_file) report.write_pdf() + + report.generate_report()