Spaces:
Running
Running
#stats_dashboard.py | |
from config import temp_file_path # Import the global variable | |
def update_dashboard(): | |
total_ppts, total_slides, chart_data, latest_html = get_dashboard_stats() | |
return ( | |
gr.update(visible=True), | |
gr.update(value=f"<div><h3>Total PPTs: {total_ppts}</h3></div>"), | |
gr.update(value=f"<div><h3>Total Slides: {total_slides}</h3></div>"), | |
gr.update(value=chart_data), | |
gr.update(value=latest_html) | |
) | |
import pandas as pd | |
import gradio as gr | |
import os | |
def get_dashboard_stats(): | |
# Load metadata CSV | |
global temp_file_path | |
print('Reading CSV...') | |
#metadata_file_name= "Master_metadata.csv" | |
# df = pd.read_csv(metadata_file_name) | |
#temp_file_path = os.path.join("/tmp", metadata_file_name) | |
df = pd.read_csv(temp_file_path) | |
# Ensure upload_date column is in datetime format | |
df["Upload_date"] = pd.to_datetime(df["Upload_date"], errors="coerce") | |
print(df) | |
# Total unique PPTs and slides | |
total_ppts = df["PPT_Unique_ID"].nunique() | |
total_slides = len(df) | |
# Monthly PPT uploads | |
df["month_year"] = df["Upload_date"].dt.to_period("M").astype(str) | |
monthly_stats = df.groupby("month_year")["PPT_Unique_ID"].nunique().reset_index() | |
monthly_stats.columns = ["Month", "PPT Uploads"] | |
# Gradio BarPlot requires a DataFrame | |
chart_data = monthly_stats | |
# Latest 5 PPTs by upload date | |
latest_df = df.drop_duplicates(subset="PPT_Unique_ID").sort_values("Upload_date", ascending=False) | |
latest_5 = latest_df[["Suitable_Title", "Slide_Category","Upload_date"]].head(5) | |
# Create HTML for the latest PPTs list | |
# Create HTML for the latest PPTs list with heading | |
latest_html = "<h4 style='margin-bottom: 8px;'>π Top 5 Latest Uploaded PPTs</h4><ul style='line-height:1.6em;'>" | |
for _, row in latest_5.iterrows(): | |
title = row["Suitable_Title"] | |
category = row["Slide_Category"] | |
date_str = row["upload_date"].strftime("%Y-%m-%d") if pd.notnull(row["upload_date"]) else "Unknown Date" | |
latest_html += f"<li><b>{title}</b> <br><i>{category}</i> β <span style='color:gray;'>{date_str}</span></li>" | |
latest_html += "</ul>" | |
return total_ppts, total_slides, chart_data, latest_html |