Jeremy Live commited on
Commit
4e8dd01
·
1 Parent(s): bf2b46a
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -226,24 +226,33 @@ def safe_get_column(df, column):
226
 
227
  def plot_and_save(plt, filename=None, **savefig_kwargs):
228
  import os
 
 
 
 
 
229
  if filename is None:
230
  filename = PLOT_FILENAME
231
 
232
  # Convert to absolute path
233
- filename = os.path.abspath(filename)
234
 
235
  # Ensure the directory exists
236
  os.makedirs(os.path.dirname(filename) or '.', exist_ok=True)
237
 
238
- # Set default savefig parameters if not provided
239
- save_params = {'bbox_inches': 'tight', 'dpi': 100}
240
- save_params.update(savefig_kwargs)
241
-
242
- # Save the plot
243
- plt.savefig(filename, **save_params)
244
- plt.close()
245
- print(f"[DEBUG] Plot saved to: {filename}")
246
- return filename
 
 
 
 
247
 
248
  # Monkey patch DataFrame to add safe column access
249
  import pandas as pd
 
226
 
227
  def plot_and_save(plt, filename=None, **savefig_kwargs):
228
  import os
229
+ import matplotlib
230
+
231
+ # Make sure we're using the non-interactive backend
232
+ matplotlib.use('Agg')
233
+
234
  if filename is None:
235
  filename = PLOT_FILENAME
236
 
237
  # Convert to absolute path
238
+ filename = os.path.abspath(str(filename))
239
 
240
  # Ensure the directory exists
241
  os.makedirs(os.path.dirname(filename) or '.', exist_ok=True)
242
 
243
+ try:
244
+ # Set default savefig parameters if not provided
245
+ save_params = {'bbox_inches': 'tight', 'dpi': 100}
246
+ save_params.update(savefig_kwargs)
247
+
248
+ # Save the plot using the matplotlib savefig function directly
249
+ plt.savefig(filename, **save_params)
250
+ plt.close()
251
+ print(f"[DEBUG] Plot saved to: {filename}")
252
+ return filename
253
+ except Exception as e:
254
+ print(f"[ERROR] Failed to save plot: {str(e)}")
255
+ return None
256
 
257
  # Monkey patch DataFrame to add safe column access
258
  import pandas as pd