Spaces:
Running
Running
File size: 2,290 Bytes
19c190a a10b233 19c190a 43fee5d 5e91c58 19c190a 20b7401 5e91c58 20b7401 42d82e0 20b7401 42d82e0 5e91c58 19c190a 42d82e0 19c190a 859fba7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#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 |