C2MV commited on
Commit
15c0025
verified
1 Parent(s): 833d492

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -431,17 +431,17 @@ def download_excel():
431
  if 'rsm' not in globals():
432
  return None, "Error: Carga los datos primero."
433
 
434
- output = io.BytesIO()
435
- with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
 
 
436
  rsm.data.to_excel(writer, sheet_name='Datos', index=False)
437
  rsm.generate_prediction_table().to_excel(writer, sheet_name='Predicciones', index=False)
438
  rsm.optimize().to_excel(writer, sheet_name='Optimizacion', index=False)
439
  rsm.calculate_contribution_percentage().to_excel(writer, sheet_name='Contribucion', index=False)
440
  rsm.calculate_detailed_anova().to_excel(writer, sheet_name='ANOVA', index=False)
441
-
442
- output.seek(0)
443
 
444
- return output, "Descargar Excel" # Return BytesIO and label
445
 
446
  def download_images():
447
  global plot_images
@@ -451,16 +451,37 @@ def download_images():
451
  if not plot_images:
452
  return None, "Error: No se han generado gr谩ficos."
453
 
454
- # Crear un archivo zip en memoria
455
- zip_buffer = io.BytesIO()
456
- with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zipf:
 
457
  for i, fig in enumerate(plot_images):
458
  img_bytes = fig.to_image(format="png")
459
  zipf.writestr(f"plot_{i}.png", img_bytes)
 
 
 
 
 
 
 
 
 
 
 
460
 
461
- zip_buffer.seek(0)
462
- return zip_buffer, "Descargar ZIP" # Return BytesIO and label
463
 
 
 
 
 
 
 
 
 
 
464
  def next_plot():
465
  global current_plot_index
466
  current_plot_index += 1
@@ -545,12 +566,14 @@ with gr.Blocks() as demo:
545
  )
546
 
547
  fit_button.click(fit_and_optimize_model, outputs=[model_completo_output, pareto_completo_output, model_simplificado_output, pareto_simplificado_output, equation_output, optimization_table_output, prediction_table_output, contribution_table_output, anova_table_output])
 
 
 
 
548
 
549
- plot_button.click(generate_rsm_plot, inputs=[fixed_variable_input, fixed_level_input], outputs=[rsm_plot_output, equation_output, plot_image_output])
550
-
551
- download_excel_button.click(download_excel, outputs=[download_excel_button, download_excel_button])
552
 
553
- download_images_button.click(download_images, outputs=[download_images_button, download_images_button])
554
 
555
  prev_plot_button.click(prev_plot, outputs=prev_plot_button).then(generate_rsm_plot, inputs=[fixed_variable_input, fixed_level_input], outputs=[rsm_plot_output, equation_output, plot_image_output])
556
 
 
431
  if 'rsm' not in globals():
432
  return None, "Error: Carga los datos primero."
433
 
434
+ # Create a temporary file
435
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx')
436
+
437
+ with pd.ExcelWriter(temp_file.name, engine='xlsxwriter') as writer:
438
  rsm.data.to_excel(writer, sheet_name='Datos', index=False)
439
  rsm.generate_prediction_table().to_excel(writer, sheet_name='Predicciones', index=False)
440
  rsm.optimize().to_excel(writer, sheet_name='Optimizacion', index=False)
441
  rsm.calculate_contribution_percentage().to_excel(writer, sheet_name='Contribucion', index=False)
442
  rsm.calculate_detailed_anova().to_excel(writer, sheet_name='ANOVA', index=False)
 
 
443
 
444
+ return temp_file.name
445
 
446
  def download_images():
447
  global plot_images
 
451
  if not plot_images:
452
  return None, "Error: No se han generado gr谩ficos."
453
 
454
+ # Create a temporary file
455
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.zip')
456
+
457
+ with zipfile.ZipFile(temp_file.name, 'w', zipfile.ZIP_DEFLATED) as zipf:
458
  for i, fig in enumerate(plot_images):
459
  img_bytes = fig.to_image(format="png")
460
  zipf.writestr(f"plot_{i}.png", img_bytes)
461
+
462
+ return temp_file.name
463
+
464
+ def generate_rsm_plot(fixed_variable, fixed_level):
465
+ global current_plot_index, plot_images
466
+
467
+ if 'rsm' not in globals():
468
+ return None, "Error: Carga los datos primero.", None
469
+
470
+ if not plot_images:
471
+ plot_images = rsm.generate_all_plots()
472
 
473
+ if not plot_images:
474
+ return None, "Error: No se pudieron generar los gr谩ficos.", None
475
 
476
+ current_plot_index = (current_plot_index) % len(plot_images)
477
+ fig = plot_images[current_plot_index]
478
+
479
+ # Create a temporary file for the image
480
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
481
+ fig.write_image(temp_file.name)
482
+
483
+ return fig, "", temp_file.name
484
+
485
  def next_plot():
486
  global current_plot_index
487
  current_plot_index += 1
 
566
  )
567
 
568
  fit_button.click(fit_and_optimize_model, outputs=[model_completo_output, pareto_completo_output, model_simplificado_output, pareto_simplificado_output, equation_output, optimization_table_output, prediction_table_output, contribution_table_output, anova_table_output])
569
+
570
+ plot_button.click(generate_rsm_plot,
571
+ inputs=[fixed_variable_input, fixed_level_input],
572
+ outputs=[rsm_plot_output, equation_output, plot_image_output])
573
 
574
+ download_excel_button.click(download_excel, outputs=[download_excel_button])
 
 
575
 
576
+ download_images_button.click(download_images, outputs=[download_images_button])
577
 
578
  prev_plot_button.click(prev_plot, outputs=prev_plot_button).then(generate_rsm_plot, inputs=[fixed_variable_input, fixed_level_input], outputs=[rsm_plot_output, equation_output, plot_image_output])
579