Update app.py
Browse files
app.py
CHANGED
|
@@ -28,45 +28,37 @@ def adjust_excel_formatting(file_path):
|
|
| 28 |
wb = load_workbook(file_path)
|
| 29 |
ws = wb.active
|
| 30 |
|
| 31 |
-
# Adjust column widths and enable text wrapping
|
| 32 |
for col in ws.columns:
|
| 33 |
max_length = 0
|
| 34 |
col_letter = col[0].column_letter
|
| 35 |
for cell in col:
|
| 36 |
if cell.value:
|
| 37 |
max_length = max(max_length, len(str(cell.value)))
|
| 38 |
-
cell.alignment = Alignment(wrap_text=True)
|
| 39 |
-
ws.column_dimensions[col_letter].width = max_length + 2
|
| 40 |
|
| 41 |
wb.save(file_path)
|
| 42 |
|
| 43 |
def process_file(input_file):
|
| 44 |
"""Process uploaded Excel file and return output"""
|
| 45 |
try:
|
| 46 |
-
# Read input file
|
| 47 |
input_df = pd.read_excel(input_file, header=1)
|
| 48 |
-
|
| 49 |
-
# Store original date order
|
| 50 |
date_columns = input_df.columns[1:].tolist()
|
| 51 |
|
| 52 |
-
# Melt to long format
|
| 53 |
df_long = input_df.melt(
|
| 54 |
id_vars=[input_df.columns[0]],
|
| 55 |
var_name='DATE',
|
| 56 |
value_name='CHATTER'
|
| 57 |
)
|
| 58 |
|
| 59 |
-
# Force date order
|
| 60 |
df_long['DATE'] = pd.Categorical(
|
| 61 |
df_long['DATE'],
|
| 62 |
categories=date_columns,
|
| 63 |
ordered=True
|
| 64 |
)
|
| 65 |
|
| 66 |
-
# Clean names
|
| 67 |
df_long['CHATTER'] = auto_correct_names(df_long['CHATTER'])
|
| 68 |
|
| 69 |
-
# Group and pivot
|
| 70 |
grouped = df_long.groupby(['CHATTER', 'DATE'], observed=True)[input_df.columns[0]] \
|
| 71 |
.apply(lambda x: ', '.join(sorted(x))).reset_index()
|
| 72 |
|
|
@@ -74,14 +66,11 @@ def process_file(input_file):
|
|
| 74 |
chatter_order = grouped['CHATTER'].value_counts().index.tolist()
|
| 75 |
final_df = pivoted.reindex(chatter_order)[date_columns].fillna("OFF")
|
| 76 |
|
| 77 |
-
# Reset index to show chatter names in preview
|
| 78 |
final_df = final_df.reset_index()
|
| 79 |
|
| 80 |
-
# Create temp file for output
|
| 81 |
temp_file_path = os.path.join(tempfile.gettempdir(), "processed_schedule.xlsx")
|
| 82 |
final_df.to_excel(temp_file_path, index=False, sheet_name='Schedule')
|
| 83 |
|
| 84 |
-
# Adjust formatting to ensure everything fits within cells
|
| 85 |
adjust_excel_formatting(temp_file_path)
|
| 86 |
|
| 87 |
return final_df, temp_file_path
|
|
@@ -90,6 +79,11 @@ def process_file(input_file):
|
|
| 90 |
error_df = pd.DataFrame({"Error": [f"⚠️ {str(e)}"]})
|
| 91 |
return error_df, None
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# Gradio interface
|
| 94 |
with gr.Blocks(title="Schedule Processor") as demo:
|
| 95 |
gr.Markdown("# 📅 Schedule Processor")
|
|
@@ -121,7 +115,6 @@ with gr.Blocks(title="Schedule Processor") as demo:
|
|
| 121 |
return df, out_path, gr.update(visible=True)
|
| 122 |
return df, None, gr.update(visible=False)
|
| 123 |
|
| 124 |
-
# Event handlers
|
| 125 |
process_btn.click(
|
| 126 |
process_and_show,
|
| 127 |
inputs=input_file,
|
|
|
|
| 28 |
wb = load_workbook(file_path)
|
| 29 |
ws = wb.active
|
| 30 |
|
|
|
|
| 31 |
for col in ws.columns:
|
| 32 |
max_length = 0
|
| 33 |
col_letter = col[0].column_letter
|
| 34 |
for cell in col:
|
| 35 |
if cell.value:
|
| 36 |
max_length = max(max_length, len(str(cell.value)))
|
| 37 |
+
cell.alignment = Alignment(wrap_text=True)
|
| 38 |
+
ws.column_dimensions[col_letter].width = max_length + 2
|
| 39 |
|
| 40 |
wb.save(file_path)
|
| 41 |
|
| 42 |
def process_file(input_file):
|
| 43 |
"""Process uploaded Excel file and return output"""
|
| 44 |
try:
|
|
|
|
| 45 |
input_df = pd.read_excel(input_file, header=1)
|
|
|
|
|
|
|
| 46 |
date_columns = input_df.columns[1:].tolist()
|
| 47 |
|
|
|
|
| 48 |
df_long = input_df.melt(
|
| 49 |
id_vars=[input_df.columns[0]],
|
| 50 |
var_name='DATE',
|
| 51 |
value_name='CHATTER'
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
df_long['DATE'] = pd.Categorical(
|
| 55 |
df_long['DATE'],
|
| 56 |
categories=date_columns,
|
| 57 |
ordered=True
|
| 58 |
)
|
| 59 |
|
|
|
|
| 60 |
df_long['CHATTER'] = auto_correct_names(df_long['CHATTER'])
|
| 61 |
|
|
|
|
| 62 |
grouped = df_long.groupby(['CHATTER', 'DATE'], observed=True)[input_df.columns[0]] \
|
| 63 |
.apply(lambda x: ', '.join(sorted(x))).reset_index()
|
| 64 |
|
|
|
|
| 66 |
chatter_order = grouped['CHATTER'].value_counts().index.tolist()
|
| 67 |
final_df = pivoted.reindex(chatter_order)[date_columns].fillna("OFF")
|
| 68 |
|
|
|
|
| 69 |
final_df = final_df.reset_index()
|
| 70 |
|
|
|
|
| 71 |
temp_file_path = os.path.join(tempfile.gettempdir(), "processed_schedule.xlsx")
|
| 72 |
final_df.to_excel(temp_file_path, index=False, sheet_name='Schedule')
|
| 73 |
|
|
|
|
| 74 |
adjust_excel_formatting(temp_file_path)
|
| 75 |
|
| 76 |
return final_df, temp_file_path
|
|
|
|
| 79 |
error_df = pd.DataFrame({"Error": [f"⚠️ {str(e)}"]})
|
| 80 |
return error_df, None
|
| 81 |
|
| 82 |
+
# Add the missing download_file function
|
| 83 |
+
def download_file(out_path):
|
| 84 |
+
"""Return the processed file path for download"""
|
| 85 |
+
return out_path
|
| 86 |
+
|
| 87 |
# Gradio interface
|
| 88 |
with gr.Blocks(title="Schedule Processor") as demo:
|
| 89 |
gr.Markdown("# 📅 Schedule Processor")
|
|
|
|
| 115 |
return df, out_path, gr.update(visible=True)
|
| 116 |
return df, None, gr.update(visible=False)
|
| 117 |
|
|
|
|
| 118 |
process_btn.click(
|
| 119 |
process_and_show,
|
| 120 |
inputs=input_file,
|