adding formatting to excel
Browse files- utils/convert_to_excel.py +137 -13
utils/convert_to_excel.py
CHANGED
@@ -4,26 +4,150 @@ import time
|
|
4 |
import pandas as pd
|
5 |
import streamlit as st
|
6 |
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
# IMPORTANT: Cache the conversion to prevent computation on every rerun
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
with pd.ExcelWriter(bytes_io, engine="xlsxwriter") as writer:
|
17 |
-
for df,
|
18 |
-
df.
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
# Get the bytes data
|
21 |
-
bytes_data = bytes_io.getvalue()
|
22 |
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
def save_dataframe(df: pd.DataFrame, sheet_name: str):
|
|
|
4 |
import pandas as pd
|
5 |
import streamlit as st
|
6 |
|
7 |
+
# @st.cache_data
|
8 |
+
# def convert_dfs(dfs: list[pd.DataFrame], sheet_names: list[str]) -> bytes:
|
9 |
+
# # IMPORTANT: Cache the conversion to prevent computation on every rerun
|
10 |
|
11 |
+
# # Create a BytesIO object
|
12 |
+
# bytes_io = io.BytesIO()
|
|
|
13 |
|
14 |
+
# # Write the dataframes to the BytesIO object
|
15 |
+
# with pd.ExcelWriter(bytes_io, engine="xlsxwriter") as writer:
|
16 |
+
# for df, sheet_name in zip(dfs, sheet_names):
|
17 |
+
# df.to_excel(writer, sheet_name=sheet_name, index=True)
|
18 |
+
|
19 |
+
# # Get the bytes data
|
20 |
+
# bytes_data = bytes_io.getvalue()
|
21 |
+
|
22 |
+
# # Close the BytesIO object
|
23 |
+
# bytes_io.close()
|
24 |
+
|
25 |
+
# return bytes_data
|
26 |
+
|
27 |
+
|
28 |
+
def get_formats(workbook):
|
29 |
+
return {
|
30 |
+
"green": workbook.add_format(
|
31 |
+
{"bg_color": "#37CC73", "bold": True, "border": 1}
|
32 |
+
),
|
33 |
+
"blue": workbook.add_format({"bg_color": "#1A64FF", "bold": True, "border": 1}),
|
34 |
+
"blue_light": workbook.add_format(
|
35 |
+
{"bg_color": "#00B0F0", "bold": True, "border": 1}
|
36 |
+
),
|
37 |
+
"beurre": workbook.add_format(
|
38 |
+
{"bg_color": "#FFE699", "bold": True, "border": 1}
|
39 |
+
),
|
40 |
+
"orange": workbook.add_format(
|
41 |
+
{"bg_color": "#F47F31", "bold": True, "border": 1}
|
42 |
+
),
|
43 |
+
"purple5": workbook.add_format(
|
44 |
+
{"bg_color": "#E03DCD", "bold": True, "border": 1}
|
45 |
+
),
|
46 |
+
"purple6": workbook.add_format(
|
47 |
+
{"bg_color": "#AE83F8", "bold": True, "border": 1}
|
48 |
+
),
|
49 |
+
"gray": workbook.add_format({"bg_color": "#D9D9D9", "bold": True, "border": 1}),
|
50 |
+
"red": workbook.add_format({"bg_color": "#FF0000", "bold": True, "border": 1}),
|
51 |
+
}
|
52 |
+
|
53 |
+
|
54 |
+
def get_format_map_by_format_type(formats: dict, format_type: str) -> dict:
|
55 |
+
if format_type == "GSM_Analysis":
|
56 |
+
return {
|
57 |
+
# "name": formats["blue"],
|
58 |
+
"amrSegLoadDepTchRateLower": formats["beurre"],
|
59 |
+
"amrSegLoadDepTchRateUpper": formats["beurre"],
|
60 |
+
"dedicatedGPRScapacity": formats["beurre"],
|
61 |
+
"defaultGPRScapacity": formats["beurre"],
|
62 |
+
"number_trx_per_cell": formats["blue_light"],
|
63 |
+
"number_trx_per_bcf": formats["blue_light"],
|
64 |
+
"number_tch_per_cell": formats["blue"],
|
65 |
+
"number_sd_per_cell": formats["blue"],
|
66 |
+
"number_bcch_per_cell": formats["blue"],
|
67 |
+
"number_ccch_per_cell": formats["blue"],
|
68 |
+
"number_cbc_per_cell": formats["blue"],
|
69 |
+
"number_total_channels_per_cell": formats["blue"],
|
70 |
+
"number_signals_per_cell": formats["blue"],
|
71 |
+
"hf_rate_coef": formats["purple5"],
|
72 |
+
"GPRS": formats["purple5"],
|
73 |
+
"TCH Actual HR%": formats["green"],
|
74 |
+
"Offered Traffic BH": formats["green"],
|
75 |
+
"Max_Traffic BH": formats["green"],
|
76 |
+
"Avg_Traffic BH": formats["green"],
|
77 |
+
"Max_tch_call_blocking BH": formats["green"],
|
78 |
+
"Avg_tch_call_blocking BH": formats["green"],
|
79 |
+
"number_of_days_with_tch_blocking_exceeded": formats["green"],
|
80 |
+
"Max_sdcch_real_blocking BH": formats["green"],
|
81 |
+
"Avg_sdcch_real_blocking BH": formats["green"],
|
82 |
+
"number_of_days_with_sdcch_blocking_exceeded": formats["green"],
|
83 |
+
"TCH UTILIZATION (@Max Traffic)": formats["orange"],
|
84 |
+
"Target FR CHs": formats["purple6"],
|
85 |
+
"Target HR CHs": formats["purple6"],
|
86 |
+
"Target TCHs": formats["purple6"],
|
87 |
+
"Target TRXs": formats["purple6"],
|
88 |
+
"Numberof required TRXs": formats["purple6"],
|
89 |
+
}
|
90 |
+
elif format_type == "database":
|
91 |
+
return {
|
92 |
+
"code": formats["blue"],
|
93 |
+
"Azimut": formats["green"],
|
94 |
+
"Longitude": formats["green"],
|
95 |
+
"Latitude": formats["green"],
|
96 |
+
"Hauteur": formats["green"],
|
97 |
+
}
|
98 |
+
# elif format_type == "LTE":
|
99 |
+
# return {
|
100 |
+
# "DL PRB Utilization": formats["orange"],
|
101 |
+
# "UL PRB Utilization": formats["orange"],
|
102 |
+
# "RSRP": formats["blue_light"],
|
103 |
+
# "RSRQ": formats["blue_light"],
|
104 |
+
# "Throughput (Mbps)": formats["green"],
|
105 |
+
# }
|
106 |
+
else:
|
107 |
+
return {} # No formatting if format_type not matched
|
108 |
+
|
109 |
+
|
110 |
+
def _apply_custom_formatting(
|
111 |
+
writer, df: pd.DataFrame, sheet_name: str, format_type: str
|
112 |
+
):
|
113 |
+
workbook = writer.book
|
114 |
+
worksheet = writer.sheets[sheet_name]
|
115 |
|
116 |
+
formats = get_formats(workbook)
|
117 |
+
format_map = get_format_map_by_format_type(formats, format_type)
|
118 |
+
|
119 |
+
for col_idx, col_name in enumerate(df.columns):
|
120 |
+
fmt = format_map.get(col_name)
|
121 |
+
if fmt:
|
122 |
+
worksheet.write(0, col_idx + 1, col_name, fmt)
|
123 |
+
|
124 |
+
|
125 |
+
def _write_to_excel(
|
126 |
+
dfs: list[pd.DataFrame], sheet_names: list[str], index=True, format_type: str = None
|
127 |
+
) -> bytes:
|
128 |
+
bytes_io = io.BytesIO()
|
129 |
with pd.ExcelWriter(bytes_io, engine="xlsxwriter") as writer:
|
130 |
+
for df, name in zip(dfs, sheet_names):
|
131 |
+
# df.index.name = "index"
|
132 |
+
df.to_excel(writer, sheet_name=name, index=index)
|
133 |
+
if format_type:
|
134 |
+
_apply_custom_formatting(writer, df, name, format_type)
|
135 |
+
return bytes_io.getvalue()
|
136 |
|
|
|
|
|
137 |
|
138 |
+
@st.cache_data
|
139 |
+
def convert_dfs(dfs: list[pd.DataFrame], sheet_names: list[str]) -> bytes:
|
140 |
+
return _write_to_excel(dfs, sheet_names, index=True)
|
141 |
|
142 |
+
|
143 |
+
@st.cache_data
|
144 |
+
def convert_gsm_dfs(dfs, sheet_names) -> bytes:
|
145 |
+
return _write_to_excel(dfs, sheet_names, index=True, format_type="GSM_Analysis")
|
146 |
+
|
147 |
+
|
148 |
+
@st.cache_data
|
149 |
+
def convert_database_dfs(dfs, sheet_names) -> bytes:
|
150 |
+
return _write_to_excel(dfs, sheet_names, index=True, format_type="database")
|
151 |
|
152 |
|
153 |
def save_dataframe(df: pd.DataFrame, sheet_name: str):
|