klydekushy commited on
Commit
aad803d
·
verified ·
1 Parent(s): 55b4938

Update src/modules/ml_dashboard.py

Browse files
Files changed (1) hide show
  1. src/modules/ml_dashboard.py +38 -37
src/modules/ml_dashboard.py CHANGED
@@ -183,38 +183,16 @@ def show_ml_features(client, sheet_name):
183
  df_2026 = df_prets_master[df_prets_master['Date_Deblocage_dt'].dt.year == 2026].copy()
184
 
185
  if not df_2026.empty:
186
- # Extraire le mois
187
- df_2026['Mois'] = df_2026['Date_Deblocage_dt'].dt.month
188
- df_2026['Mois_Nom'] = df_2026['Date_Deblocage_dt'].dt.strftime('%B')
 
189
 
190
- # Agrégation par mois
191
- benefices_mensuels = df_2026.groupby('Mois')['Cout_Credit_Num'].sum().reset_index()
192
- benefices_mensuels.columns = ['Mois', 'Benefice']
193
 
194
- # Créer un DataFrame complet pour tous les mois (1-12)
195
- mois_complet = pd.DataFrame({
196
- 'Mois': range(1, 13),
197
- 'Mois_Nom': ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin',
198
- 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']
199
- })
200
-
201
- # Fusion pour avoir tous les mois (même ceux à 0)
202
- benefices_mensuels = mois_complet.merge(benefices_mensuels, on='Mois', how='left')
203
- benefices_mensuels['Benefice'] = benefices_mensuels['Benefice'].fillna(0)
204
-
205
- # Calcul des variations mensuelles (%)
206
- benefices_mensuels['Variation'] = 0.0
207
- for i in range(1, len(benefices_mensuels)):
208
- mois_precedent = benefices_mensuels.loc[i-1, 'Benefice']
209
- mois_actuel = benefices_mensuels.loc[i, 'Benefice']
210
-
211
- if mois_precedent > 0:
212
- variation = ((mois_actuel - mois_precedent) / mois_precedent) * 100
213
- benefices_mensuels.loc[i, 'Variation'] = variation
214
- elif mois_actuel > 0:
215
- benefices_mensuels.loc[i, 'Variation'] = 100.0
216
-
217
- # Graphique en barres avec Plotly (pleine largeur)
218
  fig_mensuel = go.Figure()
219
 
220
  # Barres des bénéfices
@@ -228,15 +206,31 @@ def show_ml_features(client, sheet_name):
228
  ),
229
  text=benefices_mensuels['Benefice'].apply(lambda x: f"{x:,.0f}".replace(',', ' ')),
230
  textposition='outside',
231
- textfont=dict(size=12, color='#c9d1d9', family='Space Grotesk'),
232
  opacity=0.85,
233
  hovertemplate='<b>%{x}</b><br>Bénéfice: %{y:,.0f} XOF<extra></extra>'
234
  ))
235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  # Mise en forme
237
  fig_mensuel.update_layout(
238
  title={
239
- 'text': 'Évolution des bénéfices mensuels (2026)',
240
  'font': {'size': 16, 'color': '#c9d1d9', 'family': 'Space Grotesk'},
241
  'x': 0,
242
  'xanchor': 'left'
@@ -253,20 +247,27 @@ def show_ml_features(client, sheet_name):
253
  },
254
  yaxis={
255
  'title': {
256
- 'text': 'Bénéfices (XOF)',
257
  'font': {'size': 13, 'family': 'Space Grotesk'}
258
  },
259
  'gridcolor': 'rgba(48, 54, 61, 0.3)',
260
  'linecolor': 'rgba(48, 54, 61, 0.5)',
261
  'tickfont': {'family': 'Space Grotesk', 'size': 12}
262
  },
263
- showlegend=False,
 
 
 
 
 
 
 
 
 
264
  height=550,
265
- margin=dict(t=80, b=120, l=80, r=30)
266
  )
267
 
268
- st.plotly_chart(fig_mensuel, use_container_width=True)
269
-
270
  # === TREEMAP DES VARIATIONS MENSUELLES ===
