kreemyyyy commited on
Commit
2654a8e
·
verified ·
1 Parent(s): 9ccd310

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -104,10 +104,8 @@ def convert_schedule(file_path, direction):
104
  # For display, include index as a column
105
  display_df = result.reset_index().rename(columns={'index': first_col_name})
106
 
107
- # 6. Save to Excel for download with better error handling
108
- # Use temporary directory for file creation
109
- temp_dir = tempfile.gettempdir()
110
- output_filename = os.path.join(temp_dir, f"converted_{uuid.uuid4().hex}.xlsx")
111
 
112
  # Clean the DataFrame before saving
113
  result_clean = result.copy()
@@ -119,22 +117,21 @@ def convert_schedule(file_path, direction):
119
  for col in result_clean.columns:
120
  result_clean[col] = result_clean[col].astype(str)
121
 
122
- # Save with additional parameters for better compatibility
123
  try:
124
- with pd.ExcelWriter(output_filename, engine='openpyxl', mode='w') as writer:
125
- result_clean.to_excel(writer, sheet_name='Schedule', index=True, header=True)
126
 
127
- # Verify file was created successfully
128
- if not os.path.exists(output_filename):
129
- raise FileNotFoundError("Excel file was not created successfully")
 
 
130
 
131
  except Exception as save_error:
132
- # If Excel save fails, try alternative approach
133
  print(f"Excel save error: {save_error}")
134
- # Try saving as CSV as fallback
135
- csv_filename = os.path.join(temp_dir, f"converted_{uuid.uuid4().hex}.csv")
136
- result_clean.to_csv(csv_filename, index=True)
137
- output_filename = csv_filename
138
 
139
  # Return both DataFrame and download path
140
  return display_df, output_filename
 
104
  # For display, include index as a column
105
  display_df = result.reset_index().rename(columns={'index': first_col_name})
106
 
107
+ # 6. Save to Excel for download - use current directory for Gradio compatibility
108
+ output_filename = f"converted_{uuid.uuid4().hex}.xlsx"
 
 
109
 
110
  # Clean the DataFrame before saving
111
  result_clean = result.copy()
 
117
  for col in result_clean.columns:
118
  result_clean[col] = result_clean[col].astype(str)
119
 
120
+ # Save with better error handling
121
  try:
122
+ # Use simple to_excel with minimal options for better compatibility
123
+ result_clean.to_excel(output_filename, index=True, engine='openpyxl')
124
 
125
+ # Return the filename directly for Gradio to handle
126
+ if os.path.exists(output_filename):
127
+ print(f"File created successfully: {output_filename}")
128
+ else:
129
+ raise FileNotFoundError("File creation failed")
130
 
131
  except Exception as save_error:
 
132
  print(f"Excel save error: {save_error}")
133
+ # Return None if save fails
134
+ return display_df, None
 
 
135
 
136
  # Return both DataFrame and download path
137
  return display_df, output_filename