-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
147 lines (123 loc) · 3.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import sys
from datetime import date
from pathlib import Path
import sqlite3
import pandas as pd
import plotly.express as px
from fpdf import FPDF
# Defining the plotly template.
plotly_template = "presentation"
current_dir = Path(__file__).parent
database_path = current_dir / "sales.db"
output_dir = current_dir / "output"
output_dir.mkdir(parents=True, exist_ok=True)
# Create a connection to the database
conn = sqlite3.connect(database_path)
# Execute the query and load results into a Pandas DataFrame
query = '''
SELECT sale_date, SUM(total_price) as total_sales
FROM sales
GROUP BY sale_date
ORDER BY sale_date ASC
'''
df = pd.read_sql_query(query, conn)
df["sale_date"] = pd.to_datetime(df["sale_date"])
# Set the sale_date column as the index
df = df.set_index('sale_date')
df_monthly = df.resample('ME').sum()
# Map the month number to short month name
df_monthly['month_name'] = df_monthly.index.strftime('%b')
# Create the Plotly figure with text parameter
fig = px.bar(df_monthly,
x='month_name',
y='total_sales',
template=plotly_template,
text='total_sales')
# Set the layout
fig.update_layout(
title='Total Sales by Month',
xaxis_title='Month',
yaxis_title="Total Sales ($)",
yaxis_tickprefix="$",
)
# fig.show()
fig.write_image(output_dir / "monthly_sales.png",
width=1200,
height=400,
scale=4)
# query to load the results into a Pandas DataFrame
query = '''
SELECT p.product_name, SUM(s.total_price) as total_sales
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY p.product_name
'''
df = pd.read_sql_query(query, conn)
print(df)
# create the Plotly figure with text parameter
fig = px.bar(df,
x='product_name',
y='total_sales',
template=plotly_template,
text='total_sales')
# Setting the layout for sales figure
fig.update_layout(
title="Total Sales by Product",
xaxis_title="Product",
yaxis_title="Total Sales ($)",
yaxis_tickprefix="$",
)
#fig.show()
fig.write_image(output_dir / "product_sales.png",
width=1200,
height=400,
scale=4
)
# Query to load the results into a Pandas DataFrame
query = '''
SELECT c.first_name || ' ' || c.last_name as customer_name, SUM(s.total_price) as total_sales
FROM sales s
JOIN customers c ON s.customer_id = c.customer_id
GROUP BY customer_name
ORDER BY total_sales DESC
LIMIT 10
'''
df = pd.read_sql_query(query, conn)
print(df)
# Now lets create a Plotly figure.
fig = px.bar(df,
x='customer_name',
y='total_sales',
template=plotly_template,
text='total_sales')
# finally I am setting the layout
fig.update_layout(
title='Top Customers by Sales',
xaxis_title="Customer",
yaxis_title="Total Sales ($)",
yaxis_tickprefix="$",
)
# lets take a look at the plot.
# fig.show()
fig.write_image(output_dir / 'customer_sales.png',
width=1200,
height=400,
scale=4)
conn.close()
# Define the font color as RGB values (dark gray)
font_colour = (64, 64, 64)
# Find all PNG files in the output folder
chart_filenames = [str(chart_path) for chart_path in output_dir.glob("*.png")]
# create a pdf document and set the page size
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", "B", 24)
# Add the overall page title.
title = f"Sales Report as of {date.today().strftime('%m/%d/%Y')}"
pdf.set_text_color(*font_colour)
pdf.cell(0, 20, title, align="C", ln=1)
for chart_filename in chart_filenames:
pdf.ln(10)
pdf.image(chart_filename, x=None, y=None, w=pdf.w - 20, h=0)
# Save the PDF document to a file on disk
pdf.output(output_dir / " sales_report.pdf", "F")