Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,14 +12,14 @@ def upload_csv(file):
|
|
| 12 |
try:
|
| 13 |
# Check if a file was actually uploaded
|
| 14 |
if file is None:
|
| 15 |
-
return ["All"], ["All"], "No file uploaded. Please upload a CSV file."
|
| 16 |
|
| 17 |
# Read the CSV file
|
| 18 |
df = pd.read_csv(file.name)
|
| 19 |
|
| 20 |
# Check if the DataFrame is empty
|
| 21 |
if df.empty:
|
| 22 |
-
return ["All"], ["All"], "The uploaded CSV file is empty."
|
| 23 |
|
| 24 |
# Define required columns
|
| 25 |
required_columns = {'DeviceID', 'Lab', 'Type', 'Timestamp', 'Status', 'UsageCount'}
|
|
@@ -27,16 +27,17 @@ def upload_csv(file):
|
|
| 27 |
# Check if all required columns are present
|
| 28 |
if not required_columns.issubset(df.columns):
|
| 29 |
missing_cols = required_columns - set(df.columns)
|
| 30 |
-
return ["All"], ["All"], f"CSV is missing required columns: {', '.join(missing_cols)}"
|
| 31 |
|
| 32 |
# Extract unique values for dropdowns
|
| 33 |
labs = ['All'] + sorted(df['Lab'].dropna().unique().tolist())
|
| 34 |
types = ['All'] + sorted(df['Type'].dropna().unique().tolist())
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
# Handle any other errors (e.g., file not readable, invalid CSV format)
|
| 39 |
-
return ["All"], ["All"], f"Failed to load CSV: {str(e)}"
|
| 40 |
|
| 41 |
def filter_and_plot(selected_lab, selected_type):
|
| 42 |
global df
|
|
@@ -124,8 +125,12 @@ with gr.Blocks() as demo:
|
|
| 124 |
download_btn = gr.Button("Download PDF Summary")
|
| 125 |
error_box = gr.Textbox(label="Status/Error Message", visible=True, interactive=False)
|
| 126 |
|
| 127 |
-
# Connect the components
|
| 128 |
-
csv_input.change(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
lab_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 130 |
type_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 131 |
download_btn.click(fn=download_pdf, inputs=[lab_dropdown, type_dropdown], outputs=gr.File(label="Download PDF"))
|
|
|
|
| 12 |
try:
|
| 13 |
# Check if a file was actually uploaded
|
| 14 |
if file is None:
|
| 15 |
+
return ["All"], ["All"], "No file uploaded. Please upload a CSV file.", "All", "All"
|
| 16 |
|
| 17 |
# Read the CSV file
|
| 18 |
df = pd.read_csv(file.name)
|
| 19 |
|
| 20 |
# Check if the DataFrame is empty
|
| 21 |
if df.empty:
|
| 22 |
+
return ["All"], ["All"], "The uploaded CSV file is empty.", "All", "All"
|
| 23 |
|
| 24 |
# Define required columns
|
| 25 |
required_columns = {'DeviceID', 'Lab', 'Type', 'Timestamp', 'Status', 'UsageCount'}
|
|
|
|
| 27 |
# Check if all required columns are present
|
| 28 |
if not required_columns.issubset(df.columns):
|
| 29 |
missing_cols = required_columns - set(df.columns)
|
| 30 |
+
return ["All"], ["All"], f"CSV is missing required columns: {', '.join(missing_cols)}", "All", "All"
|
| 31 |
|
| 32 |
# Extract unique values for dropdowns
|
| 33 |
labs = ['All'] + sorted(df['Lab'].dropna().unique().tolist())
|
| 34 |
types = ['All'] + sorted(df['Type'].dropna().unique().tolist())
|
| 35 |
|
| 36 |
+
# Return the updated choices and reset the dropdown values to "All"
|
| 37 |
+
return labs, types, "", "All", "All"
|
| 38 |
except Exception as e:
|
| 39 |
# Handle any other errors (e.g., file not readable, invalid CSV format)
|
| 40 |
+
return ["All"], ["All"], f"Failed to load CSV: {str(e)}", "All", "All"
|
| 41 |
|
| 42 |
def filter_and_plot(selected_lab, selected_type):
|
| 43 |
global df
|
|
|
|
| 125 |
download_btn = gr.Button("Download PDF Summary")
|
| 126 |
error_box = gr.Textbox(label="Status/Error Message", visible=True, interactive=False)
|
| 127 |
|
| 128 |
+
# Connect the components
|
| 129 |
+
csv_input.change(
|
| 130 |
+
fn=upload_csv,
|
| 131 |
+
inputs=csv_input,
|
| 132 |
+
outputs=[lab_dropdown, type_dropdown, error_box, lab_dropdown, type_dropdown]
|
| 133 |
+
)
|
| 134 |
lab_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 135 |
type_dropdown.change(fn=filter_and_plot, inputs=[lab_dropdown, type_dropdown], outputs=plot_output)
|
| 136 |
download_btn.click(fn=download_pdf, inputs=[lab_dropdown, type_dropdown], outputs=gr.File(label="Download PDF"))
|