Spaces:
Sleeping
Sleeping
File size: 10,834 Bytes
3157d64 6a99f84 3157d64 6a99f84 3157d64 6a99f84 3157d64 6a99f84 3157d64 6a99f84 3157d64 6a99f84 3157d64 81a5ff5 |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
import pandas as pd
import os
import gradio as gr
import plotly.express as px
from typing import Tuple, List, Union
import traceback
import io
import zipfile
# NTU Singapore colors
NTU_BLUE = "#003D7C"
NTU_RED = "#C11E38"
NTU_GOLD = "#E7B820"
def process_data(file: gr.File, progress=gr.Progress()) -> Tuple[str, str, pd.DataFrame, Union[io.BytesIO, None]]:
try:
# Check if file is uploaded
if file is None:
raise ValueError("No file uploaded. Please upload an Excel file.")
# Check file extension
if not file.name.lower().endswith(('.xls', '.xlsx')):
raise ValueError("Invalid file format. Please upload an Excel file (.xls or .xlsx).")
# Load the raw Excel file
try:
raw_data = pd.read_excel(file.name)
except Exception as e:
raise ValueError(f"Error reading Excel file: {str(e)}")
# Check if required columns are present
required_columns = ['user_id', 'lastname', 'course_id']
missing_columns = [col for col in required_columns if col not in raw_data.columns]
if missing_columns:
raise ValueError(f"Missing required columns: {', '.join(missing_columns)}")
# Extract filename without extension
base_filename = os.path.splitext(os.path.basename(file.name))[0]
# Step 1: Extract User Information
user_info = raw_data[['user_id', 'lastname']].drop_duplicates().copy()
user_info['Username'] = user_info['user_id']
user_info['Name'] = user_info['lastname']
user_info['Email'] = user_info['user_id'] + '@ntu.edu.sg'
progress(0.2, desc="Extracting user information")
# Step 2: Calculate Course Count
course_counts = raw_data.groupby('user_id')['course_id'].nunique().reset_index()
course_counts.columns = ['Username', 'Courses']
user_info = user_info.merge(course_counts, on='Username', how='left')
progress(0.4, desc="Calculating course counts")
# Step 3: Calculate Grand Total
event_counts = raw_data.groupby('user_id').size().reset_index(name='Grand Total')
event_counts.columns = ['Username', 'Grand Total']
user_info = user_info.merge(event_counts, on='Username', how='left')
progress(0.6, desc="Calculating grand totals")
# Step 4: Generate Filenames and Paths (for reference only, not creating actual files)
user_info['File'] = 'User_' + user_info['Username'] + '_data.csv'
user_info['Path'] = 'mailmerge/' + user_info['File']
# Remove extra columns and summary rows
user_info = user_info[['Username', 'Name', 'Courses', 'Grand Total', 'Email', 'File', 'Path']]
user_info = user_info[user_info['Username'].notna()]
user_info.drop_duplicates(subset=['Username'], inplace=True)
user_info.sort_values(by='Username', inplace=True)
progress(0.8, desc="Generating output files")
# Create a BytesIO object to store the zip file
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
# Save individual CSV files
for user_id in user_info['Username'].unique():
user_data = raw_data[raw_data['user_id'] == user_id][required_columns]
user_file_path = f'mailmerge/User_{user_id}_data.csv'
zip_file.writestr(user_file_path, user_data.to_csv(index=False))
# Save the final Excel file
excel_buffer = io.BytesIO()
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
user_info.to_excel(writer, index=False, sheet_name='Sheet1')
workbook = writer.book
worksheet = writer.sheets['Sheet1']
last_row = len(user_info) + 1
worksheet.write(f'B{last_row + 1}', 'Total')
worksheet.write(f'C{last_row + 1}', user_info['Courses'].sum())
worksheet.write(f'D{last_row + 1}', user_info['Grand Total'].sum())
zip_file.writestr(f'mailmerge {base_filename}.xlsx', excel_buffer.getvalue())
zip_buffer.seek(0)
progress(1.0, desc="Processing complete")
return "Processing complete. You can now download the results.", "Results are packaged in the zip file below.", user_info, zip_buffer
except Exception as e:
error_msg = f"Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
return error_msg, "Processing failed", pd.DataFrame(), None
def create_summary_stats(df: pd.DataFrame) -> dict:
try:
return {
"Total Users": len(df),
"Total Courses": df['Courses'].sum(),
"Total Activity": df['Grand Total'].sum(),
"Avg Courses per User": df['Courses'].mean(),
"Avg Activity per User": df['Grand Total'].mean()
}
except Exception as e:
return {"Error": f"Failed to create summary stats: {str(e)}"}
def create_bar_chart(df: pd.DataFrame, x: str, y: str, title: str) -> Union[px.bar, None]:
try:
if df.empty:
return None
fig = px.bar(df, x=x, y=y, title=title)
fig.update_layout(
plot_bgcolor='white',
paper_bgcolor='white',
font_color=NTU_BLUE
)
fig.update_traces(marker_color=NTU_BLUE)
return fig
except Exception as e:
print(f"Error creating bar chart: {str(e)}")
return None
def create_scatter_plot(df: pd.DataFrame) -> Union[px.scatter, None]:
try:
if df.empty:
return None
fig = px.scatter(df, x='Courses', y='Grand Total', title='Courses vs. Activity Level',
hover_data=['Username', 'Name'])
fig.update_layout(
plot_bgcolor='white',
paper_bgcolor='white',
font_color=NTU_BLUE
)
fig.update_traces(marker_color=NTU_RED)
return fig
except Exception as e:
print(f"Error creating scatter plot: {str(e)}")
return None
def update_insights(df: pd.DataFrame, zip_file: Union[io.BytesIO, None]) -> List[Union[gr.components.Component, None]]:
try:
if df.empty:
return [gr.Markdown("No data available. Please upload and process a file first.")] + [None] * 5
stats = create_summary_stats(df)
stats_md = gr.Markdown("\n".join([f"**{k}**: {v:.2f}" for k, v in stats.items()]))
users_activity_chart = create_bar_chart(df, 'Username', 'Grand Total', 'User Activity Levels')
users_courses_chart = create_bar_chart(df, 'Username', 'Courses', 'Courses per User')
scatter_plot = create_scatter_plot(df)
user_table = gr.DataFrame(value=df)
if zip_file:
download_button = gr.File(value=zip_file, filename="gradebook_results.zip", visible=True, label="Download Results")
else:
download_button = gr.File(visible=False, label="Download Results")
return [stats_md, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button]
except Exception as e:
error_msg = f"Error updating insights: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
return [gr.Markdown(error_msg)] + [None] * 5
def process_and_update(file):
try:
result_msg, csv_loc, df, zip_file = process_data(file)
insights = update_insights(df, zip_file)
return [result_msg, csv_loc] + insights
except Exception as e:
error_msg = f"Error in process_and_update: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
return [error_msg, "Processing failed"] + [gr.Markdown(error_msg)] + [None] * 5
def clear_outputs():
return [""] * 2 + [None] * 6 # 2 text outputs and 6 graph/table/file outputs
# Create a custom theme
custom_theme = gr.themes.Base().set(
body_background_fill="#E6F3FF",
body_text_color="#003D7C",
button_primary_background_fill="#C11E38",
button_primary_background_fill_hover="#A5192F",
button_primary_text_color="white",
block_title_text_color="#003D7C",
block_label_background_fill="#E6F3FF",
input_background_fill="white",
input_border_color="#003D7C",
input_border_color_focus="#C11E38",
)
# Custom CSS
custom_css = """
.gr-button-secondary {
background-color: #F0F0F0;
color: #003D7C;
border: 1px solid #003D7C;
border-radius: 12px;
padding: 8px 16px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
transition: background-color 0.3s, color 0.3s, border-color 0.3s;
}
.gr-button-secondary:hover {
background-color: #003D7C;
color: white;
border-color: #003D7C;
}
.gr-button-secondary:active {
transform: translateY(1px);
}
.app-title {
color: #003D7C;
font-size: 24px;
font-weight: bold;
text-align: center;
margin-bottom: 20px;
}
"""
with gr.Blocks(theme=custom_theme, css=custom_css) as iface:
gr.Markdown("# Gradebook Data Processor", elem_classes=["app-title"])
with gr.Tabs():
with gr.TabItem("1. File Upload and Processing"):
gr.Markdown("## Step 1: Upload your Excel file and process the data")
file_input = gr.File(label="Upload Excel File")
process_btn = gr.Button("Process Data", variant="primary")
output_msg = gr.Textbox(label="Processing Result")
csv_location = gr.Textbox(label="Output Information")
gr.Markdown("After processing, switch to the 'Data Insights' tab to view results and download files.")
with gr.TabItem("2. Data Insights Dashboard"):
gr.Markdown("## Step 2: Review insights and download results")
summary_stats = gr.Markdown("Upload and process a file to see summary statistics.")
with gr.Row():
users_activity_chart = gr.Plot()
users_courses_chart = gr.Plot()
scatter_plot = gr.Plot()
user_table = gr.DataFrame()
download_button = gr.File(visible=False, label="Download Results")
clear_btn = gr.Button("Clear All Data", variant="secondary")
gr.Markdown("Click 'Clear All Data' to reset the application and start over.")
process_btn.click(
process_and_update,
inputs=[file_input],
outputs=[output_msg, csv_location, summary_stats, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button]
)
clear_btn.click(
clear_outputs,
inputs=[],
outputs=[output_msg, csv_location, summary_stats, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button]
)
if __name__ == "__main__":
iface.launch() |