Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 45e1449

Browse files
committed
Add comments on main script to make charts and excel report
1 parent fdbec65 commit 45e1449

1 file changed

Lines changed: 41 additions & 37 deletions

File tree

labonneboite/scripts/impact_retour_emploi/make_report.py

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1818

19+
# FIXME : Refacto all files about the creation of charts and pasting on sheets (<3 Joris)
20+
21+
1922
def get_infos_from_sql():
2023
#Get all joined activity logs and dpae CLEAN and NO DUPLICATES
2124
engine = import_util.create_sqlalchemy_engine()
@@ -65,54 +68,35 @@ def get_infos_from_sql():
6568

6669
def make_charts(df_act_dpae, idpe_connect, total_idpe_connect, total_idpe_connect_sign):
6770

68-
# Creation of all directory needed : We will put everything in /srv/lbb/data
71+
# Creation of all directory needed : We will put everything in /srv/lbb/data on the server
6972
shutil.rmtree(root_path,ignore_errors=True)
7073
folders_to_create = [root_path, images_path, gd_pub_path, clean_path]
7174
for folder in folders_to_create:
7275
makedirs(folder)
7376

74-
# Names of different legend for Cohortes
75-
all_the_names_1 = ("Total from activity", "Nbre_Total_DPAE", "Mois d'activité",
76-
"Mois d'embauche", "Origine de l'Activité, en fonction du mois d'Embauche")
77-
all_the_names_2 = ('Nbre_Total_DPAE', "Total from activity", "Mois d'embauche",
78-
"Mois d'activité", "Nombres et Mois d'Embauche, en fonction du mois d'Activité")
79-
80-
81-
def location(num_image, file_name, link=False): # Pasting of pictures
82-
if link is True:
83-
ws.merge_cells("A21:F21")
84-
ws.merge_cells("G21:L21")
85-
ws.merge_cells("M21:R21")
86-
87-
num_image += 3
88-
list_abscisses = ["A", "G", "M"]
89-
if "cohorte" in file_name:
90-
list_abscisses = ["A", "G"]
91-
y = (((num_image//len(list_abscisses)))*20)+1
92-
x = list_abscisses[num_image % len(list_abscisses)]
93-
return x+str(y)
94-
9577
# Write Datas and Charts in Excel :
96-
97-
# Initialisation
78+
# -------------------------------------
79+
# Initialisation of the excel sheets
9880
sheet_names = [None, "BoxPlot + Graph",
9981
"Détail Embauches", "Pie Charts", "Map", "Cohortes"]
10082
sheet_sizes = [None, 5, 2, 2, 3] # Number of files per sheet (start at 0)
10183
num_sheet = 1
10284

103-
# Writes raw data
85+
# First sheet : paste raw datas of the dataframe
86+
# -------------------------------------
10487
wb = openpyxl.Workbook()
10588
wb.save(root_path+'Temporaire.xlsx')
106-
temporaire_df = pd.ExcelWriter(root_path+'Temporaire.xlsx', engine='xlsxwriter')
107-
df_act_dpae.to_excel(temporaire_df, 'DPAE', index=False)
108-
temporaire_df.save()
89+
df_tmp = pd.ExcelWriter(root_path+'Temporaire.xlsx', engine='xlsxwriter')
90+
df_act_dpae.to_excel(df_tmp, 'DPAE', index=False)
91+
df_tmp.save()
10992
book = openpyxl.load_workbook(root_path+'Temporaire.xlsx', data_only=True)
11093

111-
# Extend columns
94+
# Enlarge the columns of the excel document first sheet
11295
for i in range(len(df_act_dpae.columns.tolist())):
11396
book.active.column_dimensions[ALPHABET[i]].width = 20
11497

115-
# Past of graphics/maps/Pie etc...
98+
# next sheets : paste different graphics/maps/Pie/charts etc...
99+
# ------------------------
116100
book.create_sheet(sheet_names[num_sheet])
117101
ws = book.worksheets[num_sheet]
118102
dict_charts = {('01', "diff_activite_embauche_jrs", "Nombre de Jours entre l'activite sur lbb et la DPAE", "act_emb", 1): charts.BoxPlot,
@@ -127,11 +111,14 @@ def location(num_image, file_name, link=False): # Pasting of pictures
127111
('12', "code_postal", "Part des DPAE par anciennes régions", "old_region_gd_public_svg", "old_region"): fr.map_fr,
128112
('13', "code_postal", "Part des DPAE par nouvelles régions", "new_region_gd_public_svg", "new_region"): fr.map_fr,
129113
('14', "code_postal", "Part des DPAE par département", "dep_gd_public_svg", "departement"): fr.map_fr,
130-
('15', 'date_embauche', all_the_names_1, 'cohorte_1_gd_public', 'date_activite'): charts.Stacked_Bar,
131-
('16', 'date_activite', all_the_names_2, 'cohorte_2_gd_public', 'date_embauche'): charts.Stacked_Bar}
114+
('15', 'date_embauche', ("Total from activity", "Nbre_Total_DPAE", "Mois d'activité",
115+
"Mois d'embauche", "Origine de l'Activité, en fonction du mois d'Embauche"), 'cohorte_1_gd_public', 'date_activite'): charts.Stacked_Bar,
116+
('16', 'date_activite', ('Nbre_Total_DPAE', "Total from activity", "Mois d'embauche",
117+
"Mois d'activité", "Nombres et Mois d'Embauche, en fonction du mois d'Activité"), 'cohorte_2_gd_public', 'date_embauche'): charts.Stacked_Bar}
132118

133119

134-
####Boucle ? #################################################################################
120+
# Add some important numbers on the first sheet
121+
#------------------------------------------
135122
# Add number of DPAE with LBB activity
136123
nbre_DPAE = df_act_dpae['date_activite'].describe().tolist()[0]
137124
ws.merge_cells('A4:F4')
@@ -180,29 +167,46 @@ def location(num_image, file_name, link=False): # Pasting of pictures
180167
IDPE_sign_for_gd_pub = [cell_A10.value,
181168
nbre_IDPE_sign, cell_A11.value] # for grand_public
182169

183-
##################################################################################################
184170

185171
num_im = 1
186172
package_svg = []
187173
all_stats = []
188174

189-
for args in dict_charts: # Creation and saving of charts, using charts.py
175+
# Creation and saving of charts, using charts.py
176+
#-----------------------------
177+
for args in dict_charts:
190178
if 'sql' in dict_charts[args].__name__: # choose data
191179
data = idpe_connect
192180
else:
193181
data = df_act_dpae
194182

195183
# Creation of charts/maps in directory "images/"
196184
image = dict_charts[args](
197-
args[0], args[1], data, args[2], args[3], args[4]) # function
185+
args[0], args[1], data, args[2], args[3], args[4]) # a chart function is related to each set of args
198186

199-
# if sem in args, function return the number of graph by "week". It means that create a new sheet is necessary
187+
# FIXME : ??? What ???
188+
# if sem in args, function return the number of graph by "week". It means that creating a new sheet is necessary
200189
# if sem not in args AND image is not None, it means that the function return a list of stats (for gd_pub sheet)
201190
if "sem" in args[3]:
202191
sheet_sizes.insert(2, image-1)
203192
elif image is not None:
204193
all_stats.append(image)
205194

195+
# Pasting of pictures
196+
def location(num_image, file_name, link=False):
197+
if link is True:
198+
ws.merge_cells("A21:F21")
199+
ws.merge_cells("G21:L21")
200+
ws.merge_cells("M21:R21")
201+
202+
num_image += 3
203+
list_abscisses = ["A", "G", "M"]
204+
if "cohorte" in file_name:
205+
list_abscisses = ["A", "G"]
206+
y = (((num_image//len(list_abscisses)))*20)+1
207+
x = list_abscisses[num_image % len(list_abscisses)]
208+
return x+str(y)
209+
206210
# Iterate through the created images
207211
# Pasting of charts from the directory
208212
for filename in sorted(listdir(images_path)):

0 commit comments

Comments
 (0)