simonraj commited on
Commit
6a99f84
1 Parent(s): 3157d64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -16
app.py CHANGED
@@ -36,10 +36,6 @@ def process_data(file: gr.File, progress=gr.Progress()) -> Tuple[str, str, pd.Da
36
 
37
  # Extract filename without extension
38
  base_filename = os.path.splitext(os.path.basename(file.name))[0]
39
-
40
- # Define output paths
41
- final_file_path = f'mailmerge {base_filename}.xlsx'
42
- base_path = 'mailmerge'
43
 
44
  # Step 1: Extract User Information
45
  user_info = raw_data[['user_id', 'lastname']].drop_duplicates().copy()
@@ -63,9 +59,9 @@ def process_data(file: gr.File, progress=gr.Progress()) -> Tuple[str, str, pd.Da
63
 
64
  progress(0.6, desc="Calculating grand totals")
65
 
66
- # Step 4: Generate Filenames and Paths
67
  user_info['File'] = 'User_' + user_info['Username'] + '_data.csv'
68
- user_info['Path'] = user_info['File'].apply(lambda x: os.path.join(base_path, x))
69
 
70
  # Remove extra columns and summary rows
71
  user_info = user_info[['Username', 'Name', 'Courses', 'Grand Total', 'Email', 'File', 'Path']]
@@ -73,13 +69,7 @@ def process_data(file: gr.File, progress=gr.Progress()) -> Tuple[str, str, pd.Da
73
  user_info.drop_duplicates(subset=['Username'], inplace=True)
74
  user_info.sort_values(by='Username', inplace=True)
75
 
76
- progress(0.8, desc="Generating individual CSV files")
77
-
78
- # Generate individual CSV files for each user
79
- required_columns = ['course_id', 'course_pk1', 'data', 'event_type', 'internal_handle', 'lastname', 'session_id', 'timestamp', 'user_id', 'system_role']
80
- missing_columns = [col for col in required_columns if col not in raw_data.columns]
81
- if missing_columns:
82
- raise ValueError(f"Missing columns for individual CSV files: {', '.join(missing_columns)}")
83
 
84
  # Create a BytesIO object to store the zip file
85
  zip_buffer = io.BytesIO()
@@ -88,7 +78,7 @@ def process_data(file: gr.File, progress=gr.Progress()) -> Tuple[str, str, pd.Da
88
  # Save individual CSV files
89
  for user_id in user_info['Username'].unique():
90
  user_data = raw_data[raw_data['user_id'] == user_id][required_columns]
91
- user_file_path = f'{base_path}/User_{user_id}_data.csv'
92
  zip_file.writestr(user_file_path, user_data.to_csv(index=False))
93
 
94
  # Save the final Excel file
@@ -103,7 +93,7 @@ def process_data(file: gr.File, progress=gr.Progress()) -> Tuple[str, str, pd.Da
103
  worksheet.write(f'C{last_row + 1}', user_info['Courses'].sum())
104
  worksheet.write(f'D{last_row + 1}', user_info['Grand Total'].sum())
105
 
106
- zip_file.writestr(final_file_path, excel_buffer.getvalue())
107
 
108
  zip_buffer.seek(0)
109
  progress(1.0, desc="Processing complete")
@@ -171,7 +161,10 @@ def update_insights(df: pd.DataFrame, zip_file: Union[io.BytesIO, None]) -> List
171
 
172
  user_table = gr.DataFrame(value=df)
173
 
174
- download_button = gr.File(value=zip_file, visible=(zip_file is not None), label="Download Results")
 
 
 
175
 
176
  return [stats_md, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button]
177
  except Exception as e:
 
36
 
37
  # Extract filename without extension
38
  base_filename = os.path.splitext(os.path.basename(file.name))[0]
 
 
 
 
39
 
40
  # Step 1: Extract User Information
41
  user_info = raw_data[['user_id', 'lastname']].drop_duplicates().copy()
 
59
 
60
  progress(0.6, desc="Calculating grand totals")
61
 
62
+ # Step 4: Generate Filenames and Paths (for reference only, not creating actual files)
63
  user_info['File'] = 'User_' + user_info['Username'] + '_data.csv'
64
+ user_info['Path'] = 'mailmerge/' + user_info['File']
65
 
66
  # Remove extra columns and summary rows
67
  user_info = user_info[['Username', 'Name', 'Courses', 'Grand Total', 'Email', 'File', 'Path']]
 
69
  user_info.drop_duplicates(subset=['Username'], inplace=True)
70
  user_info.sort_values(by='Username', inplace=True)
71
 
72
+ progress(0.8, desc="Generating output files")
 
 
 
 
 
 
73
 
74
  # Create a BytesIO object to store the zip file
75
  zip_buffer = io.BytesIO()
 
78
  # Save individual CSV files
79
  for user_id in user_info['Username'].unique():
80
  user_data = raw_data[raw_data['user_id'] == user_id][required_columns]
81
+ user_file_path = f'mailmerge/User_{user_id}_data.csv'
82
  zip_file.writestr(user_file_path, user_data.to_csv(index=False))
83
 
84
  # Save the final Excel file
 
93
  worksheet.write(f'C{last_row + 1}', user_info['Courses'].sum())
94
  worksheet.write(f'D{last_row + 1}', user_info['Grand Total'].sum())
95
 
96
+ zip_file.writestr(f'mailmerge {base_filename}.xlsx', excel_buffer.getvalue())
97
 
98
  zip_buffer.seek(0)
99
  progress(1.0, desc="Processing complete")
 
161
 
162
  user_table = gr.DataFrame(value=df)
163
 
164
+ if zip_file:
165
+ download_button = gr.File(value=zip_file, filename="gradebook_results.zip", visible=True, label="Download Results")
166
+ else:
167
+ download_button = gr.File(visible=False, label="Download Results")
168
 
169
  return [stats_md, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button]
170
  except Exception as e: