Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from typing import Tuple, List, Union
|
|
| 6 |
import traceback
|
| 7 |
import io
|
| 8 |
import zipfile
|
|
|
|
| 9 |
|
| 10 |
# NTU Singapore colors
|
| 11 |
NTU_BLUE = "#003D7C"
|
|
@@ -150,7 +151,7 @@ def create_scatter_plot(df: pd.DataFrame) -> Union[px.scatter, None]:
|
|
| 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] *
|
| 154 |
|
| 155 |
stats = create_summary_stats(df)
|
| 156 |
stats_md = gr.Markdown("\n".join([f"**{k}**: {v:.2f}" for k, v in stats.items()]))
|
|
@@ -163,14 +164,16 @@ def update_insights(df: pd.DataFrame, zip_data: Union[Tuple[io.BytesIO, str], No
|
|
| 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
|
|
|
|
| 167 |
else:
|
| 168 |
-
download_button = gr.File(visible=False, label="Download Results
|
|
|
|
| 169 |
|
| 170 |
-
return [stats_md, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button]
|
| 171 |
except Exception as e:
|
| 172 |
error_msg = f"Error updating insights: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 173 |
-
return [gr.Markdown(error_msg)] + [None] *
|
| 174 |
|
| 175 |
def process_and_update(file):
|
| 176 |
try:
|
|
@@ -179,10 +182,10 @@ def process_and_update(file):
|
|
| 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()}"
|
| 182 |
-
return [error_msg, "Processing failed"] + [gr.Markdown(error_msg)] + [None] *
|
| 183 |
|
| 184 |
def clear_outputs():
|
| 185 |
-
return [""] * 2 + [None] * 6 # 2 text outputs
|
| 186 |
|
| 187 |
# Create a custom theme
|
| 188 |
custom_theme = gr.themes.Base().set(
|
|
@@ -253,7 +256,10 @@ with gr.Blocks(theme=custom_theme, css=custom_css) as iface:
|
|
| 253 |
|
| 254 |
scatter_plot = gr.Plot()
|
| 255 |
user_table = gr.DataFrame()
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
| 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.")
|
|
@@ -261,13 +267,13 @@ with gr.Blocks(theme=custom_theme, css=custom_css) as iface:
|
|
| 261 |
process_btn.click(
|
| 262 |
process_and_update,
|
| 263 |
inputs=[file_input],
|
| 264 |
-
outputs=[output_msg, csv_location, summary_stats, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button]
|
| 265 |
)
|
| 266 |
|
| 267 |
clear_btn.click(
|
| 268 |
clear_outputs,
|
| 269 |
inputs=[],
|
| 270 |
-
outputs=[output_msg, csv_location, summary_stats, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button]
|
| 271 |
)
|
| 272 |
|
| 273 |
if __name__ == "__main__":
|
|
|
|
| 6 |
import traceback
|
| 7 |
import io
|
| 8 |
import zipfile
|
| 9 |
+
import tempfile
|
| 10 |
|
| 11 |
# NTU Singapore colors
|
| 12 |
NTU_BLUE = "#003D7C"
|
|
|
|
| 151 |
def update_insights(df: pd.DataFrame, zip_data: Union[Tuple[io.BytesIO, str], None]) -> List[Union[gr.components.Component, None]]:
|
| 152 |
try:
|
| 153 |
if df.empty:
|
| 154 |
+
return [gr.Markdown("No data available. Please upload and process a file first.")] + [None] * 6
|
| 155 |
|
| 156 |
stats = create_summary_stats(df)
|
| 157 |
stats_md = gr.Markdown("\n".join([f"**{k}**: {v:.2f}" for k, v in stats.items()]))
|
|
|
|
| 164 |
|
| 165 |
if zip_data:
|
| 166 |
zip_file, zip_name = zip_data
|
| 167 |
+
download_button = gr.File(value=zip_file, filename=zip_name, visible=True, label="Download Results")
|
| 168 |
+
download_text = gr.Markdown("Click the 'Download Results' button above to download the ZIP file containing all processed data.")
|
| 169 |
else:
|
| 170 |
+
download_button = gr.File(visible=False, label="Download Results")
|
| 171 |
+
download_text = gr.Markdown("")
|
| 172 |
|
| 173 |
+
return [stats_md, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button, download_text]
|
| 174 |
except Exception as e:
|
| 175 |
error_msg = f"Error updating insights: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 176 |
+
return [gr.Markdown(error_msg)] + [None] * 6
|
| 177 |
|
| 178 |
def process_and_update(file):
|
| 179 |
try:
|
|
|
|
| 182 |
return [result_msg, csv_loc] + insights
|
| 183 |
except Exception as e:
|
| 184 |
error_msg = f"Error in process_and_update: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 185 |
+
return [error_msg, "Processing failed"] + [gr.Markdown(error_msg)] + [None] * 6
|
| 186 |
|
| 187 |
def clear_outputs():
|
| 188 |
+
return [""] * 2 + [None] * 6 + [""] # 2 text outputs, 6 graph/table/file outputs, and 1 download text
|
| 189 |
|
| 190 |
# Create a custom theme
|
| 191 |
custom_theme = gr.themes.Base().set(
|
|
|
|
| 256 |
|
| 257 |
scatter_plot = gr.Plot()
|
| 258 |
user_table = gr.DataFrame()
|
| 259 |
+
|
| 260 |
+
gr.Markdown("## Download Processed Data")
|
| 261 |
+
download_button = gr.File(visible=False, label="Download Results")
|
| 262 |
+
download_text = gr.Markdown("")
|
| 263 |
|
| 264 |
clear_btn = gr.Button("Clear All Data", variant="secondary")
|
| 265 |
gr.Markdown("Click 'Clear All Data' to reset the application and start over.")
|
|
|
|
| 267 |
process_btn.click(
|
| 268 |
process_and_update,
|
| 269 |
inputs=[file_input],
|
| 270 |
+
outputs=[output_msg, csv_location, summary_stats, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button, download_text]
|
| 271 |
)
|
| 272 |
|
| 273 |
clear_btn.click(
|
| 274 |
clear_outputs,
|
| 275 |
inputs=[],
|
| 276 |
+
outputs=[output_msg, csv_location, summary_stats, users_activity_chart, users_courses_chart, scatter_plot, user_table, download_button, download_text]
|
| 277 |
)
|
| 278 |
|
| 279 |
if __name__ == "__main__":
|