Spaces:
Sleeping
Sleeping
Update app.py
Browse filesZip file to download
app.py
CHANGED
@@ -12,7 +12,7 @@ NTU_BLUE = "#003D7C"
|
|
12 |
NTU_RED = "#C11E38"
|
13 |
NTU_GOLD = "#E7B820"
|
14 |
|
15 |
-
def process_data(file: gr.File, progress=gr.Progress()) -> Tuple[str, str, pd.DataFrame, Union[io.BytesIO, None]]:
|
16 |
try:
|
17 |
# Check if file is uploaded
|
18 |
if file is None:
|
@@ -97,7 +97,7 @@ def process_data(file: gr.File, progress=gr.Progress()) -> Tuple[str, str, pd.Da
|
|
97 |
|
98 |
zip_buffer.seek(0)
|
99 |
progress(1.0, desc="Processing complete")
|
100 |
-
return "Processing complete. You can now download the results.", "Results are packaged in the zip file below.", user_info, zip_buffer
|
101 |
except Exception as e:
|
102 |
error_msg = f"Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
103 |
return error_msg, "Processing failed", pd.DataFrame(), None
|
@@ -147,7 +147,7 @@ def create_scatter_plot(df: pd.DataFrame) -> Union[px.scatter, None]:
|
|
147 |
print(f"Error creating scatter plot: {str(e)}")
|
148 |
return None
|
149 |
|
150 |
-
def update_insights(df: pd.DataFrame,
|
151 |
try:
|
152 |
if df.empty:
|
153 |
return [gr.Markdown("No data available. Please upload and process a file first.")] + [None] * 5
|
@@ -161,10 +161,11 @@ def update_insights(df: pd.DataFrame, zip_file: Union[io.BytesIO, None]) -> List
|
|
161 |
|
162 |
user_table = gr.DataFrame(value=df)
|
163 |
|
164 |
-
if
|
165 |
-
|
|
|
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:
|
@@ -173,8 +174,8 @@ def update_insights(df: pd.DataFrame, zip_file: Union[io.BytesIO, None]) -> List
|
|
173 |
|
174 |
def process_and_update(file):
|
175 |
try:
|
176 |
-
result_msg, csv_loc, df,
|
177 |
-
insights = update_insights(df,
|
178 |
return [result_msg, csv_loc] + insights
|
179 |
except Exception as e:
|
180 |
error_msg = f"Error in process_and_update: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
@@ -252,7 +253,7 @@ with gr.Blocks(theme=custom_theme, css=custom_css) as iface:
|
|
252 |
|
253 |
scatter_plot = gr.Plot()
|
254 |
user_table = gr.DataFrame()
|
255 |
-
download_button = gr.File(visible=False, label="Download Results")
|
256 |
|
257 |
clear_btn = gr.Button("Clear All Data", variant="secondary")
|
258 |
gr.Markdown("Click 'Clear All Data' to reset the application and start over.")
|
|
|
12 |
NTU_RED = "#C11E38"
|
13 |
NTU_GOLD = "#E7B820"
|
14 |
|
15 |
+
def process_data(file: gr.File, progress=gr.Progress()) -> Tuple[str, str, pd.DataFrame, Union[Tuple[io.BytesIO, str], None]]:
|
16 |
try:
|
17 |
# Check if file is uploaded
|
18 |
if file is None:
|
|
|
97 |
|
98 |
zip_buffer.seek(0)
|
99 |
progress(1.0, desc="Processing complete")
|
100 |
+
return "Processing complete. You can now download the results.", "Results are packaged in the zip file below.", user_info, (zip_buffer, f"gradebook_results_{base_filename}.zip")
|
101 |
except Exception as e:
|
102 |
error_msg = f"Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
103 |
return error_msg, "Processing failed", pd.DataFrame(), None
|
|
|
147 |
print(f"Error creating scatter plot: {str(e)}")
|
148 |
return None
|
149 |
|
150 |
+
def update_insights(df: pd.DataFrame, zip_data: Union[Tuple[io.BytesIO, str], None]) -> List[Union[gr.components.Component, None]]:
|
151 |
try:
|
152 |
if df.empty:
|
153 |
return [gr.Markdown("No data available. Please upload and process a file first.")] + [None] * 5
|
|
|
161 |
|
162 |
user_table = gr.DataFrame(value=df)
|
163 |
|
164 |
+
if zip_data:
|
165 |
+
zip_file, zip_name = zip_data
|
166 |
+
download_button = gr.File(value=zip_file, visible=True, label="Download Results (ZIP)")
|
167 |
else:
|
168 |
+
download_button = gr.File(visible=False, label="Download Results (ZIP)")
|
169 |
|
170 |
return [stats_md, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button]
|
171 |
except Exception as e:
|
|
|
174 |
|
175 |
def process_and_update(file):
|
176 |
try:
|
177 |
+
result_msg, csv_loc, df, zip_data = process_data(file)
|
178 |
+
insights = update_insights(df, zip_data)
|
179 |
return [result_msg, csv_loc] + insights
|
180 |
except Exception as e:
|
181 |
error_msg = f"Error in process_and_update: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
|
|
253 |
|
254 |
scatter_plot = gr.Plot()
|
255 |
user_table = gr.DataFrame()
|
256 |
+
download_button = gr.File(visible=False, label="Download Results (ZIP)")
|
257 |
|
258 |
clear_btn = gr.Button("Clear All Data", variant="secondary")
|
259 |
gr.Markdown("Click 'Clear All Data' to reset the application and start over.")
|