from dash import Input, Output import plotly.express as px import os from helpers.processor import Processor from helpers.s3 import S3Client from helpers.models import S3Config from global_vars import BUCKET_NAME from app import app from dotenv import load_dotenv load_dotenv(".env") print("*********************c") print(os.getenv("AWS_ENDPOINT_URL_S3")) # Initialisation des clients s3_config = S3Config( bucket_name=BUCKET_NAME, endpoint_url=os.getenv("AWS_ENDPOINT_URL_S3"), access_key=os.getenv("AWS_ACCESS_KEY_ID"), secret_key=os.getenv("AWS_SECRET_ACCESS_KEY") ) s3_client = S3Client(config=s3_config) processor = Processor() @app.callback( [ Output("chapter-dropdown-contrib", "options"), Output("page-dropdown-contrib", "options"), ], Input("chapter-dropdown-contrib", "value"), ) def update_dropdowns(chapter_value): df_contributions = processor.get_contribution_data(s3_client) if df_contributions.empty: return [], [] # chapitres unique chapters = sorted(df_contributions["chapter"].unique()) chapter_options = [{"label": "Tous les chapitres", "value": "all"}] + [ {"label": ch, "value": ch} for ch in chapters ] # Récupération des pages en fonction du chapitre sélectionné if chapter_value and chapter_value != "all": pages = sorted( df_contributions[df_contributions["chapter"] == chapter_value]["page"].unique() ) else: pages = sorted(df_contributions["page"].unique()) page_options = [{"label": "Toutes les pages", "value": "all"}] + [ {"label": p, "value": p} for p in pages ] return chapter_options, page_options @app.callback( [Output("contributions-graph", "figure"), Output("data-summary", "children")], [ Input("chapter-dropdown-contrib", "value"), Input("page-dropdown-contrib", "value"), Input("mode-radio", "value"), ], ) def update_graph(chapter, page, mode): df_contributions = processor.get_contribution_data(s3_client) if df_contributions.empty: return px.bar(title="Aucune donnée disponible."), "Aucune donnée chargée." filtered_df_contributions = df_contributions.copy() if chapter != "all": filtered_df_contributions = filtered_df_contributions[ filtered_df_contributions["chapter"] == chapter ] if page != "all": filtered_df_contributions = filtered_df_contributions[ filtered_df_contributions["page"] == page ] contributions = ( filtered_df_contributions.groupby("user_id").size().reset_index(name="value") ) if mode == "percentage": total = contributions["value"].sum() contributions["value"] = (contributions["value"] / total * 100).round(2) value_label = "Contributions (%)" else: value_label = "Nombre de Contributions" fig = px.bar( contributions, y="user_id", x="value", labels={"user_id": "Contributeur", "value": value_label}, title=f"Résumé des Contributions ({chapter}/{page})", text="value", orientation="h", # Barre horizontale ) fig.update_traces(textposition="outside") fig.update_layout( xaxis_title=value_label, yaxis_title="ID Contributeur", bargap=0.2 ) summary = f"Affichage de {len(filtered_df_contributions)} contributions de {len(contributions)} contributeurs." return fig, summary