271
  st.markdown("<h3 style='font-size: 1.1rem; color: #8b949e; margin-top: 32px; margin-bottom: 16px;'>Répartition mensuelle des bénéfices</h3>", unsafe_allow_html=True)
272
 
 
183
  df_2026 = df_prets_master[df_prets_master['Date_Deblocage_dt'].dt.year == 2026].copy()
184
 
185
  if not df_2026.empty:
186
+ # Calculer le capital mensuel sorti (Montant_Capital)
187
+ df_2026['Montant_Capital_Num'] = pd.to_numeric(df_2026['Montant_Capital'], errors='coerce').fillna(0)
188
+ capital_mensuel = df_2026.groupby('Mois')['Montant_Capital_Num'].sum().reset_index()
189
+ capital_mensuel.columns = ['Mois', 'Capital_Sorti']
190
 
191
+ # Fusionner avec les bénéfices mensuels
192
+ benefices_mensuels = benefices_mensuels.merge(capital_mensuel, on='Mois', how='left')
193
+ benefices_mensuels['Capital_Sorti'] = benefices_mensuels['Capital_Sorti'].fillna(0)
194
 
195
+ # Graphique en barres groupées avec Plotly (pleine largeur)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  fig_mensuel = go.Figure()
197
 
198
  # Barres des bénéfices
 
206
  ),
207
  text=benefices_mensuels['Benefice'].apply(lambda x: f"{x:,.0f}".replace(',', ' ')),
208
  textposition='outside',
209
+ textfont=dict(size=11, color='#c9d1d9', family='Space Grotesk'),
210
  opacity=0.85,
211
  hovertemplate='<b>%{x}</b><br>Bénéfice: %{y:,.0f} XOF<extra></extra>'
212
  ))
213
 
214
+ # Barres du capital sorti
215
+ fig_mensuel.add_trace(go.Bar(
216
+ x=benefices_mensuels['Mois_Nom'],
217
+ y=benefices_mensuels['Capital_Sorti'],
218
+ name='Capital Sorti',
219
+ marker=dict(
220
+ color='#f85149',
221
+ line=dict(color='rgba(139, 148, 158, 0.3)', width=1.5)
222
+ ),
223
+ text=benefices_mensuels['Capital_Sorti'].apply(lambda x: f"{x:,.0f}".replace(',', ' ')),
224
+ textposition='outside',
225
+ textfont=dict(size=11, color='#c9d1d9', family='Space Grotesk'),
226
+ opacity=0.85,
227
+ hovertemplate='<b>%{x}</b><br>Capital Sorti: %{y:,.0f} XOF<extra></extra>'
228
+ ))
229
+
230
  # Mise en forme
231
  fig_mensuel.update_layout(
232
  title={
233
+ 'text': 'Évolution des bénéfices et capital sorti mensuels (2026)',
234
  'font': {'size': 16, 'color': '#c9d1d9', 'family': 'Space Grotesk'},
235
  'x': 0,
236
  'xanchor': 'left'
 
247
  },
248
  yaxis={
249
  'title': {
250
+ 'text': 'Montant (XOF)',
251
  'font': {'size': 13, 'family': 'Space Grotesk'}
252
  },
253
  'gridcolor': 'rgba(48, 54, 61, 0.3)',
254
  'linecolor': 'rgba(48, 54, 61, 0.5)',
255
  'tickfont': {'family': 'Space Grotesk', 'size': 12}
256
  },
257
+ barmode='group',
258
+ showlegend=True,
259
+ legend=dict(
260
+ orientation='h',
261
+ yanchor='bottom',
262
+ y=1.02,
263
+ xanchor='right',
264
+ x=1,
265
+ font={'family': 'Space Grotesk', 'size': 12, 'color': '#c9d1d9'}
266
+ ),
267
  height=550,
268
+ margin=dict(t=100, b=120, l=80, r=30)
269
  )
270
 
 
 
271
  # === TREEMAP DES VARIATIONS MENSUELLES ===
272
  st.markdown("<h3 style='font-size: 1.1rem; color: #8b949e; margin-top: 32px; margin-bottom: 16px;'>Répartition mensuelle des bénéfices</h3>", unsafe_allow_html=True)
273