From d6399e9601c526b594f340b483f4f2c1636ead35 Mon Sep 17 00:00:00 2001 From: Tony-Thawatchai Date: Mon, 12 Feb 2024 23:19:50 -0800 Subject: [PATCH 1/3] #15:feat:report generation as HTML --- .../src/report_generation/report_generator.py | 84 +++++++++++++++++++ .../template/reporttemplate.html | 47 +++++++++++ analytics/report/src/report_store/output.html | 83 ++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 analytics/report/src/report_generation/template/reporttemplate.html create mode 100644 analytics/report/src/report_store/output.html diff --git a/analytics/report/src/report_generation/report_generator.py b/analytics/report/src/report_generation/report_generator.py index e69de29..713b40a 100644 --- a/analytics/report/src/report_generation/report_generator.py +++ b/analytics/report/src/report_generation/report_generator.py @@ -0,0 +1,84 @@ +print("Starting script...") +import pandas as pd +import os +from jinja2 import Environment, FileSystemLoader + +# Sample data +data = { + "listing_id": [1, 2, 3, 4, 5, 6, 7], + "address": ["11 ABC street.", "22 ABC street.", "33 ABC street.", "44 ABC street.", "55 ABC street.", "66 ABC street.", "77 ABC street."], + "policy_A": [True, False, True, False, True, False, True] +} + + +######################################################################## +######################################################################## +# USING JINJA2 TO HTML +######################################################################## +######################################################################## + + +# Get the absolute path to the directory containing report_generator.py +dir_path = os.path.dirname(os.path.realpath(__file__)) + +# Use this path to set the correct path to the template/ directory +template_dir = os.path.join(dir_path, 'template') + + +# Create a Jinja2 environment +env = Environment(loader=FileSystemLoader(template_dir)) +template = env.get_template('reporttemplate.html') + +# get the date of report generation as the current date +from datetime import datetime +today = datetime.now() +todayStr = today.strftime('%Y-%m-%d %H:%M') + +# render the template with the data +html_out = template.render( + data=data, + date=todayStr +) + +# Get the absolute path to the directory containing the script +script_dir = os.path.dirname(os.path.abspath(__file__)) + +# Construct the absolute path to the output file +output_file = os.path.join(script_dir, '../report_store/output.html') + +# Write the HTML table to the output file +print("Writing HTML table to output.html...") +with open(output_file, 'w') as f: + f.write(html_out) + +print("Done!") + + +######################################################################## +######################################################################## +# USING DATAFRAME TO HTML +######################################################################## +######################################################################## + + +# # Convert the array to a DataFrame +# df = pd.DataFrame(data) +# print("Converted data to DataFrame") + +# # Convert the DataFrame to an HTML table +# html_table = df.to_html(index=False) +# print("Converted DataFrame to HTML table") + +# # Get the absolute path to the directory containing the script +# script_dir = os.path.dirname(os.path.abspath(__file__)) + +# # Construct the absolute path to the output file +# output_file = os.path.join(script_dir, '../report_store/output.html') + + +# # Write the HTML table to the output file +# print("Writing HTML table to output.html...") +# with open(output_file, 'w') as f: +# f.write(html_table) + +# print("Done!") \ No newline at end of file diff --git a/analytics/report/src/report_generation/template/reporttemplate.html b/analytics/report/src/report_generation/template/reporttemplate.html new file mode 100644 index 0000000..e8eb854 --- /dev/null +++ b/analytics/report/src/report_generation/template/reporttemplate.html @@ -0,0 +1,47 @@ + + + + + + + + Open_BC Airbnb regulation report on {{date}} + + + +

Open_BC Airbnb regulation report on {{date}}

+ + + + + + + + + + {% for i in range(data.listing_id|length) %} + + + + + + {% endfor %} + +
listing_idaddresspolicy_A
{{data.listing_id[i]}}{{data.address[i]}}{{data.policy_A[i]}}
+ + \ No newline at end of file diff --git a/analytics/report/src/report_store/output.html b/analytics/report/src/report_store/output.html new file mode 100644 index 0000000..23675ef --- /dev/null +++ b/analytics/report/src/report_store/output.html @@ -0,0 +1,83 @@ + + + + + + + + Open_BC Airbnb regulation report on 2024-02-12 23:12 + + + +

Open_BC Airbnb regulation report on 2024-02-12 23:12

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
listing_idaddresspolicy_A
111 ABC street.True
222 ABC street.False
333 ABC street.True
444 ABC street.False
555 ABC street.True
666 ABC street.False
777 ABC street.True
+ + \ No newline at end of file From ccf66faedc9e30881596934c1f394fc63ea4816b Mon Sep 17 00:00:00 2001 From: Tony-Thawatchai Date: Mon, 12 Feb 2024 23:24:33 -0800 Subject: [PATCH 2/3] comment out pandas --- analytics/report/src/report_generation/report_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analytics/report/src/report_generation/report_generator.py b/analytics/report/src/report_generation/report_generator.py index 713b40a..3ce0052 100644 --- a/analytics/report/src/report_generation/report_generator.py +++ b/analytics/report/src/report_generation/report_generator.py @@ -1,5 +1,5 @@ print("Starting script...") -import pandas as pd +#import pandas as pd import os from jinja2 import Environment, FileSystemLoader From c4997f1f36d60e553d9a33fc03c53993957aeffc Mon Sep 17 00:00:00 2001 From: Tony-Thawatchai Date: Mon, 12 Feb 2024 23:32:25 -0800 Subject: [PATCH 3/3] #15:feat:report generation as HTML --- analytics/report/src/report_generation/report_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analytics/report/src/report_generation/report_generator.py b/analytics/report/src/report_generation/report_generator.py index 3ce0052..dca4ee1 100644 --- a/analytics/report/src/report_generation/report_generator.py +++ b/analytics/report/src/report_generation/report_generator.py @@ -1,5 +1,5 @@ print("Starting script...") -#import pandas as pd +# import pandas as pd import os from jinja2 import Environment, FileSystemLoader